//
//  EthernetConfigTableViewCell.swift
//  Example
//
//  Created by ldc on 2024/3/1.
//

import UIKit
import PrinterSDK

class EthernetConfigTableViewCell: UITableViewCell {
    
    @IBOutlet weak var ipDHCPSegmentedControl: UISegmentedControl!
    
    @IBOutlet weak var ipTextField: UITextField!
    
    @IBOutlet weak var netMaskTextField: UITextField!
    
    @IBOutlet weak var gatewayTextField: UITextField!
    
    var config: S1EthernetConfig? {
        didSet {
            ipDHCPSegmentedControl.selectedSegmentIndex = config?.dhcpEnable ?? true ? 0 : 1
            ipTextField.text = config?.ip
            netMaskTextField.text = config?.mask
            gatewayTextField.text = config?.gateway
        }
    }
    
    @IBAction func segmentedControlValueChanged(_ sender: UISegmentedControl) {
        
        config?.dhcpEnable = sender.selectedSegmentIndex == 0
    }
    
    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 EthernetConfigTableViewCell: UITextFieldDelegate {
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        
        let value = textField.text ?? ""
        if textField == ipTextField {
            config?.ip = value
        }
        if textField == netMaskTextField {
            config?.mask = value
        }
        if textField == gatewayTextField {
            config?.gateway = value
        }
    }
}
