package com.xplora.xpsettings.Activity;

import android.os.SystemProperties;
import android.provider.Settings;
import android.util.Log;

public class BasePasswordActivity extends BaseActivity {
    protected static final String BD_PW_DEFAULT = "011235";
    protected static final int BD_TIMEOUT = 3600;

    protected void resetAfterTimeout(boolean isForce) {
        long time = getLeftTime();
        if (isForce || time <= 0) {
            setErrorCount(0);
            setLockTime(0);
        }
    }

    protected long getLeftTime() {
        long curTime = getCurrentTime();
        long lockTime = getLockTime();
        long costTime = curTime - lockTime;
        return costTime >= BD_TIMEOUT ? 0 : BD_TIMEOUT - costTime;
    }

    protected boolean isLock() {
        long leftTime = getLeftTime();
        int count = getErrorCount();
        return leftTime > 0 && count >= 3;
    }

    protected boolean checkTestPassword(String value) {
        return value.equals(getPwd(0));
    }

    protected boolean checkLogPassword(String value) {
        return value.equals(getPwd(1));
    }

    protected boolean checkDefaultPassword(String value) {
        return getImei().length() < 15 && value.equals(BD_PW_DEFAULT);
    }

    // type 0: test tool; type 1: log tool
    protected String getPwd(int type) {
        String imei = getImei();
        if (imei.length() < 15) {
            return BD_PW_DEFAULT;
        } else {
            imei = new StringBuilder(imei).reverse().toString();
            int start = 0;
            if (type == 0) { //相加取模
                int sum = 0;
                for (int i = 0; i < imei.length(); i++) {
                    sum += imei.charAt(i) - '0';
                }
                start = sum % 10;
            } else { //翻转取第一位
                start = imei.charAt(0) - '0';
            }
            StringBuilder sb = new StringBuilder();
            sb.append(imei.charAt(start));
            sb.append(imei.charAt(start + 1));
            sb.append(imei.charAt(start + 1));
            sb.append(imei.charAt(start + 2));
            sb.append(imei.charAt(start + 3));
            sb.append(imei.charAt(start + 5));
            return sb.toString();
        }
    }

    public void addError() {
        int count = getErrorCount();
        setErrorCount(count + 1);
        if (getLeftTime() <= 0) {
            setLockTime(getCurrentTime());
        }
    }

    public String getImei() {
        String imei = SystemProperties.get("persist.zs.imei.number", "");
        //imei = "350685600002525";
        Log.d(TAG, "getImei: " + imei);
        return imei;
    }

    public int getErrorCount() {
        return Settings.Global.getInt(getContentResolver(), "bd_error_count", 0);
    }

    public void setErrorCount(int count) {
        Settings.Global.putInt(getContentResolver(), "bd_error_count", count);
    }

    public long getLockTime() {
        return Settings.Global.getLong(getContentResolver(), "bd_lock_time", 0);
    }

    public void setLockTime(long time) {
        Settings.Global.putLong(getContentResolver(), "bd_lock_time", time);
    }

    public long getCurrentTime() {
        return System.currentTimeMillis() / 1000;
    }
}
