package com.lemonhc.mplus.framework.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.lemonhc.mplus.framework.constant.FrameworkConstant;
import com.lemonhc.mplus.framework.util.encription.AES;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.StringTokenizer;

/**
 * Created by kangsh on 2017. 6. 22..
 * 잠금화면 비밀번호 저장 및, 잠금화면 모드를 저장하는 sharedpreference singleton class.
 */

public class PreferenceUtil {

    //프리퍼런스 KEY값
    public static final String PREFERENCE_NAME = "preference_nm";
    private static PreferenceUtil preferenceModule = null;
    private static SharedPreferences prefs;
    private static SharedPreferences.Editor editor;

    private static ArrayList<Integer> arrPassword;
    private static FrameworkConstant.passwordInputType passwordInputType;
    private static String INPUT_TYPE_KEY_NAME = "inputType_key";

    private static String UNLOCK_TIME_KEY_NAME = "unlock_time_key";

    @SuppressWarnings("FieldCanBeLocal")
    private final String PASSWORD_KEY_NAME = "password_key";

    public PreferenceUtil() {
        this.arrPassword = new ArrayList<Integer>();
    }

    @SuppressLint("CommitPrefEdits")
    public static PreferenceUtil getInstance(Context context) {

        if (preferenceModule == null) {
            preferenceModule = new PreferenceUtil();
        }
        if (prefs == null) {
            prefs = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
            editor = prefs.edit();
        }
        return preferenceModule;
    }

    public void setPassword(ArrayList arrPassword){
        this.arrPassword = arrPassword;
        // 암호화하여 저장
        AES aes = new AES();
        try {
            for (int a=0; a<this.arrPassword.size(); a++){

                String temp = String.valueOf(this.arrPassword.get(a));

              editor.putString(String.valueOf(a) , aes.Encrypt(temp));
            }
            editor.commit();
        }catch (Exception e){
            Log.i("test<><><>", e.toString());
        }

    }

    public String getPasswordByIndex(int index){
        Log.e("ttt",prefs.getString(String.valueOf(index), "").trim());

        String str = prefs.getString(String.valueOf(index), "").trim();
        str.replace( System.getProperty( "line.separator" ), "" );

        return str;
    }

    public ArrayList getPassword() {

        String pw1 = prefs.getString("0", "");
        String pw2 = prefs.getString("1", "");
        String pw3 = prefs.getString("2", "");
        String pw4 = prefs.getString("3", "");

        Log.i("pwwwww"    , pw1);
        Log.i("pwwwww"    , pw2);
        Log.i("pwwwww"    , pw3);
        Log.i("pwwwww"    , pw4);
        this.arrPassword.clear();


        String savedString = prefs.getString(PASSWORD_KEY_NAME, "");
        Log.d("getPassword() >>>>>>> ", savedString);


        StringTokenizer st = new StringTokenizer(savedString, ",");
        Log.d("countTokens() >>>>>>> ", st.countTokens()+"");

        if (st.countTokens() == 0){
            return this.arrPassword;
        }

        for (int a = 0; a < 4; a++) {
//            savedList[a] = Integer.parseInt(st.nextToken());
            int temp = Integer.parseInt(st.nextToken());
            Log.e("토크나이저가 찍어주는 값",a + ":" + temp);
            this.arrPassword.add(temp);
        }
        Log.d("getPassword() >>>>>>> ", this.arrPassword.toString());
        return this.arrPassword;
    }

    public FrameworkConstant.passwordInputType getPasswordInputType() {
        String savedType = prefs.getString(INPUT_TYPE_KEY_NAME, FrameworkConstant.passwordInputType.FIRST_INPUT.getTypeName());
        this.passwordInputType = FrameworkConstant.passwordInputType.getEnum(savedType);

        Log.d("## savedType:", savedType);

        return this.passwordInputType;
    }

    public void setPasswordInputType(FrameworkConstant.passwordInputType passwordInputType) {
        this.passwordInputType = passwordInputType;
        editor.putString(INPUT_TYPE_KEY_NAME, passwordInputType.getTypeName());
        editor.commit();
    }

    public void setUnlockTime(){
        long time = System.currentTimeMillis();
        SimpleDateFormat dayTime = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        String str = dayTime.format(new Date(time));

        Log.e("setUnlockTime", "밀리초 :" + time + " , 현재시간환산 :" + str);

        editor.putLong(UNLOCK_TIME_KEY_NAME, time);
        editor.commit();
    }


    public long getUnlockTime(){
        return  prefs.getLong(UNLOCK_TIME_KEY_NAME,-1);
    }

    /**
     * SharedPreferences String type data 저장
     * @author jksong
     * @Param Context, 저장될 타입, key, value
     * @since 2019-08-08 오후 7:47
     **/
    public static void setStringPreference(Context context, String type, String key, String value) {
        SharedPreferences prefs = context.getSharedPreferences(type, context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * SharedPreferences String type data 가져오기
     * @author jksong
     * @Param Context, 저장했던 타입, key
     * @since 2019-08-08 오후 7:47
     **/
    public static String getStringPreference(Context context, String type, String key) {
//    SharedPreferences prefs = context.getSharedPreferences(type, context.MODE_MULTI_PROCESS);
        SharedPreferences prefs = context.getSharedPreferences(type, context.MODE_PRIVATE);
        String rtn = prefs.getString(key, "");

        return rtn;
    }

}


