//
//  KMPopMenuButton.swift
//  PDF Reader Pro
//
//  Created by wanjun on 2023/10/7.
//

import Cocoa

class KMPopMenuButton: NSButton {

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.setupButton()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.setupButton()
    }
    
    private func setupButton() {
        self.addTrackingArea()
        self.wantsLayer = true
        self.layer?.backgroundColor = NSColor.clear.cgColor
        self.font = NSFont.systemFont(ofSize: 14)
    }
    
//    override class var cellClass: AnyClass? {
//        return KMPopMenuButtonCell.self
//    }
//    
    func addTrackingArea() {
        let trackingArea = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeAlways, .mouseMoved], owner: self, userInfo: nil)
        self.addTrackingArea(trackingArea)
    }
    
    override func mouseEntered(with event: NSEvent) {
        super.mouseEntered(with: event)
        
        if self.isEnabled {
            if #available(macOS 10.14, *) {
                self.layer?.backgroundColor = NSColor.controlAccentColor.cgColor
            } else {
                self.layer?.backgroundColor = NSColor.blue.cgColor
            }
            self.setTitleColorWithColor(NSColor.white, font: nil)
        }
    }
    
    override func mouseExited(with event: NSEvent) {
        super.mouseExited(with: event)
        
        if self.isEnabled {
            self.layer?.backgroundColor = NSColor.clear.cgColor
            self.setTitleColorWithColor(NSColor.labelColor, font: nil)
        }
    }
    
    override var intrinsicContentSize: NSSize {
        var size = super.intrinsicContentSize
        size.width += 45
        return size
    }
    
    override var state: NSControl.StateValue {
        didSet {
            if state == .on {
                if #available(macOS 10.14, *) {
                    self.layer?.backgroundColor = NSColor.controlAccentColor.cgColor
                } else {
                    self.layer?.backgroundColor = NSColor.blue.cgColor
                }
                self.setTitleColorWithColor(NSColor.white, font: nil)
            } else {
                self.layer?.backgroundColor = NSColor.clear.cgColor
                self.setTitleColorWithColor(NSColor.labelColor, font: nil)
            }
        }
    }
    
//    override func setTitle(_ title: String) {
//        super.title = title
//        self.setTitleColorWithColor(NSColor.labelColor, font: nil)
//    }
    
    func setTitleColorWithColor(_ color: NSColor?, font: NSFont?) {
        // 设置按钮文本颜色和字体
        if let color = color {
            let titleAttributes: [NSAttributedString.Key: Any] = [
                .foregroundColor: color
            ]
            self.attributedTitle = NSAttributedString(string: self.title, attributes: titleAttributes)
        }
        if let font = font {
            self.font = font
        }
    }

    
}