// Path: app/src/main/java/com/game/dhanraj/myownalexa/sharedpref/Util.java
// public class Util {
// 
//     private static SharedPreferences mPreferences;
//     private static final String IDENTIFIER = "identifier";
//     private static OkHttpClient client;
// 
//     public static SharedPreferences getPrefernces(Context context){
//         if(mPreferences==null)
//         {
//             mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
//         }
//         return mPreferences;
//     }
// 
//     private static String getIdentifier() {
//         return (mPreferences!=null)?mPreferences.getString(IDENTIFIER,""):"";
//     }
// 
//     public static String getUuid() {
//         String prefix=(TextUtils.isEmpty(getIdentifier()))?"":getIdentifier()+".";
//         return prefix + UUID.randomUUID().toString();
//     }
// 
//     public static OkHttpClient getOkhttp() {
//         if (client == null)
//             client = new OkHttpClient();
//         return client;
//     }
// 
// 
// }
// 
// Path: app/src/main/java/com/game/dhanraj/myownalexa/Constants.java
// public final static String PREF_ACCESS_TOKEN = "access_token_042017";
// 
// Path: app/src/main/java/com/game/dhanraj/myownalexa/Constants.java
// public final static String PREF_REFRESH_TOKEN = "refresh_token_042017";
// 
// Path: app/src/main/java/com/game/dhanraj/myownalexa/Constants.java
// public final static String PREF_TOKEN_EXPIRES = "token_expires_042017";
// 
// Path: app/src/main/java/com/game/dhanraj/myownalexa/sharedpref/Util.java
// public static OkHttpClient getOkhttp() {
//     if (client == null)
//         client = new OkHttpClient();
//     return client;
// }


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import com.game.dhanraj.myownalexa.sharedpref.Util;
import com.google.gson.Gson;
import org.greenrobot.eventbus.EventBus;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import static com.game.dhanraj.myownalexa.Constants.PREF_ACCESS_TOKEN;
import static com.game.dhanraj.myownalexa.Constants.PREF_REFRESH_TOKEN;
import static com.game.dhanraj.myownalexa.Constants.PREF_TOKEN_EXPIRES;
import static com.game.dhanraj.myownalexa.sharedpref.Util.getOkhttp;
        okclient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                call.cancel();
                Log.d("accessToken","failed");
                doPostRequest(form,checkRefreshTokenEvent);
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                SharedPreferences preferences = Util.getPrefernces(myContext);
                myresponse = response.body().string();
                TokenResponse tokenResponse = new Gson().fromJson(myresponse, TokenResponse.class);
                saveToken(tokenResponse);
                if(checkRefreshTokenEvent==FirstMainActivityDoPostRequest) {
                    Intent st = new Intent(myContext,DownChannel.class);
                    myContext.startService(st);
                } else
                    EventBus.getDefault().post(new MessageEvent(checkRefreshTokenEvent,preferences.getString(PREF_ACCESS_TOKEN,null)));
            }
        });
    }


    public void saveToken(final TokenResponse tokenResponse) {
        REFRESH_TOKEN = tokenResponse.refresh_token;
        ACCESS_TOKEN = tokenResponse.access_token;
        SharedPreferences.Editor preferences = Util.getPrefernces(myContext).edit();
        preferences.putString(PREF_ACCESS_TOKEN, ACCESS_TOKEN);
        preferences.putString(PREF_REFRESH_TOKEN, REFRESH_TOKEN);
        //comes back in seconds, needs to be milis
// Path: app/src/main/java/com/game/dhanraj/myownalexa/sharedpref/Util.java
// public class Util {
// 
//     private static SharedPreferences mPreferences;
//     private static final String IDENTIFIER = "identifier";
//     private static OkHttpClient client;
// 
//     public static SharedPreferences getPrefernces(Context context){
//         if(mPreferences==null)
//         {
//             mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
//         }
//         return mPreferences;
//     }
// 
//     private static String getIdentifier() {
//         return (mPreferences!=null)?mPreferences.getString(IDENTIFIER,""):"";
//     }
// 
//     public static String getUuid() {
//         String prefix=(TextUtils.isEmpty(getIdentifier()))?"":getIdentifier()+".";
//         return prefix + UUID.randomUUID().toString();
//     }
// 
//     public static OkHttpClient getOkhttp() {
//         if (client == null)
//             client = new OkHttpClient();
//         return client;
//     }
// 
// 
// }
// 
// Path: app/src/main/java/com/game/dhanraj/myownalexa/Constants.java
// public final static String PREF_ACCESS_TOKEN = "access_token_042017";
// 
// Path: app/src/main/java/com/game/dhanraj/myownalexa/Constants.java
// public final static String PREF_REFRESH_TOKEN = "refresh_token_042017";
// 
// Path: app/src/main/java/com/game/dhanraj/myownalexa/Constants.java
// public final static String PREF_TOKEN_EXPIRES = "token_expires_042017";
// 
// Path: app/src/main/java/com/game/dhanraj/myownalexa/sharedpref/Util.java
// public static OkHttpClient getOkhttp() {
//     if (client == null)
//         client = new OkHttpClient();
//     return client;
// }


// Path: app/src/main/java/com/game/dhanraj/myownalexa/TokenHandler.java
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import com.game.dhanraj.myownalexa.sharedpref.Util;
import com.google.gson.Gson;
import org.greenrobot.eventbus.EventBus;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import static com.game.dhanraj.myownalexa.Constants.PREF_ACCESS_TOKEN;
import static com.game.dhanraj.myownalexa.Constants.PREF_REFRESH_TOKEN;
import static com.game.dhanraj.myownalexa.Constants.PREF_TOKEN_EXPIRES;
import static com.game.dhanraj.myownalexa.sharedpref.Util.getOkhttp;

        okclient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                call.cancel();
                Log.d("accessToken","failed");
                doPostRequest(form,checkRefreshTokenEvent);
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                SharedPreferences preferences = Util.getPrefernces(myContext);
                myresponse = response.body().string();
                TokenResponse tokenResponse = new Gson().fromJson(myresponse, TokenResponse.class);
                saveToken(tokenResponse);
                if(checkRefreshTokenEvent==FirstMainActivityDoPostRequest) {
                    Intent st = new Intent(myContext,DownChannel.class);
                    myContext.startService(st);
                } else
                    EventBus.getDefault().post(new MessageEvent(checkRefreshTokenEvent,preferences.getString(PREF_ACCESS_TOKEN,null)));
            }
        });
    }


    public void saveToken(final TokenResponse tokenResponse) {
        REFRESH_TOKEN = tokenResponse.refresh_token;
        ACCESS_TOKEN = tokenResponse.access_token;
        SharedPreferences.Editor preferences = Util.getPrefernces(myContext).edit();
        preferences.putString(PREF_ACCESS_TOKEN, ACCESS_TOKEN);
        preferences.putString(PREF_REFRESH_TOKEN, REFRESH_TOKEN);
        //comes back in seconds, needs to be milis
        preferences.putLong(PREF_TOKEN_EXPIRES, (System.currentTimeMillis() + tokenResponse.expires_in * 1000));