//
//  GMFGTestScreen.swift
//  MVM5G
//
//  Created by Shohrab Chowdhury on 7/15/20.
//  Copyright © 2020 Kyle. All rights reserved.
//

import Foundation

class GMFGTestScreen: TopLabelsAndBottomButtonsViewController {
    
    private var deviceLocationLatTextField, deviceLocationLongTextField, advertisedDataTextField, pinTextField, orderNumberTextField, imeiTextField, currentRssiTextField, highRssiTextField, lowRssiTextField, cpeVersionNumberTextField: MFTextField?
    
    private var isARCapableToggle, isCPESupportedToggle, isHardcodeToggle, isEnable5GSignalToggle, isBCSForce: UISwitch?
    
    var toggleval = [String: Bool]()
    
    
    override public func newDataBuildScreen() {
        super.newDataBuildScreen()
        topLabelsView?.setHeadlineString("Gemini 5G Test Screen", messageString: "Click on load values button to load default values")
        topLabelsView?.separatorView?.isHidden = false
    }
    
    public override func buildViewsBetweenLabelsAndButtons() -> [UIView]? {
        
        var views:[UIView] = []
        
        let locationText = GMFGTestScreenData.shared.deviceLocation
        deviceLocationLatTextField = createTextField("Device Location Latitude", locationText.count > 0 && locationText[0] != GMFGConstant.empty ? locationText[0] : "29.72955022663853")
        views.append(deviceLocationLatTextField ?? MFTextField())
        
        deviceLocationLongTextField = createTextField("Device Location Longitude", locationText.count > 0 && locationText[0] != GMFGConstant.empty ? locationText[1] : "-95.370197614801185")
        views.append(deviceLocationLongTextField ?? MFTextField())
        
        let advertisedDataText = GMFGTestScreenData.shared.hardcodeDict.stringForkey(GMFGConstant.BLE.advertisedData)
        advertisedDataTextField = createTextField("Advertised Data", advertisedDataText != GMFGConstant.empty ? advertisedDataText : "VZ5G_RECEIVER_203975")
        views.append(advertisedDataTextField ?? MFTextField())
        
        let pinText = GMFGTestScreenData.shared.hardcodeDict.stringForkey(GMFGConstant.BLE.pin)
        pinTextField = createTextField("Pin", pinText != GMFGConstant.empty ? pinText : "00000")
        views.append(pinTextField ?? MFTextField())
        
        let orderNumberText = GMFGTestScreenData.shared.hardcodeDict.stringForkey(GMFGConstant.orderNumber)
        orderNumberTextField = createTextField("Order Number", orderNumberText != GMFGConstant.empty ? orderNumberText : "3110116")
        views.append(orderNumberTextField ?? MFTextField())
        
        let imeiText = GMFGTestScreenData.shared.hardcodeDict.stringForkey(GMFGConstant.imei)
        imeiTextField = createTextField("IMEI Number", imeiText != GMFGConstant.empty ? imeiText : "353450100203975")
        views.append(imeiTextField ?? MFTextField())
        
        let currentRSSIText = GMFGTestScreenData.shared.hardcodeDict.stringForkey(GMFGConstant.BLE.fiveGCurrentSignal)
        currentRssiTextField = createTextField("Current Rssi", currentRSSIText != GMFGConstant.empty ? currentRSSIText : "-95")
        views.append(currentRssiTextField ?? MFTextField())
        
        let highRSSIText = GMFGTestScreenData.shared.hardcodeDict.stringForkey(GMFGConstant.BLE.fiveGHighSignal)
        highRssiTextField = createTextField("High Rssi", highRSSIText != GMFGConstant.empty ? highRSSIText : "-98")
        views.append(highRssiTextField ?? MFTextField())
        
        let lowRSSIText = GMFGTestScreenData.shared.hardcodeDict.stringForkey(GMFGConstant.BLE.fiveGLowSignal)
        lowRssiTextField = createTextField("Low Rssi", lowRSSIText != GMFGConstant.empty ? lowRSSIText : "-92")
        views.append(lowRssiTextField ?? MFTextField())
        
        let cpeVersionNumberText = GMFGTestScreenData.shared.cpeVersionNumber
        cpeVersionNumberTextField = createTextField("CPE Version Number", cpeVersionNumberText != GMFGConstant.empty ? cpeVersionNumberText : "2.29.49.1")
        views.append(cpeVersionNumberTextField ?? MFTextField())

        let hardcodeToggleView = getSwitchView("Hardcoded Request", 1, GMFGConstant.Test.hardcodeToggle)
        hardcodeToggleView.uiSwitch.isOn = GMFGTestScreenData.shared.isHardcodedValues
        views.append(hardcodeToggleView.container)
        
        let arToggleView = getSwitchView("AR Capable", 2, GMFGConstant.AR.capable)
        arToggleView.uiSwitch.isOn = GMFGTestScreenData.shared.isARCapable
        views.append(arToggleView.container)
        
        let cpeToggleView = getSwitchView("CPE Simulation", 3, GMFGConstant.Test.CPEToggle)
        cpeToggleView.uiSwitch.isOn = GMFGTestScreenData.shared.isCPESimulated
        views.append(cpeToggleView.container)
        
        let fiveGSignalToggle = getSwitchView("Enable 5G Signal", 4, GMFGConstant.Test.fiveGSignalToggle)
        fiveGSignalToggle.uiSwitch.isOn = GMFGTestScreenData.shared.isEnable5GSignal
        views.append(fiveGSignalToggle.container)
        
        let fiveGBCSToggle = getSwitchView("Force BCS Wifi", 5, GMFGConstant.Test.forceBCS)
        fiveGBCSToggle.uiSwitch.isOn = GMFGTestScreenData.shared.forceBCS
        views.append(fiveGBCSToggle.container)

        let visualDebuggerToggle = getSwitchView("Visual Debugger", 6, GMFGConstant.Test.visualDebugger)
        visualDebuggerToggle.uiSwitch.isOn = GMFGTestScreenData.shared.visualDebugger
        views.append(visualDebuggerToggle.container)

        
        /// Added Empty label to add extra space above button
        let emptyLabel = Label()
        emptyLabel.text = " "
        views.append(emptyLabel)
        
        let doneButton = PrimaryButtonView(frame: .zero)
        doneButton.primaryButton?.setTitle("Load Values", for: .normal)
        doneButton.primaryButton?.addTarget(self, action: #selector(donePressed(sender:)), for: .touchUpInside)
        doneButton.primaryButton?.isEnabled = true
        views.append(doneButton)
        
        return views
    }
    
    @objc func donePressed(sender: UIButton) {
        setHardcodeValue()
        dismiss(animated: false, completion: nil)
    }
    
    @objc func switchValueDidChange(_ sender: UISwitch!) {
        
        switch sender.tag {
        case 1:
            toggleval[GMFGConstant.Test.hardcodeToggle] = sender.isOn
            GMFGTestScreenData.shared.isHardcodedValues = toggleval.boolForKey(GMFGConstant.Test.hardcodeToggle)
        case 2:
            toggleval.updateValue(sender.isOn, forKey: GMFGConstant.AR.capable)
            GMFGTestScreenData.shared.isARCapable = toggleval.boolForKey(GMFGConstant.AR.capable)
        case 3:
            toggleval.updateValue(sender.isOn, forKey: GMFGConstant.Test.CPEToggle)
            GMFGTestScreenData.shared.isCPESimulated = toggleval.boolForKey(GMFGConstant.Test.CPEToggle)
        case 4:
            toggleval.updateValue(sender.isOn, forKey: GMFGConstant.Test.fiveGSignalToggle)
            GMFGTestScreenData.shared.isEnable5GSignal = toggleval.boolForKey(GMFGConstant.Test.fiveGSignalToggle)
        case 5:
            toggleval.updateValue(sender.isOn, forKey: GMFGConstant.Test.forceBCS)
            GMFGTestScreenData.shared.forceBCS = toggleval.boolForKey(GMFGConstant.Test.forceBCS)
        case 6:
            toggleval.updateValue(sender.isOn, forKey: GMFGConstant.Test.visualDebugger)
            GMFGTestScreenData.shared.visualDebugger = toggleval.boolForKey(GMFGConstant.Test.visualDebugger)
        default:
            break
        }
    }
    
    override public func spaceAboveBetweenView() -> NSNumber? {
        return 30
    }
    
    override func spaceAroundUIObject(_ object: Any?) -> UIEdgeInsets {
        var insets = super.spaceAroundUIObject(object)
        insets.top = PaddingOne
        return insets
    }
    
    private func createTextField(_ formLabel: String, _ text: String) -> MFTextField? {
        let textField = MFTextField(bothDelegates: self)
        textField?.formLabel?.text = formLabel
        textField?.text = text as NSString?
        return textField
    }
    
    func getSwitchView(_ labelText: String, _ tag: Int, _ key: String) -> (container: UIView, uiSwitch: UISwitch, label: Label) {
        let toggleView = MFCommonViewsUtility.commonView()
        
        let toggle = UISwitch(frame: .zero)
        toggleView.addSubview(toggle)
        toggle.tag = tag
        toggle.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)
        
        let arToggleLabel = Label()
        arToggleLabel.text = labelText
        toggleView.addSubview(arToggleLabel)
        arToggleLabel.font = MFStyler.fontRegularBodyLarge()
        
        NSLayoutConstraint.constraintPinSubview(toggle, pinTop: true, pinBottom: true, pinLeft: true, pinRight: false)
        NSLayoutConstraint.constraintPinSubview(arToggleLabel, pinTop: true, pinBottom: true, pinLeft: false, pinRight: true)
        NSLayoutConstraint(item: toggle as Any, attribute: .width, relatedBy: .equal, toItem: toggleView, attribute: .width, multiplier: 0, constant: 0).isActive = true
        NSLayoutConstraint(item: arToggleLabel as Any, attribute: .left, relatedBy: .equal, toItem: toggle, attribute: .right, multiplier: 1.0, constant: PaddingThree).isActive = true
        
        return (toggleView, toggle, arToggleLabel)
    }
    
    func setHardcodeValue() {
        let hardCodeValue: [String : Any] = [
            GMFGConstant.AR.deviceLocation : [
                deviceLocationLatTextField?.text?.doubleValue,
                deviceLocationLongTextField?.text?.doubleValue
            ],
            GMFGConstant.AR.capable             : GMFGTestScreenData.shared.isARCapable,
            GMFGConstant.BLE.advertisedData     : advertisedDataTextField?.text as Any,
            GMFGConstant.BLE.firmwareVersion    : (cpeVersionNumberTextField?.text)! as String,
            GMFGConstant.BLE.pin                : pinTextField?.text as Any,
            GMFGConstant.orderNumber            : orderNumberTextField?.text as Any,
            GMFGConstant.imei                   : imeiTextField?.text as Any,
            GMFGConstant.BLE.fiveGCurrentSignal : currentRssiTextField?.text as Any,
            GMFGConstant.BLE.fiveGHighSignal    : highRssiTextField?.text as Any,
            GMFGConstant.BLE.fiveGLowSignal     : lowRssiTextField?.text as Any
        ]
        
        /// We can append to above dict but make sure whether backend is expecting below parameters as well when a call is made
        let hardcodeToggleVal: [String: Bool] = [
            GMFGConstant.Test.hardcodeToggle    : toggleval.boolForKey(GMFGConstant.Test.hardcodeToggle),
            GMFGConstant.Test.CPEToggle         : toggleval.boolForKey(GMFGConstant.Test.CPEToggle),
            GMFGConstant.Test.fiveGSignalToggle : toggleval.boolForKey(GMFGConstant.Test.fiveGSignalToggle),
            GMFGConstant.Test.forceBCS          : toggleval.boolForKey(GMFGConstant.Test.forceBCS),
            GMFGConstant.Test.visualDebugger    : toggleval.boolForKey(GMFGConstant.Test.visualDebugger)
        ]
        
        GMFGTestScreenData.shared.hardcodeDict = hardCodeValue
        GMFGTestScreenData.shared.toggleDict = hardcodeToggleVal
        GMFGTestScreenData.shared.deviceLocation = [(deviceLocationLatTextField?.text as String? ?? GMFGConstant.empty), deviceLocationLongTextField?.text as String? ?? GMFGConstant.empty]
        GMFGTestScreenData.shared.cpeVersionNumber = (cpeVersionNumberTextField?.text)! as String
    }
}

class GMFGTestScreenData: NSObject {
    var geminiHardCode = MFSessionSingleton.sharedGlobal()?.testObject?.is5GGoodSignalActive ?? false
    var isCPESimulated = true && (MFSessionSingleton.sharedGlobal()?.testObject?.is5GGoodSignalActive ?? false)
    var isHardcodedValues = false  && (MFSessionSingleton.sharedGlobal()?.testObject?.is5GGoodSignalActive ?? false)
    var isARCapable = true
    var cpeVersionNumber = GMFGConstant.empty
    var isEnable5GSignal = true  && (MFSessionSingleton.sharedGlobal()?.testObject?.is5GGoodSignalActive ?? false)
    var forceBCS = false
    var visualDebugger = false
    var deviceLocation: [String] = []
    var hardcodeDict: [String: Any] = [:]
    var toggleDict: [String : Bool] = [:]
    static let shared = GMFGTestScreenData()
    
    /// to avoid called to create instance
    private override init() {
        super.init()
    }
}
