//
//  NetworkManager.swift
//  wallpaper_project


import Foundation
import Alamofire

class NetworkManager {
    static let shared = NetworkManager()
    
    private init() {}
    
    /// 封装的POST请求
    /// - Parameters:
    ///   - url: 请求的URL字符串
    ///   - parameters: 请求参数
    ///   - headers: 请求头
    ///   - completion: 请求完成的回调
    func postRequest(url: String, parameters: [String: Any]?, headers: HTTPHeaders?, completion: @escaping (Result<Any, AFError>) -> Void) {
        AF.request(url,
                   method: .post,
                   parameters: parameters,
                   encoding: JSONEncoding.default,
                   headers: headers)
            .validate()
            .responseJSON { response in
                switch response.result {
                case .success(let data):
                    completion(.success(data))
                case .failure(let error):
                    completion(.failure(error))
                }
            }
    }
}

func setPostSHOW(){
    let url = "http://192.168.40.8:8080/top_selection/save_ad_show_log"
    let parameters:[String : Any] = [
        "deviceId": StartManager.shared.devicID ?? "",
        "localIp": StartManager.shared.localIP ?? "",
        "remoteIp": StartManager.shared.publicIP ?? "",
        "linkId": "",
        "packageName": "com.wallpapaer.hd.live.app",
        "gaid": StartManager.shared.idfaid ?? "",
        "dataId": "",
        "shelfNumber":StartManager.shared.shelfNumber,
        "succeed": StartManager.shared.succeed,
        "loadTime":StartManager.shared.loadTime ?? "",
        "adPlatform": "TopOn",
        "countryCode": StartManager.shared.countryCode ?? "",
        "adId": StartManager.shared.adId ?? "",
        "platformResponseTime": StartManager.shared.platformResponseTime ?? "",
        "getIpResponseTime": StartManager.shared.getIpResponseTime ?? "",
        "ecpm": StartManager.shared.ecpm ?? "",
        "dsp": StartManager.shared.dsp ?? "",
        "network": StartManager.shared.network ?? "",
    ]
    
    let headers: HTTPHeaders = [
        "Content-Type": "application/json"
    ]
    
    NetworkManager.shared.postRequest(url: url, parameters: parameters, headers: headers) { result in
        switch result {
        case .success(let data):
            print("Response:--- \(data)")
        case .failure(let error):
            print("Error:--- \(error)")
        }
    }
    
}
func setPostload(){
    let url = "http://192.168.40.8:8080/top_selection/save_ad_load_log"
    let parameters:[String : Any] = [
        "deviceId": StartManager.shared.devicID ?? "",
        "localIp": StartManager.shared.localIP ?? "",
        "remoteIp": StartManager.shared.publicIP ?? "",
        "linkId": "",
        "packageName": "com.wallpapaer.hd.live.app",
        "gaid": StartManager.shared.idfaid ?? "",
        "dataId": "",
        "shelfNumber":StartManager.shared.shelfNumber ?? "",
        "succeed": StartManager.shared.succeed,
        "loadTime":StartManager.shared.loadTime ?? "",
        "adPlatform": "TopOn",
        "countryCode": StartManager.shared.countryCode ?? "",
        "adId": StartManager.shared.adId ?? "",
        "platformResponseTime": StartManager.shared.platformResponseTime ?? "",
        "getIpResponseTime": StartManager.shared.getIpResponseTime ?? "",
        "ecpm": StartManager.shared.ecpm ?? "",
        "dsp": StartManager.shared.dsp ?? "",
        "network": StartManager.shared.network ?? "",
        "showStatus": 0,
        "washParam": true,
    ]
    
    
    let headers: HTTPHeaders = [
        "Content-Type": "application/json"
    ]
    
    NetworkManager.shared.postRequest(url: url, parameters: parameters, headers: headers) { result in
        switch result {
        case .success(let data):
            print("Response:--- \(data)")
        case .failure(let error):
            print("Error:--- \(error)")
        }
    }
    
}
