package com.hh.arome.utils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.blankj.utilcode.util.FileUtils;
import com.hh.lib_base.bean.device.DeviceConfigResultBean;
import com.hh.arome.services.keeplive.LocalService;
import com.hh.arome.services.keeplive.MyJobService;
import com.hh.arome.services.keeplive.RemoteService;
import com.hh.lib_base.BaseApplication;
import com.hh.lib_base.log.AppLogUtils;
import com.hh.lib_base.log.LogType;
import com.hh.lib_base.utils.GsonUtils;
import com.hh.lib_base.utils.StringUtils;
import com.hh.lib_common.local.AppLocalConfigHandler;
import com.hh.lib_common.local.IAppLocalConfig;
import com.hh.lib_device.deviceui.IDeviceUI;

import java.io.File;

/**
 * Description: $
 *
 * @author xwh
 * time 2024/8/22
 **/
public class CommonUtils {


    /**
     * @author xwh
     * Time 2024/8/22
     * Description: 则取本地设备信息对象
     **/
    public static DeviceConfigResultBean getDeviceConfigDataFromLocal() {
        DeviceConfigResultBean deviceConfigBean = null;
        IAppLocalConfig localConfig = AppLocalConfigHandler.create(BaseApplication.getContext(), IAppLocalConfig.class);
        String deviceConfigResultBeanStr = localConfig.getDeviceConfigResultBeanStr("");
        if (StringUtils.isNotNull(deviceConfigResultBeanStr)) {
            deviceConfigBean = GsonUtils.getGson().fromJson(deviceConfigResultBeanStr, DeviceConfigResultBean.class);
            return deviceConfigBean;
        }
        return null;
    }

    /**
     * @author xwh
     * Time 2024/8/22
     * Description: 如果接口返回空，则取本地设备信息对象
     **/
    public static DeviceConfigResultBean getDeviceConfigData(DeviceConfigResultBean bean) {
        if (bean == null) {
            DeviceConfigResultBean deviceConfigDataFromLocal = getDeviceConfigDataFromLocal();
            if (deviceConfigDataFromLocal == null) {
                return null;
            } else {
                return deviceConfigDataFromLocal;
            }

        } else if (TextUtils.isEmpty(bean.getSn())) {
            DeviceConfigResultBean deviceConfigDataFromLocal = getDeviceConfigDataFromLocal();
            if (deviceConfigDataFromLocal == null) {
                return null;
            } else {
                return deviceConfigDataFromLocal;
            }
        } else {
            return bean;
        }
    }


    /**
     * @author xwh
     * Time 控制状态栏, 禁止下拉通知栏 false-不启用(默认) true-启用
     * Description:
     **/
    public static void forbidPullDown(Activity context, IDeviceUI iDeviceUI, boolean forbidPullDown) {
        if (iDeviceUI == null || context == null) {
            return;
        }
        if (context.isFinishing()) {
            return;
        }
        if (forbidPullDown) {
            iDeviceUI.hideNotification(context);
        } else {
            iDeviceUI.showNotification(context);
        }
    }

    /**
     * @author xwh
     * Time 2024/8/22
     * Description: 控制导航栏,隐藏底部导航栏 false-不启用(默认) true-启用
     **/
    public static void navigationBar(Activity context, IDeviceUI iDeviceUI, boolean hideNavigationBar) {
        if (iDeviceUI == null || context == null) {
            return;
        }
        if (context.isFinishing()) {
            return;
        }
        if (hideNavigationBar) {
            iDeviceUI.hideNavigationBar(context);
        } else {
            iDeviceUI.showNavigationBar(context);
        }
    }

    /**
     * @author xwh
     * Time 2024/8/21
     * Description: 删除大于7天的日志
     **/
    public static void delLocalLog() {
        // 删除日志
        AppLogUtils.deleteFileLog(LogType.Logcat, 7);
        AppLogUtils.deleteFileLog(LogType.Error, 7);
        AppLogUtils.deleteFileLog(LogType.Crash, 7);
    }

    /**
     * @author xwh
     * Time 2024/8/21
     * Description: 启动保活服务
     **/
    public static void startKeepliveService(Context context) {
        // 可能导致收到两次广播
        LocalService.startService(context);
        RemoteService.startService(context);
        MyJobService.Companion.startJob(context);
    }

    public static void stopKeepliveService(Context context) {
        // 可能导致收到两次广播
        LocalService.stopService(context);
        RemoteService.stopService(context);
        MyJobService.Companion.stopJob(context);
    }

    /**
     * @author xwh
     * Time 2024/8/23
     * Description: 删除临时缓存身份证缓存图片
     **/
    public static void deleteAllPicture() {

        // 来自打印图片
        String path = "/storage/emulated/0/Android/data/com.hh.arome/files/picture";

        boolean orExistsDir = FileUtils.createOrExistsDir(path);

        if (!orExistsDir) {
            AppLogUtils.e("没有该图片文件");
            return;
        } else {
            AppLogUtils.e("有该图片文件");
        }
        File file = new File(path);
        File[] files = file.listFiles();
        if (files != null) {
            for (File logFile : files) {
                FileUtils.delete(logFile);
            }
        } else {
        }

    }


}
