package com.videogo.util;

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

import androidx.annotation.NonNull;

import com.videogo.global.ValueKeys;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 用于demo的SharedPreference工具类
 */
public class SpTool {

    private final static String TAG = SpTool.class.getSimpleName();
    private final static String SP_FILE_NAME = "demo";
    private static SharedPreferences mSP = null;

    /**
     * 初始化
     */
    public static void init(@NonNull Context context) {
        mSP = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
    }

    /**
     * 安全Key取
     */
    public static String obtainValue(ValueKeys key) {
        return obtainValue(key.name());
    }

    /**
     * 取
     */
    public static String obtainValue(String key) {
        if (mSP == null) {
            Log.e(TAG, "SpTool is not init!!!");
            return null;
        }
        return mSP.getString(key, null);
    }

    /**
     * 存
     */
    public static void storeValue(ValueKeys key, String value) {
        storeValue(key.name(), value);
    }

    /**
     * 安全Key存
     */
    public static void storeValue(String key, String value) {
        if (mSP == null) {
            Log.e(TAG, "SpTool is not init!!!");
            return;
        }
        mSP.edit().putString(key, value).apply();
    }


    /**
     * 安全Key取
     */
    public static boolean obtainBooleanValue(ValueKeys key) {
        return obtainBooleanValue(key.name());
    }

    /**
     * 取
     */
    public static boolean obtainBooleanValue(String key) {
        if (mSP == null) {
            Log.e(TAG, "SpTool is not init!!!");
            return false;
        }
        return mSP.getBoolean(key, false);
    }

    /**
     * 存
     */
    public static void storeBooleanValue(ValueKeys key, boolean value) {
        storeBooleanValue(key.name(), value);
    }

    /**
     * 安全Key存
     */
    public static void storeBooleanValue(String key, boolean value) {
        if (mSP == null) {
            Log.e(TAG, "SpTool is not init!!!");
            return;
        }
        mSP.edit().putBoolean(key, value).apply();
    }

    /**
     * 清空所有数据
     */
    public static void clearAllValues() {
        if (mSP == null) {
            Log.e(TAG, "SpTool is not init!!!");
            return;
        }
        mSP.edit().clear().commit();
    }

    /**
     * 只清除非系统级的 key（保留 ValueKeys 枚举中定义的系统级数据，如登录参数等）
     */
    public static void clearNonSystemValues() {
        if (mSP == null) {
            Log.e(TAG, "SpTool is not init!!!");
            return;
        }
        // 收集所有系统级 key
        Set<String> systemKeys = new HashSet<>();
        for (ValueKeys key : ValueKeys.values()) {
            systemKeys.add(key.name());
        }
        // 遍历 SP 中所有 key，删除非系统级的
        Map<String, ?> allEntries = mSP.getAll();
        if (allEntries == null || allEntries.isEmpty()) {
            return;
        }
        SharedPreferences.Editor editor = mSP.edit();
        for (String key : allEntries.keySet()) {
            if (!systemKeys.contains(key)) {
                editor.remove(key);
            }
        }
        editor.commit();
    }

}
