package com.uiui.zyappstore.manager;

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

import com.tencent.mmkv.MMKV;
import com.uiui.zyappstore.utils.TimeUtils;

public class ConnectManager {
    private static final String TAG = ConnectManager.class.getSimpleName();


    public static final long ONE_MINUTES_TIME = 60 * 1000;
    public static final long FIFTEEN_MINUTES_TIME = ONE_MINUTES_TIME * 15;
    public static final long HALF_HOUR_TIME = FIFTEEN_MINUTES_TIME * 2;
    public static final long ONE_HOUR_TIME = HALF_HOUR_TIME * 2;
    public static final long SIX_HOUR_TIME = ONE_HOUR_TIME * 6;
    public static final long HALF_DAY_TIME = SIX_HOUR_TIME * 2;
    public static final long ONE_DAY_TIME = HALF_DAY_TIME * 2;

    /*重启后连接成功的时间*/
    public static final String REBOOT_LAST_ONNECT_TIME = "reboot_last_connect_time";
    /*WiFi连接后连接成功的时间*/
    public static final String WIFI_LAST_CONNECT_TIME = "WiFi_last_connect_time";
    /*打开设备信息连接成功的时间*/
    public static final String OPENINFO_LAST_ONNECT_TIME = "opneinfo_last_connect_time";


    @SuppressLint("StaticFieldLeak")
    private static ConnectManager sInstance;
    private Context mContext;
    private MMKV mMMKV = MMKV.defaultMMKV();

    private ConnectManager(Context context) {
        if (context == null) {
            throw new RuntimeException("Context is NULL");
        }
        this.mContext = context;

    }

    public static void init(Context context) {
        if (sInstance == null) {
            Log.e(TAG, "init: ");
            sInstance = new ConnectManager(context);
        }
    }

    public static ConnectManager getInstance() {
        if (sInstance == null) {
            throw new IllegalStateException("You must be init ConnectManager first");
        }
        return sInstance;
    }

    public long getConnectModeTime(ConnectMode connectMode) {
        long time = 0;
        switch (connectMode) {
            case DEFAULT:
                time = 0;
                break;
            case ONE_MINUTE:
                time = ONE_MINUTES_TIME;
                break;
            case FIFTEEN_MINUTES:
                time = FIFTEEN_MINUTES_TIME;
                break;
            case HALF_HOUR:
                time = HALF_HOUR_TIME;
                break;
            case ONE_HOUR:
                time = ONE_HOUR_TIME;
                break;
            case SIX_HOUR:
                time = SIX_HOUR_TIME;
                break;
            case HALF_DAY:
                time = HALF_DAY_TIME;
                break;
            case ONE_DAY:
                time = ONE_DAY_TIME;
                break;
            default:
        }
        return time;
    }

    public boolean isNeedConnect(String key, ConnectMode connectMode) {
        long nowTime = System.currentTimeMillis();
        long lastTime = mMMKV.decodeLong(key, 0);
        long intervalTime = getConnectModeTime(connectMode);
        //防止一分钟内重复请求
        return nowTime - lastTime > intervalTime && nowTime - lastTime > ONE_MINUTES_TIME;
    }


    /**
     * @return 重启后是否连接
     */
    public boolean isRebootFistConnect() {
        long rebootTime = mMMKV.decodeLong(REBOOT_LAST_ONNECT_TIME, 0);
        //只在开机后15内连接，其他情况为service重启
        long time = System.currentTimeMillis() - rebootTime;
        return time < 15 * 1000;
    }

    /**
     * @return 今天WiFi连接是否有连接
     */
    public boolean isWiFiFistConnect() {
        long time = mMMKV.decodeLong(WIFI_LAST_CONNECT_TIME, 0);
        return !TimeUtils.isTodayTime(time);
    }

    /**
     * @return 今天打开设备信息后是否连接
     */
    public boolean isOpenInfoFistConnect() {
        long time = mMMKV.decodeLong(OPENINFO_LAST_ONNECT_TIME, 0);
        return !TimeUtils.isTodayTime(time);
    }

    /**
     * @param WiFiAlias
     * @return 今天切换WiFi后是否连接
     */
    public boolean isWiFiCutoverFistConnect(String WiFiAlias) {
        long time = mMMKV.decodeLong(WiFiAlias, 0);
        return !TimeUtils.isTodayTime(time);
    }


}
