//
//  DDAF.swift
//  DDNetworkingOfAlamofireKit_Private
//
//  Created by 中道 on 2023/8/2.
//

import Foundation
import Alamofire
import RxSwift
import DDLogKit_Private

public let DDAF = DDAlamofire.default

extension DDAlamofire {
    public struct DDParameters : Encodable {
        public init() {}
    }
    
    // MARK: example
    public func get<T:Decodable,P:Encodable>(urlString:String,parameters:P? = DDParameters(),encoding:ParameterEncoder = URLEncodedFormParameterEncoder.default,headers:HTTPHeaders? = nil,responseType:T.Type = T.self) -> Single<T?> {
        return Single<T?>.create {[weak self] single in
            self?.request(urlString: urlString, method: .get,parameters: parameters,encoding: encoding,headers: headers,responseType: responseType,completionHandler: {[weak self] response in
                self?.logInfo(parameters: parameters, response: response)
                single(.success(response.value))
            })
            return Disposables.create()
        }
    }
    
    public func post<T:Decodable,P:Encodable>(urlString:String,parameters:P? = DDParameters(),encoding:ParameterEncoder = URLEncodedFormParameterEncoder.default,headers:HTTPHeaders? = nil,responseType:T.Type = T.self) -> Single<T?> {
        return Single<T?>.create {[weak self] single in
            self?.request(urlString: urlString, method: .post,parameters: parameters,encoding: encoding,headers: headers,responseType: responseType,completionHandler: {[weak self] response in
                self?.logInfo(parameters: parameters, response: response)
                single(.success(response.value))
            })
            return Disposables.create()
        }
    }
    
    public func get<T:Decodable>(urlString:String,parameters:[String:Any]? = nil,encoding:ParameterEncoding = URLEncoding.default,headers:HTTPHeaders? = nil,responseType:T.Type = T.self) -> Single<T?> {
        return Single<T?>.create {[weak self] single in
            self?.request(urlString: urlString, method: .get,parameters: parameters,encoding: encoding,headers: headers,responseType: responseType,completionHandler: {[weak self] response in
                self?.logInfo(parameters: parameters, response: response)
                single(.success(response.value))
            })
            return Disposables.create()
        }
    }
    
    public func post<T:Decodable>(urlString:String,parameters:[String:Any]? = nil,encoding:ParameterEncoding = URLEncoding.default,headers:HTTPHeaders? = nil,responseType:T.Type = T.self) -> Single<T?> {
        return Single<T?>.create {[weak self] single in
            self?.request(urlString: urlString, method: .post,parameters: parameters,encoding: encoding,headers: headers,responseType: responseType,completionHandler: {[weak self] response in
                self?.logInfo(parameters: parameters, response: response)
                single(.success(response.value))
            })
            return Disposables.create()
        }
    }
    
    public func upload<T:Decodable>(urlString:String,headers:HTTPHeaders? = nil,responseType:T.Type = T.self,multipartFormData: @escaping (MultipartFormData) -> Void,uploadProgress: @escaping (Progress) -> Void) -> Single<T?> {
        return Single<T?>.create {[weak self] single in
            self?.upload(urlString: urlString,method: .post,headers: headers,responseType: responseType,multipartFormData: multipartFormData,uploadProgress: uploadProgress,completionHandler: { response in
                self?.logInfo(parameters: nil, response: response)
                single(.success(response.value))
            })
            return Disposables.create()
        }
    }
    
    private func logInfo<T:Decodable,P:Encodable>(parameters:P?,response:AFDataResponse<T>) {
        DDLog(message: (parameters.debugDescription+"\n"+response.debugDescription))
    }
    private func logInfo<T:Decodable>(parameters:[String:Any]?,response:AFDataResponse<T>) {
        DDLog(message: (parameters.debugDescription+"\n"+response.debugDescription))
    }
}

