package com.android.launcher3.util;

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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.launcher3.manager.ColumnDef;
import com.android.launcher3.model.AppTimeConfig;
import com.android.launcher3.model.TimeConfig;

import java.util.HashMap;
import java.util.Map;

public class TimeConfigSaver {
    private static final String TAG = "TimeConfigSaver";
    private SharedPreferences sharedPreferences;
    private static final String CONFIG = "config";

    private static final String LOCK_SCREEN_END_TIME = "lock_screen_time";

    private static final String LOCK_SCREEN_TYPE = "lock_screen_type";

    private static final String WORK_MODE = "work_mode";

    private static final String UNINSTALL = "uninstall";

    private static final String EXIT = "exit";

    public TimeConfigSaver(Context context) {

        sharedPreferences = context.getApplicationContext().getSharedPreferences("app_config", Context.MODE_PRIVATE);
    }

    public void save(Map<Integer, TimeConfig> appConfig) {

        SharedPreferences.Editor editor = sharedPreferences.edit();

        Logger.e(TAG, "Set\t" + JSON.toJSONString(appConfig));
        editor.putString(CONFIG, JSON.toJSONString(appConfig));

        editor.apply();
    }


    public Map<Integer, TimeConfig> getConfig() {
        String configStr = sharedPreferences.getString(CONFIG, "");
        Logger.e(TAG, "Get\t" + configStr);
        if (TextUtils.isEmpty(configStr)) {
            return new HashMap<Integer, TimeConfig>();
        }

        Map<Integer, TimeConfig> configMap = JSON.parseObject(configStr, new TypeReference<Map<Integer, TimeConfig>>() {
        });

        if (configMap == null) {
            return new HashMap<Integer, TimeConfig>();
        }

        return configMap;
    }


    public void updateLockScreenEndTime(long timeStamp) {
        Logger.e("updateLockScreenEndTime", timeStamp + "");
        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putLong(LOCK_SCREEN_END_TIME, timeStamp);

        editor.apply();
    }


    public long getLockScreenEndTime() {
        return sharedPreferences.getLong(LOCK_SCREEN_END_TIME, -1);
    }


    public void updateLockScreenType(@ColumnDef.LockScreen.LockScreenMode int lockScreenMode) {
        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putInt(LOCK_SCREEN_TYPE, lockScreenMode);

        editor.apply();
    }


    public int getLockScreenType() {
        return sharedPreferences.getInt(LOCK_SCREEN_TYPE, ColumnDef.LockScreen.MODE_UNLOCK);
    }


    public void saveWorkMode(@ColumnDef.TimeManager.TimeMode int mode) {
        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putInt(WORK_MODE, mode);

        editor.apply();
    }


    public int getCurrentWorkMode() {
        return sharedPreferences.getInt(WORK_MODE, ColumnDef.TimeManager.WORK_DAY);
    }

    public void putUninstallEnable(boolean enable) {
        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putBoolean(UNINSTALL, enable);

        editor.apply();
    }


    public boolean getUninstallEnable() {
        return sharedPreferences.getBoolean(UNINSTALL, false);
    }


    public void putExitState(boolean enable) {
        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putString(EXIT, enable ? "a" : "b");

        editor.apply();
    }


    public boolean getExitState() {
        return sharedPreferences.getString(EXIT, "a").equals("a");
    }
}
