//
//  TSProgressState.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2025/6/11.
//


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

    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 == "progressString" {
            return .progressString(value)
        }else if string == "success" {
            return .success(value)
        }else if string == "failed" {
            return .failed(value,0)
        }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 .progressString(let string):
            return ("progressString",string)
        case .success(let string):
            if let string = string as? String{
                return ("success",string)
            }
            return ("success","")
        case .failed(let string,let code):
            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 .progressString(_):
            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 false
        }
    }
    
    
    var reloadNewData:Bool{
        switch self {
        case .pending,.success(_),.failed(_,_):
            return true
        default:
            return false
        }
    }
    
    static func getFailed(_ error:String,_ code:Int = 0)->TSProgressState {
        return .failed(error, code)
    }
    
    static var generalNormalFailed:TSProgressState {
        return .failed(TSNetWorkCode.fail.errorMsg, TSNetWorkCode.fail.rawValue)
    }
}
