package com.example.iot_controlhost.utils.global;

import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.example.iot_controlhost.utils.MMKVUtil;
import com.example.iot_controlhost.utils.log.MyLog;

/**
 * 硬件相关配置
 */
public class HardwareSetting {
    /**
     * 主机码（设备唯一码）
     */
    public static final String HOST_ID = "HOST_ID";
    /**
     * IMEI(国际移动设备识别码)
     */
    public static final String IMEI = "IMEI";
    /**
     * ICCID（物联网卡号）
     */
    public static final String ICCID = "ICCID";
    /**
     * IMSI（国际移动用户识别码）
     */
    public static final String IMSI = "IMSI";

    /**
     * 获取IMSI国际移动用户识别码
     */
    public static String getImsi() {
        return MMKVUtil.get(IMSI, "-");
    }

    /**
     * 获取ICCID物联网卡号
     */
    public static String getIccid() {
        return MMKVUtil.get(ICCID, "-");
    }

    /**
     * 刷新蜂窝设备及SIM卡标识。
     * Android 8.0及以上使用getImei，Android 7.1使用getDeviceId兼容获取IMEI。
     */
    public static void refreshMobileIdentifiers(Context context) {
        TelephonyManager telephonyManager =
                (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager == null) {
            MMKVUtil.put(ICCID, "-");
            MMKVUtil.put(IMEI, "-");
            MyLog.appError("蜂窝设备标识获取失败：TelephonyManager不可用");
            return;
        }

        String iccid = null;
        String imei = null;
        try {
            iccid = telephonyManager.getSimSerialNumber();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                imei = telephonyManager.getImei();
            } else {
                imei = telephonyManager.getDeviceId();
            }
        } catch (SecurityException e) {
            MyLog.appError("蜂窝设备标识获取失败：缺少读取设备标识权限");
        } catch (Exception e) {
            MyLog.appError("蜂窝设备标识获取失败：" + e.getMessage());
        }

        // 始终覆盖旧值，避免历史版本将ICCID存入IMEI后继续错误上传。
        MMKVUtil.put(ICCID, normalizeIdentifier(iccid));
        MMKVUtil.put(IMEI, normalizeIdentifier(imei));
    }

    private static String normalizeIdentifier(String identifier) {
        return TextUtils.isEmpty(identifier) ? "-" : identifier.trim();
    }

    /**
     * 控制器串口
     */
    public static final String CONTROLLER_SERIAL_COM = "控制器串口";
    /**
     * 控制器波特率
     */
    public static final String CONTROLLER_SERIAL_BAUD = "控制器波特率";
    /**
     * 传感器串口
     */
    public static final String SENSOR_SERIAL_COM = "传感器串口";
    /**
     * 传感器波特率
     */
    public static final String SENSOR_SERIAL_COM_BAUD = "传感器波特率";
    /**
     * 工作模式
     */
    public static String[] CONTROLLER_WORK_MODE = {"从机模式"/**, "主机模式"**/};

    /**
     * 第一次注册主机
     */
    public static final String IS_REGISTERED = "IS_REGISTERED";
    /**
     * 通用地址-集成控制器用
     */
    public static final String COMMON_ADDRESS = "通用地址-集成控制器用";
    /**
     * 当前IP地址 ipv4
     */
    public static final String IP_ADDRESS = "IP_ADDRESS";
    /**
     * FRP服务器给自己分配的端口
     */
    public static final String REMOTE_PORT = "REMOTE_PORT";
    /**
     * FRP服务器给自己分配的端口
     */
    public static final String REMOTE_SERVER_IPADDRESS = "REMOTE_SERVER_IPADDRESS";
    /**
     * 控制器工作模式
     * 集成式：主机模式/从机模式
     * 装配式：从机模式
     */
    public static final String MY_CONTROLLER_WORK_MODE = "控制器工作模式";

    public static String getHostId() {
        return MMKVUtil.get(HOST_ID, "未获取");
    }

    public static String getImei() {
        return MMKVUtil.get(IMEI, "-");
    }

    public static String getControllerSerialCom() {
        return MMKVUtil.get(CONTROLLER_SERIAL_COM, SpinnerList.SERIAL_ADDRESS[5]);
    }

    public static void setControllerSerialCom(String controllerSerialCom) {
        MMKVUtil.put(CONTROLLER_SERIAL_COM, controllerSerialCom);
    }

    public static String getSensorSerialCom() {
        return MMKVUtil.get(SENSOR_SERIAL_COM, SpinnerList.SERIAL_ADDRESS[1]);
    }

    public static void setSensorSerialCom(String sensorSerialCom) {
        MMKVUtil.put(SENSOR_SERIAL_COM, sensorSerialCom);
    }

    public static String getCommonAddress() {
        return MMKVUtil.get(COMMON_ADDRESS, SpinnerList.ADDRESS[0]);
    }

    public static void setCommonAddress(String address) {
        MMKVUtil.put(COMMON_ADDRESS, address);
    }

    public static String getWorkMode() {
        return MMKVUtil.get(MY_CONTROLLER_WORK_MODE, CONTROLLER_WORK_MODE[0]);
    }

    public static void setWorkMode(String controllerWorkMode) {
        MMKVUtil.put(MY_CONTROLLER_WORK_MODE, controllerWorkMode);
    }

    public static String getControllerSerialBaud() {
        return MMKVUtil.get(CONTROLLER_SERIAL_BAUD, SpinnerList.BAUD[3]);
    }

    public static void setControllerSerialBaud(String controllerSerialBaud) {
        MMKVUtil.put(CONTROLLER_SERIAL_BAUD, controllerSerialBaud);
    }

    public static String getSensorSerialComBaud() {
        return MMKVUtil.get(SENSOR_SERIAL_COM_BAUD, SpinnerList.BAUD[3]);
    }

    public static void setSensorSerialComBaud(String sensorSerialComBaud) {
        MMKVUtil.put(SENSOR_SERIAL_COM_BAUD, sensorSerialComBaud);
    }

    /**
     * 是否已经注册
     */
    public static boolean isRegistered() {
        return MMKVUtil.get(IS_REGISTERED, false);
    }

    /**
     * 设置已注册
     */
    public static void setRegistered() {
        MMKVUtil.put(IS_REGISTERED, true);
    }


    /**
     * 旧版Android 7
     */
    public static final String ZC_328 = "ZC-328";
    /**
     * 旧版Android 11
     */
    public static final String ZC_356 = "ZC-356";
    /**
     * 新版Android 11
     */
    public static final String RK3568_R = "rk3568_r";

    public static boolean isZCAndroid() {
        return Build.MODEL.equals(HardwareSetting.ZC_328) || Build.MODEL.equals(HardwareSetting.ZC_356);
    }

    public static boolean isYSAndroid() {
        return Build.MODEL.equals(HardwareSetting.RK3568_R);
    }

    /**
     * 获取设备型号
     */
    public static String getDeviceModel() {
        return Build.MODEL;
    }

    public static String getIPAddress() {
        return MMKVUtil.get(IP_ADDRESS, "172.16.1.103");
    }

    public static String getRemotePort() {
        return MMKVUtil.get(REMOTE_PORT, "6000");
    }

    public static String getRemoteServerIPAddress() {
        return MMKVUtil.get(REMOTE_SERVER_IPADDRESS, "0.0.0.0");
    }


    public static final String FRP_VERSION = "FRP_VERSION";

    public static int getFrpVersion() {
        return MMKVUtil.get(FRP_VERSION, 0);
    }

}
