package tw.com.altob.goodpk.Util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.ga.sdk.util.CryptoUtil;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class PrefsUtil {

    private static final String PREF_ACCOUNT = "account";
    private static final String PREF_MESSAGE = "message";
    private static final String PREF_STICKER = "sticker";
    private static final String KEY_ACCESS_TOKEN = "accessToken";
    private static final String PREF_ADDCARLIST = "AddCar";
    private static final String FIRST = "isFirst";

    private static final byte[] seed = "Alobot".getBytes();

    public static void clearData(@NonNull Context context) {
        String[] prefs = {PREF_ACCOUNT, PREF_MESSAGE, PREF_STICKER};

        for (String pref : prefs) {
            SharedPreferences preferences = context.getSharedPreferences(
                    pref, Context.MODE_PRIVATE);

            SharedPreferences.Editor editor = preferences.edit();

            editor.clear();
            editor.apply();
        }

    }

    public static String getAccessToken(@NonNull Context context) {
        SharedPreferences preferences = context.getSharedPreferences(
                PREF_ACCOUNT, Context.MODE_PRIVATE);
        String accessToken = preferences.getString(KEY_ACCESS_TOKEN, "");

        try {
            if (!TextUtils.isEmpty(accessToken)) {
                accessToken = CryptoUtil.decrypt(accessToken, seed);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return accessToken;
    }

    public static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static boolean getFirst(@NonNull Context context){
        SharedPreferences settings;
        settings = context.getSharedPreferences(PREF_ADDCARLIST, Context.MODE_PRIVATE);

        if( settings.getString(FIRST, null) == null){
            return true;
        }else{
            return false;
        }
    }

    public static void saveFirst(@NonNull Context context){
        SharedPreferences settings;
        SharedPreferences.Editor editor;

        settings = context.getSharedPreferences(PREF_ADDCARLIST, Context.MODE_PRIVATE);
        editor = settings.edit();
        editor.putString(FIRST, "true");

        editor.commit();
    }

    public static void setAccountData(@NonNull Context context, String token) {
        SharedPreferences preferences = context.getSharedPreferences(
                PREF_ACCOUNT, Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = preferences.edit();
        try {
            editor.putString(KEY_ACCESS_TOKEN, CryptoUtil.encrypt(token, seed));

            editor.apply();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
