//
//  TSNetworkTool.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2025/1/2.
//

import Alamofire
import Network

class TSNetworkTool {
    static let shared = TSNetworkTool()
    
    func monitorNetworkPermission(escapable result: @escaping (Bool) -> Void) {
        let monitor = NWPathMonitor()
        let queue = DispatchQueue.global(qos: .background)
        monitor.start(queue: queue)
        monitor.pathUpdateHandler = { path in
            DispatchQueue.main.async {
                if path.status == .satisfied {
                    debugPrint("网络可用，用户同意了权限")
                    result(true)
                } else {
                    debugPrint("网络不可用，可能用户拒绝了权限")
                    result(false)
                }
            }
        }
    }

    func startListenNetStatus(handler: @escaping (NetworkReachabilityManager.NetworkReachabilityStatus, NetworkReachabilityManager?) -> Void) {
        // 创建 NetworkReachabilityManager 实例
        let networkManager = NetworkReachabilityManager.default

        // 开始监听网络状态
        networkManager?.startListening { status in
            handler(status, networkManager)
        }
    }
}
