//
//  InputDemoVC.swift
//  PDF Reader Pro Edition
//
//  Created by Niehaoyu on 2024/8/26.
//

import Cocoa
import KMComponentLibrary

class InputDemoVC: NSViewController {

    //Input
    @IBOutlet weak var componentInput: ComponentInput!
    @IBOutlet weak var inputHeightConst: NSLayoutConstraint!
    @IBOutlet weak var inputSizeBox: NSComboBox!
    @IBOutlet weak var inputErrorBtn: NSButton!
    @IBOutlet weak var inputDisableBtn: NSButton!
    @IBOutlet weak var inputShowClearBtn: NSButton!
    @IBOutlet weak var inputShowPrefixBtn: NSButton!
    @IBOutlet weak var inputShowSuffixBtn: NSButton!
     
    //Textare
    @IBOutlet weak var componentTextarea: ComponentTextarea!
    @IBOutlet weak var textareaDisableBtn: NSButton!
     
    //Password
    @IBOutlet weak var passwordView: ComponentPasswordView!
    @IBOutlet weak var passwordErrorBtn: NSButton!
    @IBOutlet weak var passwordDisableBtn: NSButton!
    @IBOutlet weak var passwordHeightConst: NSLayoutConstraint!
     
    //Addon
    @IBOutlet weak var addOnItem: ComponentInputAddon!
    @IBOutlet weak var addOnHeightConst: NSLayoutConstraint!
    @IBOutlet weak var addOnWidthConst: NSLayoutConstraint!
    @IBOutlet weak var addonBeforeBtn: NSButton!
    @IBOutlet weak var addonDisableBtn: NSButton!
    @IBOutlet weak var addonsizeBox: NSComboBox!
    @IBOutlet weak var addonTypeBox: NSComboBox!
    @IBOutlet weak var addonOnlyReadBtn: NSButton!
     
    //InputWithAddon
    @IBOutlet weak var inputWithAddon: ComponentInputWithAddon!
    @IBOutlet weak var inputWithAddonType: NSComboBox!
    @IBOutlet weak var inputWithAddonSize: NSComboBox!
    @IBOutlet weak var inputWithAddonDisableBtn: NSButton!
    @IBOutlet weak var inputWithAddonHeightConst: NSLayoutConstraint!
    
    
    override func viewDidLoad() {
        super.viewDidLoad(
        )
        // Do view setup here.
         
        self.addonTypeBox.selectItem(at: 0)
        self.inputWithAddonType.selectItem(at: 0)
        
        self.reloadData()
         
        self.addOnItem.setTarget(self, action: #selector(addOnItemClick(_:)))
         
    }
    
    func reloadData() {
        
        //Input
        var inputSize: ComponentSize = .m
        if self.inputSizeBox.indexOfSelectedItem == 1 {
            inputSize = .s
        } else if self.inputSizeBox.indexOfSelectedItem == 2 {
            inputSize = .xs
        } else if self.inputSizeBox.indexOfSelectedItem == 3 {
            inputSize = .xxs
        }
        
        let inputProperty: ComponentInputProperty = ComponentInputProperty(size: inputSize,
                                                                           state: .normal,
                                                                           isError: self.inputErrorBtn.state == .on,
                                                                           showPrefix: self.inputShowPrefixBtn.state == .on,
                                                                           showSuffix: self.inputShowSuffixBtn.state == .on,
                                                                           showClear: self.inputShowClearBtn.state == .on,
                                                                           isDisabled: self.inputDisableBtn.state == .on,
                                                                           placeholder: "Please enter...",
                                                                           text: componentInput.properties.text)
        self.componentInput.properties = inputProperty
        self.inputHeightConst.constant = self.componentInput.properties.propertyInfo.viewHeight
         
        //Textarea
        let textareaProperty: ComponentTextareaProperty = ComponentTextareaProperty(size: .m,
                                                                                    state: .normal,
                                                                                    isError: false,
                                                                                    placeholderString: "Please enter...",
                                                                                    totalCount: 100,
                                                                                    text: self.componentTextarea.properties.text,
                                                                                    isDisabled:self.textareaDisableBtn.state == .on)
        self.componentTextarea.properties = textareaProperty
        
        //Password
        let passwordProperty: ComponentPasswordProperty = ComponentPasswordProperty(size: .m,
                                                                                    state: .normal,
                                                                                    isError: self.passwordErrorBtn.state == .on,
                                                                                    placeholderString: "Please enter password",
                                                                                    text: "",
                                                                                    isDisabled: self.passwordDisableBtn.state == .on,
                                                                                    errorText: "Here is the error message description.")
        self.passwordView.properties = passwordProperty
        self.passwordHeightConst.constant = passwordProperty.propertyInfo.viewHeight
        
        //Addon
        var addonSize: ComponentSize = .m
        if self.addonsizeBox.indexOfSelectedItem == 1 {
            addonSize = .s
        }
        
        self.addonOnlyReadBtn.isEnabled = true
        
        var addonType: InputAddonType = .text
        if self.addonTypeBox.indexOfSelectedItem == 0 {
            self.addonOnlyReadBtn.isEnabled = false
            self.addonOnlyReadBtn.state = .on
        } else if self.addonTypeBox.indexOfSelectedItem == 1 {
            addonType = .textWithColor
        } else if self.addonTypeBox.indexOfSelectedItem == 2 {
            addonType = .imageWithColor
        } else if self.addonTypeBox.indexOfSelectedItem == 3 {
            addonType = .dropDown
        }
        let addonProperty: ComponentInputAddonProperty = ComponentInputAddonProperty(size: addonSize,
                                                                                     state: .normal,
                                                                                     isDisabled: self.addonDisableBtn.state == .on,
                                                                                     addOnBefore: self.addonBeforeBtn.state == .on,
                                                                                     onlyRead: self.addonOnlyReadBtn.state == .on,
                                                                                     addonType: addonType,
                                                                                     iconImage: ComponentLibrary.shared.image(forResource: "test"),
                                                                                     text: "add-on")
        self.addOnItem.properties = addonProperty
        self.addOnWidthConst.constant = self.addOnItem.properties.propertyInfo.viewWidth
        self.addOnHeightConst.constant = self.addOnItem.properties.propertyInfo.viewHeight
         
        //Input With Addon
        var inputWithSize: ComponentSize = .m
        if self.inputWithAddonSize.indexOfSelectedItem == 1 {
            inputWithSize = .s
        }
        var inputWithType: InputWithAddonType = .text
        if self.inputWithAddonType.indexOfSelectedItem == 1 {
            inputWithType = .button
        } else if self.inputWithAddonType.indexOfSelectedItem == 2 {
            inputWithType = .dropdown
        }
         
        let inputWithAddonProperty = ComponentInputWithAddonProperty(size: inputWithSize,
                                                                     isDisabled: self.inputWithAddonDisableBtn.state == .on,
                                                                     addonType: inputWithType)
        self.inputWithAddon.properties = inputWithAddonProperty
        self.inputWithAddonHeightConst.constant = self.inputWithAddon.properties.propertyInfo.viewHeight
        
    }
     
    
    @IBAction func buttonAction(_ sender: Any) {
        self.reloadData()
    }
    
    
    @objc func addOnItemClick(_ sender: Any) {
       
    }
    
    override func mouseDown(with event: NSEvent) {
        super.mouseDown(with: event)
        
        self.view.window?.makeFirstResponder(nil)
    }
}