//
//  Hud.swift

//
//  Created by Leon on 2025/7/13.
//

import Foundation
import UIKit

/// ⚠️只在不确认View主体时使用(eg: IAPCore)，其他情况请使用Hud.showIndicator() and view.hideToastActivity
class Hud {
    static let shared = Hud()

    static func showIndicator(_ blockUserTap: Bool? = true) {
        DispatchQueue.main.async {
            if let view = UIWindow.key {
                view.makeToastActivity(.center, blockUserInteraction: blockUserTap ?? true)
            } else if let view = UIWindow.getTopViewController()?.view {
                view.makeToastActivity(.center, blockUserInteraction: blockUserTap ?? true)
            }
        }
    }
    
    /// 显示加载指示器，可选择是否阻止用户交互
    /// - Parameter blockUserInteraction: 是否阻止用户与页面上其他元素的交互，默认为false
//    static func showIndicator(blockUserInteraction: Bool = true) {
//        DispatchQueue.main.async {
//            if let view = UIWindow.key {
//                view.makeToastActivity(.center, blockUserInteraction: blockUserInteraction)
//            } else if let view = UIWindow.getTopViewController()?.view {
//                view.makeToastActivity(.center, blockUserInteraction: blockUserInteraction)
//            }
//        }
//    }

    static func hideIndicator() {
        DispatchQueue.main.async {
            if let view = UIWindow.key {
                view.hideToastActivity()
            } else if let view = UIWindow.getTopViewController()?.view {
                view.hideToastActivity()
            }
        }
    }

    static func toast(str: String?, completion: ((Bool)-> Void)? = nil) {
        DispatchQueue.main.async {
            guard let toast = str, let view = UIWindow.applicationKey else {
                return
            }
            view.makeToast(toast, completion: completion)
        }
    }
    
    static func hideToast(){
        DispatchQueue.main.async {
            guard let view = UIWindow.applicationKey else {
                return
            }
            view.hideToast()
        }
    }
    
    static func comingsoon(){
        Hud.toast(str: "Comming soon...")
    }

    required init() {
        //ToastManager.shared.duration = 1.0
        
        ToastManager.shared.isQueueEnabled
    }
}
