//
//  CustomDescription.swift
//  Example
//
//  Created by ldc on 2023/12/12.
//

import Foundation
import PrinterSDK
import RYCommunication

extension S1WiFiEncryption: @retroactive CustomStringConvertible {
    
    public var description: String {
        switch self {
        case .open:
            return "OPEN"
        case .wep:
            return "WEP"
        case .wpa2_psk:
            return "WPA2-PSK"
        case .wpa_wpa2_psk:
            return "WPA/WPA2-PSK"
        case .wpa_psk:
            return "WPA-PSK"
        case .wpa:
            return "WPA"
        case .wpa2:
            return "WPA2"
        case .sae:
            return "SAE"
        case .wpa3_wpa2_psk_mix:
            return "WPA2/WPA3-PSK"
        default:
            return "Unknown"
        }
    }
}

extension S1ErrorCode: @retroactive CustomStringConvertible {
    
    public var description: String {
        switch self {
        case .params:
            return "参数错误".localized
        case .udpConfigEthDhcp:
            return "UDP配网以太网DHCP配置失败".localized
        case .udpConfigEthIp:
            return "UDP配网以太网IP配置失败".localized
        case .udpConfigEthMask:
            return "UDP配网以太网掩码配置失败".localized
        case .udpConfigEthGateway:
            return "UDP配网以太网网关配置失败".localized
        case .udpConfigWiFiMode:
            return "UDP配网Wi-Fi模式配置失败".localized
        case .udpConfigEncryption:
            return "UDP配网Wi-Fi加密方式配置失败".localized
        case .udpConfigApName:
            return "UDP配网Wi-Fi AP名字配置失败".localized
        case .udpConfigApPwd:
            return "UDP配网Wi-Fi AP密码配置失败".localized
        case .udpConfigStaName:
            return "UDP配网Wi-Fi STA名字配置失败".localized
        case .udpConfigStaPwd:
            return "UDP配网Wi-Fi STA密码配置失败".localized
        case .udpConfigApIp:
            return "UDP配网Wi-Fi AP IP地址配置失败".localized
        case .udpConfigStaIp:
            return "UDP配网Wi-Fi STA IP地址配置失败".localized
        case .udpConfigWiFiDhcp:
            return "UDP配网Wi-Fi DHCP配置失败".localized
        case .udpConfigWiFiGateway:
            return "UDP配网Wi-Fi网关配置失败".localized
        case .udpConfigWiFiMask:
            return "UDP配网Wi-Fi掩码配置失败".localized
        case .bleTimeout:
            return "Connection timeout".localized
        case .bleDisvocerServiceTimeout:
            return "Connection failed".localized
        case .bleValidateTimeout:
            return "Vertification timeout".localized
        case .bleUnknownDevice:
            return "Unknown device".localized
        case .bleSystem:
            return "System error".localized
        case .bleValidateFail:
            return "Vertification failed".localized
        case .streamTimeout:
            return "流连接超时".localized
        case .streamEmpty:
            return "流初始化失败".localized
        case .streamOccured:
            return "流错误".localized
        case .responseResolve:
            return "回复解析失败".localized
        case .otaGetMaxPackageSize:
            return "OTA升级获取最大包大小".localized
        case .otaRetryTooMuch:
            return "OTA升级重试过多".localized
        case .otaStatusLengthError:
            return "OTA升级长度错误".localized
        case .otaStatusCrcError:
            return "OTA升级CRC错误".localized
        case .otaStatusTimeOut:
            return "OTA升级超时".localized
        case .otaStatusWriteFail:
            return "OTA升级写入失败".localized
        case .otaStatusFail:
            return "OTA升级失败".localized
        case .escPrintFail:
            return "打印失败".localized
        case .escPrintTimeout:
            return "打印超时".localized
        @unknown default:
            return "未知错误: \(rawValue)".localized
        }
    }
}

extension RYSessionManagerError: @retroactive CustomStringConvertible {
    
    public var description: String {
        switch self {
        case .accessoryNotFound:
            return "会话错误: 没找到目标设备"
        case .notConnect:
            return "会话错误: 设备未连接"
        case .timeout:
            return "会话错误: 超时"
        @unknown default:
            return "会话错误: 未知错误"
        }
    }
}

extension String: @retroactive Error {}

func S1ErrorDescription(error: Error) -> String {
    
    if let temp = error as? String {
        return temp
    }
    let nserror = error as NSError
    if let innerError = nserror.userInfo[S1InnerErrorUserInfoKey] as? Error {
        if nserror.domain == S1ErrorDomain, let code = S1ErrorCode(rawValue: nserror.code) {
            return String.init(format: "%@\n%@", code.description, S1ErrorDescription(error: innerError))
        }else if nserror.domain == RYSessionManagerErrorDomain, let code = RYSessionManagerError(rawValue: UInt(nserror.code)) {
            return String.init(format: "%@:%i\n%@", code.description, nserror.code, S1ErrorDescription(error: innerError))
        }else {
            return String.init(format: "%@:%i\n%@", nserror.domain, nserror.code, S1ErrorDescription(error: innerError))
        }
    }else {
        if nserror.domain == S1ErrorDomain, let code = S1ErrorCode(rawValue: nserror.code) {
            return code.description
        }else if nserror.domain == RYSessionManagerErrorDomain, let code = RYSessionManagerError(rawValue: UInt(nserror.code)) {
            return code.description
        }else {
            return nserror.localizedDescription
        }
    }
}
