//
//  KMDesignPropertySelector.swift
//  PDF Master
//
//  Created by wanjun on 2023/2/23.
//

import Cocoa

@objc enum PropertySelectorType : Int {
    case Icon_Btn = 0
    case Color_Icon_Btn
    case Line_Style
}

class KMDesignPropertyColorBox: NSBox {
    var isClear: Bool = false
    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        if isClear {
            let path = NSBezierPath()
            let startPoint = NSPoint(x: 3, y: 3)
            path.move(to: startPoint)
            // Set the end point of the path
            let endPoint = NSPoint(x: dirtyRect.width-3, y: dirtyRect.height-3)
            path.line(to: endPoint)
            // Draw the path
            NSColor(hex: "#F3465B").setStroke()
            path.lineWidth = 1
            path.stroke()
        }
    }
}

class KMDesignPropertySelector: KMDesignBase {
    
    @IBOutlet weak var mainBox: KMMoveBox!
    @IBOutlet weak var button: NSButton!

    @IBOutlet weak var iconView: NSView!
    @IBOutlet weak var icon: NSImageView!

    @IBOutlet weak var colorIconView: NSView!
    @IBOutlet weak var iconBox: KMDesignPropertyColorBox!

    @IBOutlet weak var iconBoxHeight_spacing: NSLayoutConstraint!
    @IBOutlet weak var iconBoxWidth_spacing: NSLayoutConstraint!
    
    var propertySelectorType: PropertySelectorType = .Icon_Btn
    
    var _fillColor: NSColor = .clear
    
    init(withType type: PropertySelectorType) {
        super.init(nibName: "KMDesignPropertySelector", bundle: nil)
        self.propertySelectorType = type
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        
        if (propertySelectorType == .Icon_Btn) {
            self.mainBox.contentView = iconView
            
            self.propertySelector(bg: "property-selector.icon-btn.bg.norm")
            self.propertySelector(bg: "property-selector.icon-btn.bg.hov", state: .Hov)
            self.propertySelector(bg: "property-selector.icon-btn.bg.sel", state: .Sel)
        } else if (propertySelectorType == .Color_Icon_Btn) {
            self.mainBox.contentView = colorIconView
            self.iconBox.cornerRadius = iconBoxWidth_spacing.constant/2
            self.iconBox.borderWidth = 1.0
            self.iconBox.borderColor = NSColor(hex: "#000000", alpha: 0.1)
            
            self.propertySelector(bg: "property-selector.icon-btn.bg.norm")
            self.propertySelector(bg: "property-selector.icon-btn.bg.hov", state: .Hov)
            self.propertySelector(bg: "property-selector.icon-btn.bg.sel", state: .Sel)
        }
        
        mainBox.move =  { [weak self](mouseEntered: Bool) -> Void in
            if mouseEntered {
                if self!.state != .Sel && self!.canHover && (self!.state != .Disabled) {
                    self!.state = .Hov
                }
            } else {
                if self!.state != .Sel && self!.canHover && (self!.state != .Disabled) {
                    self!.state = .Norm
                }
            }
        }
    }
    
    // MARK: Get、Set

    var state: KMDesignTokenState {
        get {
            return _state
        }
        set {
            _state = newValue
            updateUI()
        }
    }
    
    var fillColor: NSColor {
        get {
            return _fillColor
        }
        set {
            _fillColor = newValue
            self.iconBox.fillColor = _fillColor
            if _fillColor == NSColor.clear {
                self.iconBox.isClear = true
            } else {
                self.iconBox.isClear = false
            }
        }
    }
    
    var image: NSImage {
        get {
            if (_image == nil) {
                _image = NSImage(named: "KMFileIcon")!
            }
            return _image
        }
        set {
            _image = newValue
            icon.image = _image
        }
    }
    
    var image_hov: NSImage {
        get {
            if (_image_hov == nil) {
                _image_hov = image
            }
            return _image_hov!
        }
        set {
            _image_hov = newValue
            icon.image = _image_hov
        }
    }
    
    var image_sel: NSImage {
        get {
            if (_image_sel == nil) {
                _image_sel = image
            }
            return _image_sel!
        }
        set {
            _image_sel = newValue
            icon.image = _image_sel
        }
    }

    var action: Selector {
        get {
            return _action!
        }
        set {
            _action = newValue
            if _action != nil {
                button.action = _action
            }
        }
    }
    
    var target: AnyObject {
        get {
            return _target!
        }
        set {
            _target = newValue
            if _target != nil {
                button.target = _target
            }
        }
    }
    
    // MARK: Private Methods

    func updateUI() -> Void {
        if (state == .Norm) {
            self.mainBox.fillColor = .clear
            self.mainBox.borderWidth = 0.0
            self.icon.image = self.image
        } else if (state == .Hov) {
            self.mainBox.fillColor = self.background_hov
            self.mainBox.borderWidth = 0.0
            self.mainBox.cornerRadius = CGFloat(self.cornerRadius_hov)
            self.icon.image = self.image_hov
        } else if (state == .Sel) {
            self.mainBox.fillColor = self.background_sel
            self.mainBox.borderWidth = 0.0
            self.mainBox.cornerRadius = CGFloat(self.cornerRadius_hov)
            self.mainBox.borderColor = self.borderColor_sel
            self.icon.image = self.image_sel
        }
    }
}