//
//  TSNetworkManager+Loading.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/16.
//
import Alamofire
extension TSNetworkManager {
    
    /// 通用 get 请求
    func get<T: TSBaseModel>(
        urlType: TSNeURLType,
        parameters: [String: Any]? = nil,
        responseType: T.Type? = nil,
        animationView:UIView? = nil,
        useCache: Bool = false,
        completion: @escaping (Any?, Error?) -> Void
    ) -> Request? {
        
        
        let loadingLogic = kCreateLoadingLogic {
            guard let view = animationView else { return }
            TSToastShared.showLoading(containerView: view)
        } hideBlock: {
            TSToastShared.hideLoading()
        }

//        TSLoadingAnimation.showLoading(in: animationView)
        loadingLogic.show()

        return get(urlType: urlType, parameters:parameters,responseType:responseType,useCache: useCache) { result in
            loadingLogic.hide()
//            TSLoadingAnimation.hideLoading()
            
            switch result {
            case .success(let data):
//                print("Response data: \(data)")
                completion(data,nil)
            case .failure(let error):
//                print("Error: \(error)")
                completion(nil,error)
            }
        }
    }


    /// 通用 POST 请求
    /// - Parameters:
    ///   - endpoint: 接口路径
    ///   - parameters: 请求参数
    ///   - responseType: 响应数据模型（可选）
    ///   - completion: 请求完成的回调
    func post<T: TSBaseModel>(
        urlType: TSNeURLType,
        parameters: [String: Any]? = nil,
        responseType: T.Type? = nil,
        animationView:UIView? = nil,
        useCache: Bool = false,
        completion: @escaping (Any?, Error?) -> Void
    ) -> Request? {
        
        let loadingLogic = kCreateLoadingLogic {
            guard let view = animationView else { return }
            TSToastShared.showLoading(containerView: view)
        } hideBlock: {
            TSToastShared.hideLoading()
        }
        loadingLogic.show()
//        TSLoadingAnimation.showLoading(in: animationView)
        return post(urlType: urlType, parameters:parameters,responseType:responseType,useCache: useCache){ result in
            loadingLogic.hide()
//            TSLoadingAnimation.hideLoading()
    
            switch result {
            case .success(let data):
//                print("Response data: \(data)")
                completion(data,nil)
            case .failure(let error):
//                print("Error: \(error)")
                completion(nil,error)
            }
        }
    
    }
    
}
