package com.fujica.abk.api;

import com.fujica.abk.utils.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 缓存工具类
 */
public class cache {
    private static final String PREF_NAME = "fujicaV2";
    
    // 缓存键名
    public static final String KEY_TOKEN = "token";
    public static final String KEY_OPEN_ID = "open_id";
    public static final String KEY_MOBILE = "mobile";

    /**
     * 获取Preferences实例
     */
    private static Preferences getPref(Context context) {
        if (context == null) {
            return null;
        }
        try {
            DatabaseHelper databaseHelper = new DatabaseHelper(context.getApplicationContext());
            return databaseHelper.getPreferences(PREF_NAME);
        } catch (Exception e) {
            Log.error(e);
            return null;
        }
    }

    /**
     * 获取缓存值
     */
    public static String get(Context context, String key) {
        try {
            Preferences pref = getPref(context.getApplicationContext());
            if (pref != null && pref.hasKey(key)) {
                String val = pref.getString(key, "");
                return val == null ? "" : val;
            }
            return "";
        } catch (Exception e) {
            Log.error(e);
            return "";
        }
    }

    /**
     * 设置缓存值
     */
    public static void set(Context context, String key, String value) {
        try {
            Preferences pref = getPref(context.getApplicationContext());
            if (pref != null) {
                pref.putString(key, value);
                pref.flushSync();
            }
        } catch (Exception e) {
            Log.error(e);
        }
    }


    /**
     * 获取Token
     */
    public static String getToken(Context context) {
        return get(context, KEY_TOKEN);
    }

    /**
     * 设置Token
     */
    public static void setToken(Context context, String value) {
        set(context, KEY_TOKEN, value);
    }


    /**
     * 设置OpenId
     */
    public static void setOpenId(Context context, String value) {
        set(context, KEY_OPEN_ID, value);
    }


    /**
     * 设置手机号
     */
    public static void setMobile(Context context, String value) {
        set(context, KEY_MOBILE, value);
    }

    /**
     * 登出，清除相关缓存
     */
    public static void logout(Context context) {
        try {
            Preferences pref = getPref(context);
            if (pref != null) {
                pref.delete(KEY_TOKEN);
                pref.delete(KEY_MOBILE);
                pref.delete(KEY_OPEN_ID);
                pref.flushSync();
            }
        } catch (Exception e) {
            Log.error(e);
        }
    }

    /**
     * 判断是否已认证
     */
    public static boolean isAuth(Context context) {
        String token = getToken(context);
        return token != null && !token.isEmpty();
    }

}
