package com.drivemode.spotify.auth;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * @author KeishinYokomaku
 */
public class AccessTokenStore {
    public static final String TAG = AccessTokenStore.class.getSimpleName();
    private static final String KEY_ACCESS_TOKEN = "spotify_access_token";
    private static final String KEY_REFRESH_TOKEN = "spotify_refresh_token";
    private static final String KEY_EXPIRES_IN = "spotify_token_expires_at";
    private static final String KEY_RETRIEVED_AT = "spotify_token_retrieved_at";
    private static final String KEY_TOKEN_TYPE = "spotify_token_type";
    private final Application mApplication;
    private AccessToken mAccessToken;

    public AccessTokenStore(Application application) {
        mApplication = application;
    }

    @SuppressLint("CommitPrefEdits")
    public synchronized void store(AccessToken accessToken) {
        mAccessToken = accessToken;
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mApplication);
        pref.edit()
                .putString(KEY_ACCESS_TOKEN, accessToken.accessToken)
                .putString(KEY_REFRESH_TOKEN, accessToken.refreshToken)
                .putString(KEY_TOKEN_TYPE, accessToken.tokenType)
                .putLong(KEY_EXPIRES_IN, accessToken.expiresIn * 1000)
                .putLong(KEY_RETRIEVED_AT, System.currentTimeMillis())
                .commit();
    }

    @SuppressLint("CommitPrefEdits")
    public synchronized void update(AccessToken accessToken) {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mApplication);
        pref.edit()
                .putString(KEY_ACCESS_TOKEN, accessToken.accessToken)
                .putString(KEY_REFRESH_TOKEN, mAccessToken.refreshToken)
                .putString(KEY_TOKEN_TYPE, accessToken.tokenType)
                .putLong(KEY_EXPIRES_IN, accessToken.expiresIn * 1000)
                .putLong(KEY_RETRIEVED_AT, System.currentTimeMillis())
                .commit();
        mAccessToken = read();
    }

    public AccessToken read() {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mApplication);
        AccessToken token = new AccessToken();
        token.accessToken = pref.getString(KEY_ACCESS_TOKEN, null);
        token.refreshToken = pref.getString(KEY_REFRESH_TOKEN, null);
        token.tokenType = pref.getString(KEY_TOKEN_TYPE, "Bearer");
        token.expiresIn = pref.getLong(KEY_EXPIRES_IN, 0);
        return token;
    }

    public synchronized AccessToken get() {
        if (mAccessToken == null) {
            mAccessToken = read();
        }
        return mAccessToken;
    }

    public boolean isExpired() {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mApplication);
        long current = System.currentTimeMillis();
        long retrieved = pref.getLong(KEY_RETRIEVED_AT, 0);
        long duration = pref.getLong(KEY_EXPIRES_IN, 0);
        return current > (retrieved + duration);
    }
}

@@ -762,46 +762,8 @@
 ion;
-%0A    private AccessToken mAccessToken;
 %0A%0A  
@@ -961,44 +961,8 @@
 ) %7B%0A
-        mAccessToken = accessToken;%0A
     
@@ -1743,81 +1743,8 @@
 en)%0A
-                .putString(KEY_REFRESH_TOKEN, mAccessToken.refreshToken)%0A
     
@@ -1978,39 +1978,8 @@
 ();%0A
-        mAccessToken = read();%0A
     
@@ -2452,169 +2452,8 @@
  %7D%0A%0A
-    public synchronized AccessToken get() %7B%0A        if (mAccessToken == null) %7B%0A            mAccessToken = read();%0A        %7D%0A        return mAccessToken;%0A    %7D%0A%0A
     
@@ -2798,10 +2798,9 @@
 ;%0A    %7D%0A
+
 %7D
-%0A

Fix access token store