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

import UIKit
import PrinterSDK

class APConfigTableViewCell: UITableViewCell {
    
    @IBOutlet weak var ssidTextField: UITextField!
    
    @IBOutlet weak var passwordTextField: UITextField!
    
    @IBOutlet weak var encriptionButton: UIButton!
    
    @IBOutlet weak var ipTextField: UITextField!
    
    var config: S1WifiAPConfig? {
        didSet {
            ssidTextField.text = config?.ssid
            passwordTextField.text = config?.password
            encriptionButton.setTitle(config?.encrytion.description ?? "选择加密方式", for: .normal)
            ipTextField.text = config?.ip
        }
    }
    
    @IBAction func encriptionButtonAction(_ sender: UIButton) {
        let encryptions: [S1WiFiEncryption] = [
            .open,
            .wep,
            .wpa2_psk,
            .wpa_wpa2_psk,
            .wpa_psk,
            .wpa,
            .wpa2,
            .sae,
        ]
        self.viewController?.valuePick(encryptions, closure: { encryption in
            self.config?.encrytion = encryption
            self.encriptionButton.setTitle(encryption.description, for: .normal)
        })
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

extension APConfigTableViewCell: UITextFieldDelegate {
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        
        let value = textField.text ?? ""
        if textField == ssidTextField {
            config?.ssid = value
        }
        if textField == passwordTextField {
            config?.password = value
        }
        if textField == ipTextField {
            config?.ip = value
        }
    }
}
