//
//  NetworkConfigViewController.swift
//  Example
//
//  Created by ldc on 2024/10/23.
//

import UIKit
import SVProgressHUD
import PrinterSDK
import IQKeyboardManagerSwift

class NetworkConfigViewController: UIViewController {
    
    var printer: S1V1ProtocolPrinter?{
        PrinterManager.instance.accessory
    }

    @IBOutlet weak var modeSegmentedControl: UISegmentedControl!
    
    @IBOutlet weak var nameTextField: UITextField!
    
    @IBOutlet weak var passwordTextField: UITextField!
    
    @IBOutlet weak var dhcpSwitch: UISwitch!
    
    @IBOutlet weak var ipTextField: UITextField!
    
    @IBOutlet weak var maskTextField: UITextField!
    
    @IBOutlet weak var rssiTextField: UITextField!
    
    @IBOutlet weak var saveButton: UIButton!
    
    @IBAction func saveAction(_ sender: UIButton) {
        
        guard let printer = printer, printer.connected else {
            SVProgressHUD.showError(withStatus: "打印机连接断开")
            return
        }
        let isAp = modeSegmentedControl.selectedSegmentIndex == 0
        let password = passwordTextField.text ?? ""
        guard let name = nameTextField.text, !name.isEmpty else {
            SVProgressHUD.showError(withStatus: "请输入名称".localized)
            return
        }
        SVProgressHUD.show(withStatus: "蓝牙配网中...")
        self.printer?.configPrinterWifi(withWifiName: name, wifiPassword: password, isApMode: isAp) { (error) in
            guard error == nil else{
                SVProgressHUD.showError(withStatus: "配置失败".localized)
                return
            }
            SVProgressHUD.showSuccess(withStatus: "配置成功".localized)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.printer?.getWifiWorkMode { (mode, error) in
            self.modeSegmentedControl.selectedSegmentIndex = mode - 1
            self.getWifiInfo()
        }
    }
    @IBAction func changeModeaAction(_ sender: UISegmentedControl) {
        IQKeyboardManager.shared.resignFirstResponder()
        self.getWifiInfo()
    }
    
    
    func getWifiInfo(){
        SVProgressHUD.show()
        if self.modeSegmentedControl.selectedSegmentIndex == 0 {
            self.printer?.getWifiKey(122, bytes: 64) { (value, error) in
                self.nameTextField.text = value
                self.nameTextField.isEnabled = false
                self.nameTextField.textColor = UIColor.gray
                
                self.printer?.getWifiKey(123, bytes: 64) { (value, error) in
                    self.passwordTextField.text = value
                    
                    self.printer?.getWifiIP(128) { (value, error) in
                        self.ipTextField.text = "   "  + "IP地址：".localized + (value ?? "")
                        
                        self.printer?.getWifiIP(155) { (value, error) in
                            self.maskTextField.text = "   "  + "子网掩码：".localized + (value ?? "")
                            
                            self.printer?.getWifiRSSI { (value, error) in
                                self.rssiTextField.text = "   " + (value ?? "") + " dBm"
                                guard error == nil else{
                                    SVProgressHUD.showError(withStatus: "获取失败".localized)
                                    return
                                }
                                SVProgressHUD.showSuccess(withStatus: "获取成功".localized)
                            }
                        }
                    }
                }
            }
        }else{
            self.printer?.getWifiKey(124, bytes: 64) { (value, error) in
                self.nameTextField.text = value
                self.nameTextField.isEnabled = true
                self.nameTextField.textColor = UIColor.black
                
                self.printer?.getWifiKey(125, bytes: 64) { (value, error) in
                    self.passwordTextField.text = value
                    
                    self.printer?.getWifiIP(129) { (value, error) in
                        self.ipTextField.text = "   "  + "IP地址：".localized + (value ?? "")
                        
                        self.printer?.getWifiIP(134) { (value, error) in
                            self.maskTextField.text = "   "  + "子网掩码：".localized + (value ?? "")
                            
                            self.printer?.getWifiRSSI { (value, error) in
                                self.rssiTextField.text = "   " + (value ?? "") + " dBm"
                                guard error == nil else{
                                    SVProgressHUD.showError(withStatus: "获取失败".localized)
                                    return
                                }
                                SVProgressHUD.showSuccess(withStatus: "获取成功".localized)
                            }
                        }
                    }
                }
            }
        }
    }
    
}
