package xn.xxp.utils;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ShellUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.hikvision.dmb.system.InfoSystemApi;
import com.njlz.card.DeviceUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import http.client.HttpTool;
import xn.xxp.app.LabApp;
import xn.xxp.home.auth.AuthType;

public enum Tool {
    INSTANCE;

    public void reStartApp(String msg) {
        LogUtils.d("重启应用", msg);
        exitApp();
        InfoSystemApi.execCommand("monkey -p " + AppUtils.getAppPackageName() + " -c android.intent.category.LAUNCHER 1");
    }

    public void reboot() {
        InfoSystemApi.reboot();
    }

    public void openFileBrowser() {
        openApp("display.interactive.filebrowser");
    }

    public void exitApp() {
        InfoSystemApi.execCommand("am force-stop " + AppUtils.getAppPackageName());
    }

    public void openSetting() {
        openApp("display.interactive.hsetting");
    }

    public void openApp(String packageName) {
        InfoSystemApi.execCommand("monkey -p " + packageName + " -c android.intent.category.LAUNCHER 1");
    }

    public void getPermission() {
        cmd("appops set --uid " + AppUtils.getAppPackageName() + " MANAGE_EXTERNAL_STORAGE allow");
        cmd("pm grant " + AppUtils.getAppPackageName() + " android.permission.READ_PHONE_STATE");
        cmd("pm grant " + AppUtils.getAppPackageName() + " android.permission.CAMERA");
        cmd("pm grant " + AppUtils.getAppPackageName() + " android.permission.RECORD_AUDIO");
    }

    public void openAdb() {
        InfoSystemApi.openAdb();
    }

    /**
     * 保活updateApp
     */
    public void startTaskService() {
        cmd("am startservice -n xn.update/xn.update.service.TaskService");
    }

    public int cmd(String cmd) {
        LogUtils.d(cmd);
        return InfoSystemApi.execCommand(cmd);
    }

    private volatile boolean isLocking = false;

    /**
     * 正常开门
     *
     * @param openType 开门类型
     * @return 0 开锁失败
     * 1 开锁成功
     * 2 开锁中不响应
     */
    public int openDoor(String userId, AuthType openType) {
        int status = 2;
        if (isLocking) {
            ToastUtils.showLong("门已开");
            return status;
        }
        if (!DeviceUtils.getElectricLock(1)) {
            isLocking = true;
            if (0 == DeviceUtils.setElectricLock(1, true)) {
                status = 1;
            } else {
                status = 0;
            }
            if (!TextUtils.isEmpty(userId) && null != openType) {
                AsyncTask.execute(() -> HttpTool.updateRecord(userId, openType));
            }
            ThreadUtils.executeByCachedWithDelay(new ThreadUtils.SimpleTask<Object>() {
                @Override
                public Object doInBackground() throws Throwable {
                    return null;
                }

                @Override
                public void onSuccess(Object result) {
                    isLocking = false;
                    closeDoor();
                }
            }, 5, TimeUnit.SECONDS);
        }
        return status;
    }

    /**
     * 关门
     */
    public void closeDoor() {
        DeviceUtils.setElectricLock(1, false);
    }

    public int dip2px(float dp) {
        float scale = Utils.getApp().getResources().getDisplayMetrics().density;
        float px = dp * scale + 0.5F;
        return (int) px;
    }

    public int spanCount(int gridExpectedSize, int offset, int horizontalSpacing) {
        int screenWidth = Utils.getApp().getResources().getDisplayMetrics().widthPixels - offset + horizontalSpacing;
        float expected = (float) screenWidth / ((float) gridExpectedSize + horizontalSpacing);
        int spanCount = Math.round(expected);
        if (spanCount == 0) {
            spanCount = 1;
        }
        return spanCount;
    }

}
