package com.example.chemical.utils;

import android.net.Uri;
import android.util.Log;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ShellUtils;
import com.blankj.utilcode.util.Utils;
import com.lztek.toolkit.Lztek;

import java.util.Locale;

import okhttp3.HttpUrl;

public enum Tool {
    INSTANCE;
    private Lztek lztek;

    private Tool() {
        lztek = Lztek.create(Utils.getApp());
    }

    public void cmd(String cmd) {
        LogUtils.d(cmd);
        lztek.suExec(cmd);
    }

    public void reStartApp() {
        exitApp();
        cmd("monkey -p " + AppUtils.getAppPackageName() + " -c android.intent.category.LAUNCHER 1");
    }

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

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

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

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

    public void setBluetooth(boolean isEnable) {
        cmd("settings put global bluetooth_on " + (isEnable ? "1" : "0"));
    }

    public void openAdb() {
        cmd("setprop persist.internet_adb_enable 1");
    }

    public ShellUtils.CommandResult stopApp(String packageName) {
        return ShellUtils.execCmd("am force-stop " + packageName, true);
    }

    public void setLauncher() {
        cmd("pm set-home-activity " + AppUtils.getAppPackageName());
    }

    public void startMasterApp() {
        openApp(SPUtils.getInstance().getString("masterApp", "com.zhong.che"));
    }

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

    public String getSerialNumber() {
        String mac = lztek.getEthMac().toLowerCase(Locale.getDefault());
        return mac.replace(":", "");
    }

    public HttpUrl getBaseUrl() {
        if (AppUtils.isAppDebug()) {
            return HttpUrl.get("http://192.168.1.8/api/");
        } else {
            return HttpUrl.get("http://172.16.0.65/api/");
        }
    }

    public String checkUrl(String url) {
        try {
            String uriStr = url;
            if (!url.matches("https?://.*") && !url.matches("http?://.*")) {
                uriStr = Uri.parse(getBaseUrl() + url).toString();
            }
            return uriStr;
        } catch (Exception e) {
            LogUtils.e(Log.getStackTraceString(e));
            return url;
        }
    }

}
