package xn.xxp.utils;

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;

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;

    public void openDoor() {
        if (isLocking) {
            ToastUtils.showLong("门已开");
            return;
        }
        isLocking = true;
        if (!DeviceUtils.getElectricLock(1)) {
            DeviceUtils.setElectricLock(1, true);
            ThreadUtils.executeByCachedWithDelay(new ThreadUtils.SimpleTask<Object>() {
                @Override
                public Object doInBackground() throws Throwable {
                    return null;
                }

                @Override
                public void onSuccess(Object result) {
                    isLocking = false;
                    DeviceUtils.setElectricLock(1, false);
                }
            }, 5, TimeUnit.SECONDS);
        }
    }

    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;
    }

}
