//
//  TSNetWork+Business.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/16.
//

/// 基础 URL（根据需求修改）
private let baseURL = "http://ai.100yearslater.com"
private let baseChinaURL = "http://ai.100yearslater100.com"
import Alamofire
enum TSNeURLType:String {

    case actionInfo = "/api/action/info"         //查询生成过程接口
    case upload = "/api/upload"                  //上传图片
    case imageRewrite = "/api/image/rewrite"     //图生图
//
    case config = "/api/ops/old-photo-config"       //App配置
    case createVideo = "/api/video/create"               //视频生成
    case imageInpaint = "/api/image/inpaint"             //图片涂抹



    
    func getUrlString() -> String {
        if Locale.current.identifier.contains("_CN") {//中国区
            return baseChinaURL + self.rawValue
        }else{
            return baseURL + self.rawValue
        }
    }
    
    var needValidate : Bool {
        return validateURLTypeList.contains(self)
    }
    
    ///需要进行次数验证的接口
    var validateURLTypeList : [TSNeURLType] {
        [
            .upload,
            .imageRewrite,
            .createVideo,
            .imageInpaint,
        ]
    }
}



//let kGenerateFailed:String = "Failed to generate, please try later".localized
let kGenerateFailed:String = "Sorry there was a slight problem with the image processing, please try again later.".localized

enum TSNetWorkCode : Int {
    case success = 200
    case fail = 0   //通用错误
    case textSensitive = -10003    //文生图敏感错误
    case imageSensitive = -10004   //图生图敏感错误
    case networkError = -1005   //网络错误
    case generateTooMuch = -200   //单日生成次数过多
    
    var errorMsg:String {
        switch self {
        case .textSensitive,.imageSensitive:
            return "Your photo may contain copyright infringement, nudity, gore or violence that does not comply with the Health Policy, please replace the photo and try again.".localized
        case .networkError:
            return "No network, please check your network and try again.".localized
        case .generateTooMuch:
            return "We've detected unusually high generation activity. You may be a bot. Please try again tomorrow.".localized
        default:
            return "Sorry there was a slight problem with the image processing, please try again later.".localized
        }
        
    }
    
    //获取生成错误 code 对应的文案
    static func errorMsg(code:Int)->String{
        let netCode = TSNetWorkCode(rawValue: code) ?? .fail
        return netCode.errorMsg
    }
    
    //敏感错误
    static func sensitiveError(code:Int)->Bool{
        let netCode = TSNetWorkCode(rawValue: code)
        switch netCode {
        case .textSensitive,.imageSensitive:
            return true
        default:
            return false
        }
    }
    
    //网络错误错误
    static func networkError(code:Int)->Bool{
        let netCode = TSNetWorkCode(rawValue: code)
        switch netCode {
        case .networkError:
            return true
        default:
            return false
        }
    }
    
    //网络错误错误
    static func getErrorCode(_ error: Error) -> Int {
        if let urlError = error as? URLError {
            switch urlError.code {
            case .notConnectedToInternet, .networkConnectionLost:
                return TSNetWorkCode.networkError.rawValue
            default:
                return urlError.code.rawValue
            }
        }
        return 0
    }
    //获取生成错误 code 对应的文案
    static func getGeneratorStyle(code:Int)->TSGeneratorView.Style{
        let netCode = TSNetWorkCode(rawValue: code)
        switch netCode {
        case .textSensitive,.imageSensitive:
            return TSGeneratorView.Style.sensitiveError
        case .networkError:
            return TSGeneratorView.Style.netWorkError
//        case .generateTooMuch:
//            return TSGeneratorView.Style.generateTooMuch
        default:
            return TSGeneratorView.Style.generalError
        }
    }
}


extension Error {
    
    var tsCode:Int {
        if let error = self as? AFError, let underlyingError = error.underlyingError as? URLError {
            switch underlyingError.code {
            case .notConnectedToInternet, .networkConnectionLost,.timedOut:
                return TSNetWorkCode.networkError.rawValue
            default:
                return underlyingError.code.rawValue
            }
        }else
        if let urlError = self as? URLError {
            switch urlError.code {
            case .notConnectedToInternet, .networkConnectionLost,.timedOut:
                return TSNetWorkCode.networkError.rawValue
            default:
                return urlError.code.rawValue
            }
        }else
        if let error = self as? NSError {
            return error.code
        }
        return 0
    }
    
    var tsDesc:String {
        return TSNetWorkCode.errorMsg(code: tsCode)
    }
}

func getUserInfoJsonString()->[String:Any] {
    let uuid: String
    let uuidUdKey = "my_UUID"
    if let saved = UserDefaults.standard.string(forKey: uuidUdKey),
       !saved.isEmpty {
        uuid = saved
    } else {
        let newUuid = UUID().uuidString
        UserDefaults.standard.set(newUuid, forKey: uuidUdKey)
        UserDefaults.standard.synchronize()
        uuid = newUuid
    }
    
    let dic:[String:Any] = [
        "device":UIDevice.current.modelName,
        "deviceId":uuid,
        "iosVersion":UIDevice.current.systemVersion,
        "appVersion":appShortVersion(),
        "subscriptionStatus":kPurchaseBusiness.isVip ? "active" : "fallow",
    ]

    return dic
}


func getLanguageCode()->String{
    if Locale.current.identifier.contains("zh-Hant"){
        return "zh-Hant"
    }
    if let languageCode = Locale.current.languageCode {
//        print("当前设备语言简写: \(languageCode)") // 输出如 "en", "ja", "zh" 等
        return languageCode
    }
    return "en"
}


extension TSNetworkManager {
    
    /// 通用 get 请求
    func get<T: TSBaseModel>(
        urlType: TSNeURLType,
        parameters: [String: Any]? = nil,
        responseType: T.Type? = nil,
        completion: @escaping (Result<Any, Error>) -> Void
    ) -> Request  {
        let urlString = urlType.getUrlString()
        return request(method: .get, urlString: urlString, parameters:parameters) { result in
            completion(result)
        }
    }


    /// 通用 POST 请求
    /// - Parameters:
    ///   - endpoint: 接口路径
    ///   - parameters: 请求参数
    ///   - responseType: 响应数据模型（可选）
    ///   - completion: 请求完成的回调
    func post<T: TSBaseModel>(
        urlType: TSNeURLType,
        parameters: [String: Any]? = nil,
        responseType: T.Type? = nil,
        completion: @escaping (Result<Any, Error>) -> Void
    ) -> Request? {
        ///需要校验。且需要判断是否超过最大次数
//        if urlType.needValidate,kPurchaseBusiness.isOverTotalTimes {
//            completion(.failure(NSError(domain: "", code: TSNetWorkCode.generateTooMuch.rawValue)))
//            return nil
//        }
        
        let urlString = urlType.getUrlString()
        return request(method: .post, urlString: urlString, parameters:parameters) { result in
            completion(result)
        }
    }
    
    /// 通用 POST Stream 请求
    /// - Parameters:
    ///   - endpoint: 接口路径
    ///   - parameters: 请求参数
    ///   - responseType: 响应数据模型（可选）
    ///   - completion: 请求完成的回调
    func postStream<T: TSBaseModel>(
        urlType: TSNeURLType,
        parameters: [String: Any]? = nil,
        responseType: T.Type? = nil,
        streamHandler:@escaping (String) -> Void,
        completion: @escaping (Result<Any, Error>) -> Void
    ) -> Request?{
        let urlString = urlType.getUrlString()
        let streamRequest = postStreamRequest(urlString: urlString, parameters: parameters, streamHandler: streamHandler, completion: completion)
        return streamRequest
    }

    
    /// 上传多个 Data 数据
    /// - Parameters:
    ///   - urlType: TSNeURLType
    ///   - dataArray: Data 数组，每个元素是一个字典，包含 Data 和字段名
    ///   - parameters: 其他参数（可选）
    ///   - headers: 自定义请求头（可选）
    ///   - completion: 完成回调，返回结果或错误
    func uploadData<T: TSBaseModel>(
        urlType: TSNeURLType,
        dataArray: [[String: Any]], // Data 数组，每个元素包含 Data 和字段名
        parameters: [String: Any]? = nil,
        responseType: T.Type? = nil,
        progressHandler: @escaping (Float) -> Void, // 上传进度回调
        completion: @escaping (Result<Any, Error>) -> Void
    ) -> Request?{
        let urlString = urlType.getUrlString()
        let request = uploadData(urlString: urlString,dataArray:dataArray, parameters: parameters, progressHandler: { progress in
            progressHandler(Float(progress.fractionCompleted))
        },completion: completion)
        return request
    }
    
    func downloadFile(
        urlString: String,
        to destination: URL,
        progressHandler: ((Double) -> Void)? = nil,
        completion: @escaping (URL?, Error?) -> Void
    ) -> DownloadRequest? {
        let request = self.downloadFile(
            urlString: urlString,
            to: destination,
            progressHandler: { progress in
                print("下载进度: \(progress * 100)%")
                progressHandler?(progress)
            },
            completion: { result in
                switch result {
                case .success(let fileURL):
                    dePrint("下载完成，文件保存在: \(fileURL.path)")
                    completion(fileURL,nil)
                case .failure(let error):
                    dePrint("下载失败: \(error.localizedDescription)")
                    completion(nil,error)
                }
            }
        )
        return request
    }

}

extension TSNetworkManager {
    
    func uploadImage(
        upLoadImage:UIImage,
        maxKb:Int,
        progressHandler: @escaping (Float) -> Void, // 上传进度回调
        completion: @escaping (Any?, Error?) -> Void)
    -> Request?{
        
        ///需要校验。且需要判断是否超过最大次数
//        if kPurchaseBusiness.isOverTotalTimes {
//            completion(nil,NSError(domain: "", code: TSNetWorkCode.generateTooMuch.rawValue))
//            return nil
//        }
        
        guard let imageData = TSImageCompress.compressImageToTargetSize(upLoadImage, targetSizeKB: maxKb, preserveTransparency: false) else {
            completion(nil,NSError(domain: "image nil", code: 0))
            return nil
        }
        
        var dataArray:[[String:Any]] = []
        if upLoadImage.hasAlphaChannel(){
            dataArray = [
                ["data": imageData,
                 "fieldName": "file", // 字段名
                 "fileName": "image.png", // 文件名
                 "mimeType": "image/png" // MIME 类型
                ]
            ]
        }else {
            dataArray = [
                ["data": imageData,
                 "fieldName": "file", // 字段名
                 "fileName": "image.jpeg", // 文件名
                 "mimeType": "image/jpeg" // MIME 类型
                ]
            ]
        }

        return TSNetworkShared.uploadData(
            urlType: .upload,
            dataArray: dataArray,
            progressHandler: { progress in
                progressHandler(progress)
            },completion: { [weak self] result in
            guard let self = self else { return }
            switch result {
            case .success(let data):
                if let dataDict = kNetWorkCodeSuccess(data: data),
                   let picUrl = dataDict["result"] as? String{
                    completion(picUrl,nil)
                }else{
                    let error = NSError(domain: "Service exception", code: 0)
                    completion(nil,error)
                }
            case .failure(let error):
                completion(nil,error)
            }
        })
    }
    
}

