//
//  CommonApi.swift

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

import Moya

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

enum CommonAPI {
    static let useMock: Bool = false
    
    case getAIDict
    case getGiftDict(params: [String: Any])
    case getChatModelDict
    /// 聊天气泡字典列表
    case chatBubbleDict(aiId :Int)
}

extension CommonAPI: TargetType {
    var baseURL: URL {
        return URL(string: APIConfig.frog)!
    }

    var path: String {
        switch self {
        case .getAIDict:
            return "/web/get-ai-dict"
        case .getGiftDict:
            return "/web/gift/dict-list"
        case .getChatModelDict:
            return "/web/chat-model/dict-list"
        case .chatBubbleDict:
            return "/web/chat-set/get-chat-bubble-list"
        }
    }

    var method: Moya.Method {
        return .post
    }

    var task: Task {
        var mParams = [String: Any]()
        
        
        switch self {
        case .getAIDict:
            break
        case .getGiftDict(let params):
            mParams = params
        case .getChatModelDict:
            break
        case .chatBubbleDict(let aiId):
            mParams.updateValue(aiId, forKey: "aiId")
        }

        mParams.updateValue("IOS", forKey: "appClient")
        mParams.updateValue(UIDevice.UUID, forKey: "deviceCode")

        return .requestParameters(parameters: mParams, encoding: JSONEncoding.default)
    }

    var headers: [String: String]? {
        return APIConfig.apiHeaders()
    }

    var sampleData: Data {
        switch self {
        case .getAIDict:
            let mockJSON = """
            {
              "content": {
                "roleDictList": [
                  {
                    "code": "Original",
                    "name": "原创",
                    "childDictList": [
                      {
                        "code": "Original",
                        "name": "原创",
                        "childDictList": []
                      }
                    ]
                  },
                  {
                    "code": "Fanfic",
                    "name": "同人",
                    "childDictList": [
                      {
                        "code": "Animation",
                        "name": "动漫",
                        "childDictList": []
                      },
                      {
                        "code": "Game",
                        "name": "游戏",
                        "childDictList": []
                      },
                      {
                        "code": "TV",
                        "name": "影视",
                        "childDictList": []
                      }
                    ]
                  }
                ],
                "characterDictList": [
                  {
                    "code": "性格",
                    "name": "性格",
                    "childDictList": [
                      {
                        "code": "",
                        "name": "",
                        "childDictList": []
                      }
                    ]
                  }
                ],
                "tagDictList": [
                  {
                    "code": "",
                    "name": "",
                    "childDictList": [
                      {
                        "code": "",
                        "name": "",
                        "childDictList": []
                      }
                    ]
                  }
                ],
                "imageStyleDictList": [
                  {
                    "code": "",
                    "name": "",
                    "childDictList": [
                      {
                        "code": "12344345",
                        "name": "XXXX",
                        "childDictList": []
                      },
                      {
                        "code": "24234234",
                        "name": "YYYY",
                        "childDictList": []
                      }
                    ]
                  }
                ],
                "imageStylePicList": [
                  {
                    "id": 10,
                    "dictCode": "1",
                    "url": "https://fastly.picsum.photos/id/449/200/200.jpg?hmac=FD7uqDWwU1CeTIaCQGi9nY0XGVRtSV7cnadWYqPt0CU",
                    "sort": 0,
                    "isDelete": 0,
                    "createTime": ""
                  },
                  {
                    "id": 20,
                    "dictCode": "2",
                    "url": "https://fastly.picsum.photos/id/241/200/200.jpg?hmac=F-mDYyK1xUiXjlLd5PvE8EQpTwGfFndV7mMaANDtl28",
                    "sort": 0,
                    "isDelete": 0,
                    "createTime": ""
                  }
                ],
                "timbreDictList": [
                  {
                    "id": 0,
                    "type": 0,
                    "name": "",
                    "description": "",
                    "url": "",
                    "isDelete": 0,
                    "createTime": ""
                  }
                ]
              },
              "status": "OK",
              "errorCode": "",
              "errorMsg": "",
              "traceId": ""
            }
            """
            return Data(mockJSON.utf8)
        default:
            return Data()
        }
    }
}
