//
//  PDWiFiSelectIPViewController.swift
//  PrinterDemo
//
//  Created by ios on 2019/9/12.
//  Copyright © 2019 XiamenHY. All rights reserved.
//

import UIKit

class PDWiFiSelectIPViewController: PDBaseViewController {

    lazy var titleLb : UILabel = {
        let temp = UILabel.init()
        temp.text = "Please enter the corresponding ip address:".localized
        temp.textColor = UIColor.titleColor
        temp.textAlignment = .left
        temp.font = UIFont.systemFont(ofSize: 16)
        return temp
    }()
    
    lazy var ipTF: UILabel = {
        let temp = UILabel.init()
        temp.textColor = UIColor.white
        temp.textAlignment = .center
        temp.backgroundColor = UIColor.theme
        temp.font = UIFont.boldSystemFont(ofSize: 17)
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(clickShowIPView))
        temp.isUserInteractionEnabled = true
        temp.addGestureRecognizer(tap)
        return temp
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "WiFi".localized
        navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "Connect".localized, style: UIBarButtonItem.Style.plain, target: self, action: #selector(connectDevice))
        
        view.addSubview(titleLb)
        titleLb.snp.makeConstraints {
            $0.left.equalToSuperview().offset(10)
            $0.right.equalToSuperview().offset(-10)
            $0.top.equalToSuperview().offset(30)
            $0.height.equalTo(40)
        }
        
        view.addSubview(ipTF)
        ipTF.snp.makeConstraints {
            $0.left.equalToSuperview().offset(10)
            $0.right.equalToSuperview().offset(-10)
            $0.top.equalTo(titleLb.snp.bottom).offset(5)
            $0.height.equalTo(50)
        }
        
        guard let ip = UserDefaults.standard.string(forKey: PDWiFiIP) else {
            ipTF.text = "192.168.1.1"
            return
        }
        ipTF.text = ip
    }
    
    @objc private func clickShowIPView() {
        
        guard let temp = PDAppWindow.rootViewController else { return }
        let pickerVC = PDSelectPickerViewController()
        pickerVC.view.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        pickerVC.view.frame = temp.view.bounds
        pickerVC.selectedIP = ipTF.text
        temp.addChild(pickerVC)
        temp.view.addSubview(pickerVC.view)
        pickerVC.refreshIP()
        pickerVC.selectIPClosure = {[weak self](ip1,ip2,ip3,ip4) in
            guard let self = self else { return }
            self.ipTF.text = "\(ip1).\(ip2).\(ip3).\(ip4)"
        }
    }

    @objc private func connectDevice() {
        
        let router = PTRouter.init()
        if !router.connected {
            UIAlertController.showActionAlert("Tips".localized, message: "Connect router in iOS system setting".localized, confirm: "Setting".localized, confirmHandle: { (_) in
                let url = URL.init(string: "APP-Prefs:root")
                UIApplication.shared.openURL(url!)
            })
        }else {
            
            SVProgressHUD.show(withStatus: "Connecting, pls. wait".localized)
            let pt = PTPrinter.init()
            pt.ip = ipTF.text
            pt.module = .wiFi
            pt.port = "9100"
            
            PTDispatcher.share().connect(pt)
            PTDispatcher.share().whenConnectSuccess {[weak self] in
                guard let self = self else { return }
                SVProgressHUD.showSuccess(withStatus: "Connection succeeded".localized)
                //tip：This line of code must be added
                PTDispatcher.share().printerConnected = pt
                UserDefaults.standard.set(self.ipTF.text, forKey: PDWiFiIP)
                self.navigationController?.popViewController(animated: true)
            }
            
            PTDispatcher.share().whenConnectFailureWithErrorBlock { (error) in
                var errorStr: String?
                switch error {
                case .wifiTimeout:
                    errorStr = "Connection timeout".localized
                case .wifiSocketError:
                    errorStr = "Connection Error".localized
                default:
                    break
                }
                if let temp = errorStr {
                    SVProgressHUD.showError(withStatus: temp)
                }
            }
        }
        
    }
    
    

}
