//
//  Untitled.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/17.
//

import Combine
import Alamofire
enum TSProgressState  {
    case none
    case start
    case pending
    case progress(CGFloat)
    case success(Any?)
    case failed(String)

    static func fromKeyValue(key string: String,value:String) -> TSProgressState {
        if string == "none" {
            return .none
        } else if string == "start" {
            return .start
        } else if string == "pending" {
            return .pending
        }else if string == "progress" {
            var progress = 0.0
            if let doubleValue = Float(value) {
                progress = CGFloat(doubleValue)
            }
            return .progress(progress)
        }else if string == "success" {
            return .success(value)
        }else if string == "failed" {
            return .failed(value)
        }else{
            return .none
        }
    }
    
    var keyValue:(String,String){
        switch self {
        case .none:
            return ("none","")
        case .start:
            return ("start","")
        case .pending:
            return ("pending","")
        case .progress(let float):
            return ("progress",String(Float(float)))
        case .success(let string):
            if let string = string as? String{
                return ("success",string)
            }
            return ("success","")
        case .failed(let string):
            return ("failed",string)
        }
    }
    
    
    var isResult:Bool{
        switch self {
        case .none:
            return false
        case .start:
            return false
        case .pending:
            return false
        case .progress(_):
            return false
        case .success(_):
            return true
        case .failed(_):
            return true
        }
    }
    
    
    var reslutSuccess:Bool{
        switch self {
        case .success(_):
            return true
        case .failed(_):
            return false
        default:
            return true
        }
    }
}

class TSGenmojiGennerateViewModel {

    var creatRequest:Request?
    var queryRequest:Request?
    var stopNetwork = false
    
    @Published var stateDatauPblished:(TSProgressState,TSGenmojiModel?) = (TSProgressState.none,nil)
    var aiText:String = ""
    
    func creatImageEmoji(text:String) {
        stopNetwork = false
        aiText = text
        stateDatauPblished = (.start,nil)
        creatRequest = TSNetworkShared.post(urlType: .imageEmoji,parameters: ["prompt":text]) { [weak self] data,error in
            guard let self = self else { return }
            
            if let dataDict = data as? [String:Any] ,
               dataDict.safeInt(forKey: "code") == 200,
               let actionId = dataDict["actionId"] as? Int{
                if stopNetwork == false {
                    self.getActionInfo(action_id:actionId)
                }
            }else{
                self.stateDatauPblished = (.failed(error?.localizedDescription ?? ""),nil)
            }
        }
    }
    
    func getActionInfo(action_id:Int){
        queryRequest = TSNetworkShared.get(urlType: .actionInfo,parameters: ["action_id":action_id]) { [weak self] data,error in
            guard let self = self else { return }
            if let result = kNetWorkResultSuccess(data: data) {
                if let genmojiModel = TSGenmojiModel(JSON: result) {
                    switch genmojiModel.actionStatus {
                    case .success:
                        TSToastShared.hideLoading()
                        self.stateDatauPblished = (.success(nil),genmojiModel)
                    case .failed:
                        self.stateDatauPblished = (.failed(kNetWorkMessage(data: data) ?? ""),nil)
                    default:
                        if stopNetwork == false {
                            kDelayOnMainThread(1.0) {
                                self.getActionInfo(action_id: action_id)
                            }
                        }
                    }
                }
            }else{
                self.stateDatauPblished = (.failed(error?.localizedDescription ?? ""),nil)
            }
        }
    }
    
    
    func cancelAllRequest(){
        creatRequest?.cancel()
        queryRequest?.cancel()
        stopNetwork = true
    }
    
}


