package net.northking.cctp.upperComputer.driver.usbmuxd;


import net.northking.cctp.upperComputer.driver.usbmuxd.payload.usbmuxd.entity.DeviceConnectionDetail;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 苹果设备对象
 * <br>
 * 用于信息查询，以及usbmuxd和设备内部服务通信
 */
public class AppleDevice {

    private final Logger log = LoggerFactory.getLogger(AppleDevice.class);
    /**
     * 设备连接信息
     */
    private DeviceConnectionDetail connectionDetail;

    private final String serial;

    private String deviceName = null;

    private String productType = null;

    private String productVersion = null;

    private Integer productMainVersion = null;

    private String wifiAddress = null;


    public AppleDevice(DeviceConnectionDetail detail) {
        this.connectionDetail = detail;
        this.serial = detail.serialNumber;
    }

    public AppleDevice(String serial) {
        this.serial = serial;
    }

    public void updateConnectionDetail(DeviceConnectionDetail detail) {
        this.connectionDetail = detail;
    }

    public DeviceConnectionDetail getConnectionDetail() {
        return connectionDetail;
    }


    public void setConnectionDetail(DeviceConnectionDetail connectionDetail) {
        this.connectionDetail = connectionDetail;
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public String getProductType() {
        return productType;
    }

    public void setProductType(String productType) {
        this.productType = productType;
    }

    public String getProductVersion() {
        return productVersion;
    }

    public void setProductVersion(String productVersion) {
        this.productVersion = productVersion;
        String[] versions = productVersion.split("\\.");
        try {
            this.productMainVersion = Integer.parseInt(versions[0]);
        } catch (NumberFormatException e) {
            log.error("无法处理主要版本号：" + productVersion);
        }
    }

    public String getWifiAddress() {
        return wifiAddress;
    }

    public void setWifiAddress(String wifiAddress) {
        this.wifiAddress = wifiAddress;
    }

    public String getSerial() {
        return serial;
    }

    public boolean isNeedTunnel() {
        int mainVersion = getSystemMainVersion();
        if (mainVersion == 0) return false;
        return mainVersion >= 17; //iOS 17及之后版本需要macOS系统作为通信支撑
    }

    public int getSystemMainVersion() {
        if (productVersion == null) throw new IllegalStateException("无法比较系统版本，因为productVersion为null，请先加载设备基本信息");
        String[] versions = productVersion.split("\\.");
        if (versions.length == 0) {
            log.error("无法处理设备productVersion，原始值：" + productVersion);
            return 0;
        }
        String mainVersion = versions[0];
        if (StringUtils.isBlank(mainVersion)) {
            log.error("主要版本为空白，无法比较，完整版本：" + productVersion + "，主要版本：" + mainVersion);
            return 0;
        }
        int mainVersionNumber;
        try {
            mainVersionNumber = Integer.parseInt(mainVersion);
        } catch (NumberFormatException e) {
            log.error("主要版本无法转为整型数字：" + mainVersion, e);
            return 0;
        }
        return mainVersionNumber;
    }

    public int getProductMainVersion() {
        if (productMainVersion == null) {
            throw new IllegalStateException("productVersion未设置的情况下尝试获取主要版本");
        }
        return productMainVersion;
    }

    @Override
    public String toString() {
        return "AppleDevice{" +
                ", serial='" + serial + '\'' +
                ", deviceName='" + deviceName + '\'' +
                ", productType='" + productType + '\'' +
                ", productVersion='" + productVersion + '\'' +
                ", wifiAddress='" + wifiAddress + '\'' +
                '}';
    }
}
