//
//  UserApi.swift

//
//  Created by Leon on 2025/7/28.
//

import Moya

//let UserProvider = MoyaProvider<UserAPI>(requestClosure: myRequestClosure)

let UserProvider = APIConfig.useMock && UserAPI.useMock
? MoyaProvider<UserAPI>(endpointClosure: myEndpointClosure, stubClosure: { target in
    let data = target.sampleData
    if(data.count > 0){
        return .delayed(seconds: 0.5)
    }else{
        return .never
    }
})
: MoyaProvider<UserAPI>(requestClosure: myRequestClosure)

enum UserAPI {
    static let useMock: Bool = false
    
    case userInfoSelfGet
    case userInfoGet(userId: NSInteger)
    case userSignout
    case userDel //(checkCode: String = "", messageMediaTypeEnum: RegisterType = .MOBILE)
    case userInfoEdit(params: [String: Any])
    case userNicknameCheck(nickname:String, exUserId: Int?)
    
    // MARK: 次数
    /// 获取用户各种创作次数
    case getUserCreateCount
    /// 购买创作次数
    case buyUserCreateCount(count: Int)
}

extension UserAPI: TargetType {
    var baseURL: URL {
        return URL(string: APIConfig.frog)!
    }
    
    var path: String {
        switch self {
        case .userInfoSelfGet:
            return "/web/user/base-info"
        case .userInfoGet:
            return "/web/user/base-info"
        case .userSignout:
            return "/web/user/logout"
        case .userDel:
            return "/web/user/del"
        case .userInfoEdit:
            return "/web/user/edit-user-info"
        case .userNicknameCheck:
            return "/web/user/nickname-check"
        case .getUserCreateCount:
            return "/web/user/get-user-create-count"
        case .buyUserCreateCount:
            return "/web/ai/buy-create-image-count"
        }
    }
    
    var method: Moya.Method {
        return .post
    }
    
    var task: Task {
        var mParams = [String: Any]()
        
        switch self {
        case .userInfoSelfGet:
            return .requestPlain
        case let .userInfoGet(userId):
            mParams = ["userId": userId]
        case .userSignout:
            return .requestPlain
        case .userDel: // let .userDel(checkCode, messageMediaTypeEnum):
//            mParams = ["messageMediaTypeEnum": messageMediaTypeEnum.rawValue]
//            if checkCode.count > 0 {
//                mParams.updateValue(checkCode, forKey: "checkCode")
//            }
            return .requestPlain
        case let .userInfoEdit(params):
            mParams = params
        case let .userNicknameCheck(nickname, exUserId):
            mParams.updateValue(nickname, forKey: "nickname")
            if let userid = exUserId{
                mParams.updateValue(userid, forKey: "exUserId")
            }
        case .getUserCreateCount:
            break
        case .buyUserCreateCount(let count):
            mParams.updateValue(count, forKey: "count")
        }
        
        
        mParams.updateValue("IOS", forKey: "appClient")
        mParams.updateValue(UIDevice.UUID, forKey: "deviceCode")
        //        if let code = HCaptchaHandler.shared.reqValidateCodeForLoginAPI {
        //            mParams.updateValue(code, forKey: "reqValidateCode")
        //        }
        
        return .requestParameters(parameters: mParams, encoding: JSONEncoding.default)
    }
    
    var headers: [String: String]? {
        return APIConfig.apiHeaders()
    }
    
    var sampleData: Data {
        switch self {
        case .userInfoSelfGet:
            let content = """
                  {
                      "idCard": "52163422534", 
                      "headImage": "https://hhb.crushlevel.ai/dev/main/headImage/1952268491211472898/17545384837179865.jpeg", 
                      "thirdNickname": "woolni555", 
                      "nickname": "Rrrrrrrrrerr", 
                      "cpUserInfo": true, 
                      "thirdEmail": null, 
                      "birthday": 1074006749000, 
                      "thirdType": "DISCORD", 
                      "userId": 1952268491211473000, 
                      "sex": 1
                  }
                """
            return .okContent(string: content)
        case .userInfoGet:
            return .okData()
        case .userSignout:
            return .okData()
        case .userDel:
            return .okData()
        case .userInfoEdit:
            return .okData()
        default:
            return Data()
        }
    }
}

