
import UIKit

class NetConfigurationVewController: UIViewController {
    
    
    @IBOutlet weak var swDHCP: UISwitch!
    @IBOutlet weak var tfIP: UITextField!
    @IBOutlet weak var tfSubnet: UITextField!
    @IBOutlet weak var tfGateway: UITextField!
    @IBOutlet weak var tfPortNumber: UITextField!
    @IBOutlet weak var tfInactivityTime: UITextField!
    @IBOutlet weak var tfSystemName: UITextField!
    @IBOutlet weak var vSystemName: UIView!
    
    var targetDevice: MPosDeviceInfo?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let ad = UIApplication.shared.delegate as! AppDelegate
        if let targetDevice = ad.networkDevice {
            self.targetDevice = targetDevice
            swDHCP.setOn(targetDevice.dhcp.intValue != 0, animated: true)
            tfIP.text = targetDevice.address
            tfSubnet.text = targetDevice.subnetmask
            tfGateway.text = targetDevice.gateway
            tfPortNumber.text = targetDevice.portNumber.stringValue
            tfInactivityTime.text = targetDevice.inactivityTime.stringValue
            setEnable(swDHCP.isOn)
            if targetDevice.interfaceType == .WIFI {
                tfSystemName.text = targetDevice.name
            }else{
                vSystemName.isHidden = true
            }
        }else{
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    func setEnable(_ isDhcp: Bool) {
        if isDhcp {
            tfIP.text = targetDevice?.address
            tfSubnet.text = targetDevice?.subnetmask
            tfGateway.text = targetDevice?.gateway
        }
        tfIP.isEnabled = !isDhcp
        tfSubnet.isEnabled = !isDhcp
        tfGateway.isEnabled = !isDhcp
    }
    
    @IBAction func onDhcpSwitch(_ sender: Any) {
        setEnable(swDHCP.isOn)
    }
    
    func showMessageBox(message: String, title:String){
        DispatchQueue.main.async {
            let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
            let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alert.addAction(defaultAction)
            self.present(alert, animated: true, completion: nil)
        }
    }
    
    @IBAction func onSetup(_ sender: Any) {
        let ipAddr = tfIP.text
        let subnetMask = tfSubnet.text
        let gateway = tfGateway.text
        let useDhcp = swDHCP.isOn ? NSNumber(1) : NSNumber(0)
        let portNumber = Int(tfPortNumber.text ?? "") as NSNumber?
        let inactivityTime = Int(tfInactivityTime.text ?? "") as NSNumber?
        let systemName = tfSystemName.text
        let timeout = NSNumber(value: Double(5.0))
        
        DispatchQueue.global().async { [weak self] in
            guard let self = self else { return }
            let result = UDPSetting.setNetworkInfo(targetDevice, 
                                                   ipAddress: ipAddr,
                                                   subnetMask: subnetMask,
                                                   gateway: gateway,
                                                   useDhcp: useDhcp,
                                                   portNumber: portNumber,
                                                   inactivityTime: inactivityTime,
                                                   systemName: systemName,
                                                   timeout: timeout)
            
            if result == .MPOS_SUCCESS {
                showMessageBox(message: "setNetworkInfo success", title: "INFORMATION")
            }else{
                showMessageBox(message: "setNetworkInfo failed", title: "ERROR")
            }
        }
    }
}

extension NetConfigurationVewController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}
