import Cocoa

class NetConfigurationViewController: NSViewController {
    @IBOutlet weak var swDHCP: NSButton!
    @IBOutlet weak var tfIP: NSTextField!
    @IBOutlet weak var tfSubnet: NSTextField!
    @IBOutlet weak var tfGateway: NSTextField!
    @IBOutlet weak var tfPortNumber: NSTextField!
    @IBOutlet weak var tfInactivityTime: NSTextField!
    @IBOutlet weak var tfSystemName: NSTextField!
    @IBOutlet weak var vSystemName: NSView!
    
    var targetDevice = MPosDeviceInfo()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let ad = NSApplication.shared.delegate as! AppDelegate
        guard let target = ad.networkDevice else {
            dismiss(self)
            return
        }
        targetDevice = target
        swDHCP.state = target.dhcp.intValue == 1 ? .on : .off
        tfIP.stringValue = targetDevice.address
        tfSubnet.stringValue = targetDevice.subnetmask
        tfGateway.stringValue = targetDevice.gateway
        tfPortNumber.stringValue = String(targetDevice.portNumber.intValue)
        tfInactivityTime.stringValue = String(targetDevice.inactivityTime.intValue)
        setEnable(isDhcp: swDHCP.state == .on)
        if targetDevice.name.isEmpty {
            vSystemName.isHidden = true
        }else{
            tfSystemName.stringValue = targetDevice.name
        }
    }
    
    func setEnable(isDhcp: Bool) {
        if isDhcp {
            tfIP.stringValue = targetDevice.address
            tfSubnet.stringValue = targetDevice.subnetmask
            tfGateway.stringValue = targetDevice.gateway
        }
        tfIP.isEnabled = !isDhcp
        tfSubnet.isEnabled = !isDhcp
        tfGateway.isEnabled = !isDhcp
    }
    
    @IBAction func onDhcpSwitch(_ sender: Any) {
        setEnable(isDhcp: swDHCP.state == .on)
    }
    
    func showMessageBox(message:String, style:NSAlert.Style) {
        DispatchQueue.main.async {
            let alert = NSAlert()
            alert.alertStyle = style
            alert.messageText = message
            alert.runModal()
        }
    }
    
    @IBAction func onSetup(_ sender: Any) {
        let targetDevice = self.targetDevice
        let ipAddress = tfIP.stringValue
        let subnetMask = tfSubnet.stringValue
        let gateway = tfGateway.stringValue
        let useDhcp = swDHCP.state == .on ? NSNumber(value:1) : NSNumber(value: 0)
        let portNumber = NSNumber(value: tfPortNumber.intValue)
        let inactivityTime = NSNumber(value: tfInactivityTime.intValue)
        let systemName = tfSystemName.stringValue
        let timeout = NSNumber(value: 5.0)
        
        DispatchQueue.global(qos: .background).async { [weak self] in
            let result = UDPSetting.setNetworkInfo(targetDevice,
                                                   ipAddress: ipAddress,
                                                   subnetMask: subnetMask,
                                                   gateway: gateway,
                                                   useDhcp: useDhcp,
                                                   portNumber: portNumber,
                                                   inactivityTime: inactivityTime,
                                                   systemName: systemName,
                                                   timeout: timeout)
            
            if result == .MPOS_SUCCESS {
                self?.showMessageBox(message: "setNetworkDevice success", style: .informational)
            } else {
                self?.showMessageBox(message: "setNetworkDevice failed", style: .critical)
            }
        }
    }
    
    @IBAction func onClose(_ sender: Any) {
        dismiss(self)
    }
}
