//
//  KMDesignDropdown.swift
//  PDF Master
//
//  Created by wanjun on 2023/2/20.
//

import Cocoa

class KMDesignDropdown: NSViewController {

    @IBOutlet weak var mainBox: KMMoveBox!
    @IBOutlet weak var button : NSButton!
    @IBOutlet weak var label : NSTextField!
    @IBOutlet weak var dropdownView : NSView!

    @IBOutlet weak var horizontalPadding: NSLayoutConstraint!
    
    var height: Float = 30.0
    var horizontalPadding_spacing: Float = 8.0
    var textColor: NSColor = .black // 内容颜色
    var textColor_hov: NSColor = .black // 内容颜色
    var textColor_sel: NSColor = .black // 内容颜色
    var textColor_disabled: NSColor = .black // 内容颜色
    var background: NSColor = .clear// 背景颜色
    var background_hov: NSColor = .clear// 背景颜色
    var background_sel: NSColor = .clear// 背景颜色
    var background_disabled: NSColor = .clear// 背景颜色
    var cornerRadius: Float = 0.0// 边框圆角
    var cornerRadius_hov: Float = 0.0// 边框圆角
    var cornerRadius_sel: Float = 0.0// 边框圆角
    var cornerRadius_disabled: Float = 0.0// 边框圆角
    var lineHeight: CGFloat = 20.0 // 默认 内容行高
    var lineHeight_hov: CGFloat = 20.0 // 默认 内容行高
    var lineHeight_sel: CGFloat = 20.0 // 默认 内容行高
    var lineHeight_disabled: CGFloat = 20.0 // 默认 内容行高
    var font: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
    var font_hov: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
    var font_sel: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
    var font_disabled: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体

    var stringValue: String = ""// 内容
    var toolTip: String = "" // 提示文字

    // button 通用属性
    var action: Selector?   // 点击事件
    var target: AnyObject? // 对象目标
    var enabled: Bool = true // 是否可点击
    var state: KMDesignTokenState = .Norm
    var canHover: Bool = true // 是否可悬浮
    var isHidden: Bool = false // 是否隐藏
    var editable: Bool = false //是否允许编辑

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        
        mainBox.contentView = dropdownView
        
        mainBox.move =  { [weak self](mouseEntered: Bool) -> Void in
            if self != nil {
                if mouseEntered {
                    if self!.state != .Sel && self!.canHover && (self!.state != .Disabled) {
                        self!.state = .Hov
                        self!.updateUI()
                    }
                } else {
                    if self!.state != .Sel && self!.canHover && (self!.state != .Disabled) {
                        self!.state = .Norm
                        self!.updateUI()
                    }
                }
            }
        }
    }
    
    // MARK: Private Methods

    func updateUI() -> Void {
        let paragraphStyle = NSMutableParagraphStyle()
        if (state == .Norm) {
            mainBox.fillColor = background
            mainBox.cornerRadius = CGFloat(cornerRadius)
            label.textColor = textColor
            label.font = font
            paragraphStyle.lineSpacing = lineHeight
        } else if (state == .Hov) {
            mainBox.fillColor = background_hov
            mainBox.cornerRadius = CGFloat(cornerRadius_hov)
            label.textColor = textColor_hov
            label.font = font_hov
            paragraphStyle.lineSpacing = lineHeight_hov
        } else if (state == .Sel) {
            mainBox.fillColor = background_sel
            mainBox.cornerRadius = CGFloat(cornerRadius_sel)
            label.textColor = textColor_sel
            label.font = font_sel
            paragraphStyle.lineSpacing = lineHeight_sel
        } else if (state == .Disabled) {
            mainBox.fillColor = background_disabled
            mainBox.cornerRadius = CGFloat(cornerRadius_disabled)
            label.textColor = textColor_disabled
            label.font = font_disabled
            paragraphStyle.lineSpacing = lineHeight_disabled
        }
        if action != nil {
            button.action = action
            button.target = target
        }
        button.isEnabled = enabled
        label.attributedStringValue = NSAttributedString(string: stringValue, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
        if toolTip != "" {
            button.toolTip = toolTip
        }
        self.view.isHidden = isHidden
        
        horizontalPadding.constant = CGFloat(horizontalPadding_spacing)
    }
}