//
//  BluetoothPairableProtocol.swift
//  MVM5G
//
//  Created by Matt Bruce on 4/15/22.
//  Copyright © 2022 Kyle. All rights reserved.
//

import Foundation
import MVMCore
import MVMCoreUI

/// This Protocol will be implemented by UI
protocol BluetoothPairableProtocol: AnyObject {
    associatedtype BluetoothPairingType: BluetoothParingProtocol
    var bluetoothPair: BluetoothPairingType? { get set }
    func setBluetoothConfig() -> Bool
    func track(_ message: Any, logType: AnalyticsLogType)
}

extension BluetoothPairableProtocol where Self: ViewController, Self: MFFGHSAnalyticsProtocol {
    @discardableResult
    func setBluetoothConfig() -> Bool {
        do {
            let config = try fivegBLEConfig()
            bluetoothPair?.bluetoothConfig = config
            return true
        } catch {
            track(error.localizedDescription)
            return false
        }
    }
    
    func track(_ message: Any, logType: AnalyticsLogType = .UI) {
        if let message = message as? String {
            trackGemini(value: [KeyMessage: message], logType: logType, pageInfo: AnalyticsPageInfo(pageType: loadObject?.pageType))
        } else if let dict = message as? [String: Any] {
            trackGemini(value: dict, logType: logType, pageInfo: AnalyticsPageInfo(pageType: loadObject?.pageType))
        }
    }
}

extension BluetoothPairableProtocol where Self: ViewController, Self: MFFGHSAnalyticsProtocol, Self: BluetoothDebuggableProtocol {
    @discardableResult
    func setBluetoothConfig() -> Bool {
        do {
            let config = try fivegBLEConfig()
            bluetoothPair?.setup(config: config, delegate: self)
            return true
        } catch {
            track(error.localizedDescription)
            return false
        }
    }

    func track(_ message: Any, logType: AnalyticsLogType = .UI) {
        if let message = message as? String {
            bluetoothDebugger?.addLog(message: message)
            trackGemini(value: [KeyMessage: message], logType: logType, pageInfo: AnalyticsPageInfo(pageType: loadObject?.pageType))
        } else if let dict = message as? [String: Any] {
            bluetoothDebugger?.addLog(message: dict.description)
            trackGemini(value: dict, logType: logType, pageInfo: AnalyticsPageInfo(pageType: loadObject?.pageType))
        }
    }
}
