//
//  ModalVC.swift
//  KMComponentLibrary
//
//  Created by Niehaoyu on 2024/9/25.
//

import Cocoa
import KMComponentLibrary

class ModalVC: NSViewController {

    @IBOutlet var typeBox: NSComboBox!
    @IBOutlet var titleField: NSTextField!
    @IBOutlet var subTitleField: NSTextField!
    @IBOutlet weak var showImageAction: NSButton!
    
    let modalView: ComponentModal = ComponentModal()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        
        self.typeBox.selectItem(at: 0)
        self.reloadData()
        
        
       NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: titleField)
       NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: titleField)
       NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: titleField)
        
       NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: subTitleField)
       NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: subTitleField)
       NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: subTitleField)
        
        
        modalView.frame = CGRectMake(100, 100, 480, 172)
        
    }
    
    @IBAction func showModalAction(_ sender: Any) {
        
        var image: NSImage? = ComponentLibrary.shared.image(forResource: "test")
        
        self.showImageAction.isEnabled = false
        
        var type: ComponentMessageType = .info
        if self.typeBox.indexOfSelectedItem == 1 {
            type = .success
        } else if self.typeBox.indexOfSelectedItem == 2 {
            type = .warning
        } else if self.typeBox.indexOfSelectedItem == 3 {
            type = .error
        } else if self.typeBox.indexOfSelectedItem == 4 {
            type = .normal_custom
            self.showImageAction.isEnabled = true
            if self.showImageAction.state == .off {
                image = nil
            }
        }
        
        let text = self.titleField.stringValue
        let subText = self.subTitleField.stringValue
        
        let modalProperty: ComponentModalProperty = ComponentModalProperty(messageType: type,
                                                                           image: image,
                                                                           text: text,
                                                                           subText: subText)
       
        modalView.show(with: modalProperty, in: self.view.window)
        
    }
     
    
    func reloadData() {
        
    }
    
    @IBAction func clickAction(_ sender: Any) {
        self.reloadData()
    }
    
    //MARK: - TextNotification
    @objc func textFieldDidBeginEditingNotification(_ notification: Notification) {
        
    }
    
    @objc func textFieldDidChangeNotification(_ notification: Notification) {
         self.reloadData()
    }
    
    @objc func textFieldDidEndEditingNotification(_ notification: Notification) {
        print("textFieldDidEndEditingNotification")
        
        self.reloadData()
    }
}