//
//  WiFiScanViewController.swift
//  Example
//
//  Created by ldc on 2024/1/26.
//

import UIKit
import PrinterSDK

enum UDPNetConfigTargetType: CaseIterable {
    case eth
    case wifi
}

extension UDPNetConfigTargetType: CustomStringConvertible {
    
    var description: String {
        switch self {
        case .eth:
            return "配置以太网".localized
        case .wifi:
            return "配置Wi-Fi".localized
        }
    }
}

class UDPNetConfigScanViewController: UIViewController {
    
    var hotspotList = [S1UdpLanTarget]()
    
    let configurator = S1UdpNetConfig()

    @IBOutlet weak var tableView: UITableView!
    
    var iden: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "配网设备搜索".localized
        do {
            iden = try configurator.startSearch(-1, discovePortBlock: { [weak self] hotspot in
                if let index = self?.hotspotList.firstIndex(where: { $0.sn == hotspot.sn }) {
                    self?.hotspotList[index] = hotspot
                }else {
                    self?.hotspotList.append(hotspot)
                }
                self?.tableView?.reloadData()
            }, searchTimeoutBlock: { [weak self] in
                self?.show("搜索结束".localized)
            }, socketDisconnectedBlock: { [weak self] error in
                self?.show(String.init(format: "搜索失败: %@".localized, String(describing: error)))
            })
        } catch {
            self.show(String.init(format: "搜索失败: %@".localized, String(describing: error)))
        }
    }
    
    deinit {
        if let iden = iden {
            configurator.stopSearch(iden)
        }
    }
}

extension UDPNetConfigScanViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        return nil
    }
    
    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 hotspotList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "iden") ?? UITableViewCell.init(style: .value1, reuseIdentifier: "iden")
        let hotspot = hotspotList[indexPath.row]
        cell.textLabel?.text = hotspot.sn
        cell.detailTextLabel?.text = "\(hotspot.ip)"
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        let target = hotspotList[indexPath.row]
        self.valuePick(UDPNetConfigTargetType.allCases) { type in
            switch (type) {
            case .eth:
                let temp = EthernetConfigViewController(configurator: self.configurator, accessory: target)
                self.show(temp, sender: nil)
            case .wifi:
                let temp = WiFiConfigViewController(configurator: self.configurator, accessory: target)
                self.show(temp, sender: nil)
            }
        }
        
    }
}
