//
//  KMTextfieldVC.swift
//  PDF Reader Pro
//
//  Created by wanjun on 2023/1/31.
//

import Cocoa

protocol KMTextfieldVCDelegate: NSObjectProtocol {
    func km_controlTextDidEndEditing(_ obj: KMTextfieldVC)
    func km_controlTextDidChange(_ obj: KMTextfieldVC)
}

class KMTextfieldCell: NSTextFieldCell {
    var borderThickness: CGFloat = 1
    var offset: CGFloat = 8.0

    override func drawingRect(forBounds theRect: NSRect) -> NSRect {
        var newRect:NSRect = super.drawingRect(forBounds: theRect)
        let textSize:NSSize = self.cellSize(forBounds: theRect)
        let heightDelta:CGFloat = newRect.size.height - textSize.height
        if heightDelta > 0 {
            newRect.size.height = textSize.height
            newRect.origin.y += heightDelta * 0.5
        } else {
            newRect.size.height = textSize.height
            newRect.origin.y += heightDelta
        }
        newRect.origin.x += offset
        newRect.size.width = theRect.width - offset*2
        return newRect
    }
    
    override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
        // Area that covers the NSTextField itself. That is the total height minus our custom border size.
        let interiorFrame = NSRect(x: 0, y: 0, width: cellFrame.width, height: cellFrame.height - borderThickness)

        let path = NSBezierPath()
        path.lineWidth = borderThickness
        // Line width is at the center of the line.
        path.move(to: NSPoint(x: 0, y: cellFrame.height))
        path.line(to: NSPoint(x: cellFrame.width, y: cellFrame.height))
        path.line(to: NSPoint(x: cellFrame.width, y: 0))
        path.line(to: NSPoint(x: 0, y: 0))
        NSColor.clear.setStroke()
        path.stroke()

        // Pass in area minus the border thickness in the height
        drawInterior(withFrame: interiorFrame, in: controlView)
    }
}

class KMTextfieldVC: NSViewController {

    @IBOutlet weak var mainBox: NSBox!
    @IBOutlet weak var textField: NSTextField!
    
    @IBOutlet weak var mainBoxHeight: NSLayoutConstraint!
    @IBOutlet weak var mainBoxWidth: NSLayoutConstraint!

    var height: Float = 32.0    // 高度
    var width: Float = 400.0    // 宽度
    var textColor: NSColor = .black // 内容颜色
    var font: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
    var background: NSColor = .clear// 背景颜色
    var borderWidth: Float = 0.0// 边框宽度
    var borderColor: NSColor = .clear// 边框颜色
    var cornerRadius: Float = 0.0// 边框圆角
    var stringValue: String = ""// 内容
    var placeholderString: String = ""// 内容预设值
    var editable: Bool = true //是否允许编辑
    var becomeFirstResponder: Bool = false // 是否为第一响应者
    open weak var delete: KMTextfieldVCDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        
        updateUI()
    }
    
    // MARK: Private Methods
    
    func updateUI() -> Void {
//        textField.stringValue = stringValue
        textField.placeholderString = placeholderString
        textField.textColor = textColor
        textField.font = font
        textField.isEditable = editable
//        if becomeFirstResponder {
//            textField.becomeFirstResponder()
//        }

        mainBox.fillColor = background
        mainBox.borderWidth = CGFloat(borderWidth)
        mainBox.borderColor = borderColor
        mainBox.cornerRadius = CGFloat(cornerRadius)
        
        mainBoxHeight.constant = CGFloat(height)
        mainBoxWidth.constant = CGFloat(width)
    }
}

extension KMTextfieldVC: NSTextFieldDelegate {
    func controlTextDidEndEditing(_ obj: Notification) {
        let object = obj.object as! NSTextField
        stringValue = object.stringValue
        
        self.delete?.km_controlTextDidEndEditing(self)
    }
    
    func controlTextDidChange(_ obj: Notification) {
        let object = obj.object as! NSTextField
        stringValue = object.stringValue

        self.delete?.km_controlTextDidChange(self)
    }

}