//
//  TSGenneralPicVM.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/5.
//

import Combine
import Alamofire

let kTextPicW:Int = 800
let kTextPicH:Int = 1440

class TSGenneralPicVM {
    
    var creatRequest:Request?
    var queryRequest:Request?
    var stopNetwork = false
   
    @Published var stateDatauPblished:(TSProgressState,TSActionInfoModel?) = (TSProgressState.none,nil)
    var aiText:String = ""
    var gennerateType:TSGennerateType = .poster
    var generatingProgress = 0
    
    
    
//    func creatImageEmoji(text:String) {
//
//        stateDatauPblished = (.start,nil)
//        stateDatauPblished = (.progressString(generating(progress: 0.0)),nil)
//        
////        kDelayOnMainThread(2.0) {
////            self.stateDatauPblished = (.failed("error?.localizedDescription" ?? ""),nil)
////        }
//
//        kDelayOnMainThread(2.0) {
//            self.stateDatauPblished = (.success(nil),TSActionInfoModel())
//        }
//        
//    }
    
    //width 和 height 必须是 32 的倍数
    func creatImageEmoji(text:String) {
        generatingProgress = 0
        aiText = text
        let postDict:[String : Any] = [
            "prompt":text,
            "width":textPicW,
            "height":textPicH
        ]
        stateDatauPblished = (.start,nil)
        stateDatauPblished = (.progressString(generating(progress: 0.0)),nil)
        creatRequest = TSNetworkShared.post(urlType: .textPicCreate,parameters: postDict) { [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 = TSActionInfoModel(JSON: result) {
                    switch genmojiModel.actionStatus {
                    case .success:
                        TSToastShared.hideLoading()
                        self.stateDatauPblished = (.success(nil),genmojiModel)
                        generatingProgress = 0
                    case .failed:
                        self.stateDatauPblished = (.failed(kNetWorkMessage(data: data) ?? ""),nil)
                        generatingProgress = 0
                    default:
                        stateDatauPblished = (.progressString(generating(progress: genmojiModel.percent)),nil)
                        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
    }
    
    func generating(progress:Float) -> String {

        //Generating 0%-100%
        var progressInt = Int(progress*100)

        if generatingProgress >= progressInt{
            return "Generating \(generatingProgress)%"
        }

        if progressInt > 99 {
            progressInt = 99
        }
        
        generatingProgress = progressInt
        return "Generating \(progressInt)%"
    }
}

extension TSGenneralPicVM {
    
    var textPicW:Int{
        return kTextPicW
    }
    
    var textPicH:Int{
        if gennerateType == .photo {
            return kTextPicW
        }
        return kTextPicH
    }
}

