//
//  NetworkReachibility.swift
//  WOKA
//
//  Created by MacBook Pro on 06/05/24.
//

import Alamofire

class NetworkReachibility{
    
    static let shared = NetworkReachibility()
    private init(){}
    let manager = NetworkReachabilityManager(host: "www.apple.com")
    var isMonitoring = false
    fileprivate var isInternetReachable = false
    
    func startMonitoring(queue : DispatchQueue? = .main,onCompletion : @escaping (Bool) -> Void) {
        manager?.startListening(onQueue: queue!, onUpdatePerforming: { (status) in
            self.isMonitoring = true
            switch status {
            case .notReachable:
                print("network connection status - lost")
                self.isInternetReachable = false
                onCompletion(false)
            case .reachable(.ethernetOrWiFi):
                print("network connection status - ethernet/WiFI")
                self.isInternetReachable = true
                onCompletion(true)
            case .reachable(.cellular):
                print("network connection status - cellular")
                self.isInternetReachable = true
                onCompletion(true)
            default:
                self.isInternetReachable = false
                onCompletion(false)
                break
            }
        })
    }
    
    func stopMonitoring(){
        self.isMonitoring = false
        manager?.stopListening()
    }
}
