//
//  WiFiConfigViewController.swift
//  Example
//
//  Created by ldc on 2024/1/27.
//

import UIKit
import PrinterSDK
import SVProgressHUD

class WiFiConfigViewController: UIViewController {
    
    var apConfig = S1WifiAPConfig()
    var staConfig = S1WifiSTAConfig()
    
    var staEnable = false
    
    let accessory: S1UdpLanTarget
    let configurator: S1UdpNetConfig
    
    init(configurator: S1UdpNetConfig, accessory: S1UdpLanTarget) {
        self.configurator = configurator
        apConfig.encrytion = .wpa_wpa2_psk
        self.apConfig.ip = "192.168.1.1"
        self.staConfig.encrytion = .wpa_wpa2_psk
        self.staConfig.ip = "192.168.1.1"
        self.staConfig.mask = "255.255.255.0"
        self.staConfig.gateway = "192.168.1.1"
        if !accessory.isEth {
            if accessory.isSta {
                self.staConfig.ip = accessory.ip
                self.staConfig.gateway = accessory.gateway
                self.staConfig.mask = accessory.mask
                self.staConfig.dhcpEnable = accessory.dhcpEnable
                self.staConfig.encrytion = accessory.wifiSecurity
                self.staConfig.ssid = accessory.wifiSsid
                self.staConfig.password = accessory.wifiSsid
            }else {
                self.apConfig.ip = accessory.ip
                self.apConfig.encrytion = accessory.wifiSecurity
                self.apConfig.ssid = accessory.wifiSsid
                self.apConfig.password = accessory.wifiSsid
            }
        }
        self.accessory = accessory
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.register(UINib(nibName: "STAConfigTableViewCell", bundle: nil), forCellReuseIdentifier: "sta")
            tableView.register(UINib(nibName: "APConfigTableViewCell", bundle: nil), forCellReuseIdentifier: "ap")
            tableView.register(UINib(nibName: "WiFiConfigHeaderTableViewCell", bundle: nil), forCellReuseIdentifier: "header")
        }
    }
    
    @IBAction func refetch(_ sender: UIButton) {
        
    }
    
    @IBAction func save(_ sender: UIButton) {
        
        view.endEditing(true)
        if (staEnable) {
            configurator.setWifiSta(accessory, config: staConfig) { error in
                if let error = error {
                    self.show(S1ErrorDescription(error: error))
                    return
                }
                self.show("保存成功")
            }
            return
        }
        configurator.setWifiAp(accessory, config: apConfig) { error in
            if let error = error {
                self.show(S1ErrorDescription(error: error))
                return
            }
            self.show("保存成功")
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "网络配置"
    }
}

extension WiFiConfigViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 40
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "header")! as! WiFiConfigHeaderTableViewCell
        if staEnable {
            cell.titleLabel.text = "STA模式"
            cell.enableSwitch.isOn = staEnable
            cell.enableChanged = { [weak self] in
                self?.staEnable = $0
                self?.tableView.reloadData()
            }
        }else {
            cell.titleLabel.text = "STA模式"
            cell.enableSwitch.isOn = staEnable
            cell.enableChanged = { [weak self] in
                self?.staEnable = $0
                self?.tableView.reloadData()
            }
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        return nil
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return  1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if staEnable {
            let cell = tableView.dequeueReusableCell(withIdentifier: "sta", for: indexPath) as! STAConfigTableViewCell
            cell.config = staConfig
            return cell
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ap", for: indexPath) as! APConfigTableViewCell
            cell.config = apConfig
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
    }
}
