//
//  KMComboBox.swift
//  PDF Master
//
//  Created by wanjun on 2022/11/29.
//

import Cocoa

enum KMComboBoxType : Int {
    case none = 0
}

class KMComboBox: NSComboBox {
    
    var comboxRect: CGRect = CGRect(x: 0, y: 0, width: 0, height: 0)
    var _type: KMComboBoxType = .none

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        switch type {
        case .none:
            var p = comboxRect.origin
            p.x += comboxRect.width - 20
            p.y += (comboxRect.height - 16) / 2

            NSColor.clear.setFill()
            NSRect(x: p.x, y: 0, width: 16.0, height: comboxRect.height).fill()

            let image = NSImage(named: "KMImageNameUXIconBtnArrowDown")
            var rect = NSRect.zero
            rect.size = CGSize(width: 16.0, height: 16.0)
            image!.draw(in: NSRect(x: p.x, y: p.y, width: 16.0, height: 16.0), from: rect, operation: .sourceOver, fraction: 1.0, respectFlipped: true, hints: nil)
        default:
            break
        }
    }
    
    // MARK: Private Methods
    
    var type: KMComboBoxType {
        get {
            return _type
        }
        set {
            _type = newValue
            switch newValue {
            case .none:
                self.wantsLayer = true
                self.layer?.cornerRadius = 1.0
                self.layer?.borderWidth = 0.5
                self.layer?.borderColor = .black
                self.backgroundColor = .white
                self.isButtonBordered = false
            default:
                break
            }
        }
    }
}

class KMComboBoxCell: NSComboBoxCell {
    private func adjustedFrameToVerticallyCenterText(_ frame: NSRect) -> NSRect {
        let offset = floor((frame.size.height / 2 - (font!.ascender + font!.descender)))
        let rect = NSRect(x: 8, y: 0, width: frame.size.width, height: frame.size.height)
        return NSInsetRect(rect, 0.0, offset)
    }
    
    override func edit(withFrame aRect: NSRect, in controlView: NSView, editor textObj: NSText, delegate anObject: Any?, event: NSEvent?) {
        super.edit(withFrame: adjustedFrameToVerticallyCenterText(aRect), in: controlView, editor: textObj, delegate: anObject, event: event)
    }
    
    override func select(withFrame aRect: NSRect, in controlView: NSView, editor textObj: NSText, delegate anObject: Any?, start selStart: Int, length selLength: Int) {
        super.select(withFrame: adjustedFrameToVerticallyCenterText(aRect), in: controlView, editor: textObj, delegate: anObject, start: selStart, length: selLength)
    }
    
    override func drawInterior(withFrame frame: NSRect, in controlView: NSView) {
        super.drawInterior(withFrame: adjustedFrameToVerticallyCenterText(frame), in: controlView)
    }
}

class KMStampComboBoxCell: NSComboBoxCell {
    override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
        self.backgroundColor = NSColor.white
        super.drawInterior(withFrame: adjustedFrameToVerticallyCenterText(cellFrame), in: controlView)
    }
    
    func adjustedFrameToVerticallyCenterText(_ frame: NSRect) -> NSRect {
        let offset = floor((NSHeight(frame) / 2 - (font!.ascender + font!.descender)))
        return frame.insetBy(dx: 0.0, dy: offset)
    }
}