//
//  KeyboardNetworkManager.swift
//  AiKeyboard
//
//  Created by Destiny on 2025/4/28.
//

import Moya
import Moya_ObjectMapper

let KeyboardNetworkProvider = MoyaProvider<KeyboardClientService>(plugins: [networkLoggerPlugin])
let networkLoggerPlugin = NetworkLoggerPlugin()

enum KeyboardClientService {
    
    case character_list(params: [String: Any])
    case keyboard_list(params: [String: Any])
    case prologue_list(params: [String: Any])
    case keyboard_choose(params: [String: Any])
    case chat_super_reply(params: [String: Any])
    case chat_super_speak(params: [String: Any])
    case chat_super_prologue(params: [String: Any])
}

extension KeyboardClientService: TargetType {
    
    var baseURL: URL {
        let baseUrlStr = KeyboardApi.getBaseUrl()
        return URL(string: baseUrlStr)!
    }
    
    var path: String {
        switch self {
        case .character_list: return KeyboardApi.character_list
        case .keyboard_list: return KeyboardApi.keyboard_list
        case .prologue_list: return KeyboardApi.prologue_list
        case .keyboard_choose: return KeyboardApi.keyboard_choose
        case .chat_super_reply: return KeyboardApi.chat_super_reply
        case .chat_super_speak: return KeyboardApi.chat_super_speak
        case .chat_super_prologue: return KeyboardApi.chat_super_prologue
        }
    }
    
    var method: Moya.Method {
        return .post
    }
    
    var sampleData: Data {
        return "".data(using: String.Encoding.utf8)!
    }
    
    var task: Moya.Task {
        var parameters: [String: Any] = KeyboardApi.params
        switch self {
        case .character_list(let params):
            parameters.merge(params) { (current, _) in current }
        case .keyboard_list(let params):
            parameters.merge(params) { (current, _) in current }
        case .prologue_list(let params):
            parameters.merge(params) { (current, _) in current }
        case .keyboard_choose(let params):
            parameters.merge(params) { (current, _) in current }
        case .chat_super_reply(let params):
            parameters.merge(params) { (current, _) in current }
        case .chat_super_speak(let params):
            parameters.merge(params) { (current, _) in current }
        case .chat_super_prologue(let params):
            parameters.merge(params) { (current, _) in current }
        }
        debugPrint(parameters)
        return Task.requestParameters(parameters:parameters, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        return ["Content-type": "application/json"]
    }
}

struct KeyboardNetworkManager {
    
    typealias completeCallback = ((Response) -> (Void))
    typealias failCallback = ((Int, String) -> (Void))
    
    static func request(_ target: KeyboardClientService, success: @escaping completeCallback, fail: @escaping failCallback){
        
        
        KeyboardNetworkProvider.request(target) { result in
            //隐藏hud
            switch result {
            case let .success(response):
                
                if response.statusCode == 200 {

                    if let responseData = try? response.mapObject(KeyboardBaseResponse.self){
                        if responseData.code == 0 {
                            success(response)
                        } else {
                            fail(responseData.code, responseData.msg ?? "")
                        }
                    } else {
                        fail(500, "解析失败")
                    }
                
                } else {
                    
//                    fail(response.statusCode, response.toJSON(modelKey: "msg").stringValue)
                }
                 
                #if DEBUG
                if let json = try? response.mapJSON() {
                    print("data:\(json)")
                }
                #endif
                    
                break
            case let .failure(error):
                fail(error.errorCode, "网络错误，请检查后重试")
                //网络连接失败，提示用户
//                UIApplication.keyWindow?.toast(text: "网络错误，请检查后重试")
                break
            }
        }
    }
}
