//
//   GMFGPublicInternetAccessHandler.swift
//  MVM5G
//
//  Created by Jason Beck on 5/27/20.
//  Copyright © 2020 Kyle. All rights reserved.
//
import CoreBluetooth

protocol GMFGPublicInternetAccessDelegate: AnyObject {
    func publicInternetAccessStatus(status: GMPublicInternetAccessStatus)
}

enum GMPublicInternetAccessStatus: String {
    case enabled = "Enabled"
    case disabled = "Disabled"
    case notReady = "NotReady"
}

class GMFGPublicInternetAccessHandler: GMFGBLEHandlerProtocol {
    
    private let multicast = MulticastDelegate<GMFGPublicInternetAccessDelegate>()
    
    public func setDelegate(delegates: [GMFGPublicInternetAccessDelegate?]) {
        self.multicast.add(delegates: delegates)
    }
    
    public func reset() {
        self.multicast.removeAll()
    }
    
    public func getPublicInternetAccess(_ peripheral: CBPeripheral, _ characteristic: CBCharacteristic) {
       peripheral.readValue(for: characteristic)
    }
    
    public func response(response: [String : Any]) {
        guard let statusString = response["PublicInternetAccess"] as? String, let statusEnum = GMPublicInternetAccessStatus.init(rawValue: statusString) else {
            return
        }
        multicast.invokeForEachDelegate{ $0.publicInternetAccessStatus(status: statusEnum) }
        reset()
    }
    
    public func deviceNotReady() {
        multicast.invokeForEachDelegate{ $0.publicInternetAccessStatus(status: .notReady) }
    }
}
