//
//  KMEraserController.swift
//  PDF Reader Pro
//
//  Created by Niehaoyu on 2024/11/26.
//

import Cocoa
import KMComponentLibrary

class KMEraserController: NSViewController {
    
    @IBOutlet var eraserBGView: NSView!
    @IBOutlet var eraserLabel: NSTextField!
    @IBOutlet var eraserSlider: ComponentSlider!
    @IBOutlet var eraserSelect: ComponentSelect!
    
    //MARK: - func
    override func viewDidAppear() {
        super.viewDidAppear()
         
        eraserSlider.reloadData()
         
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        
        setupProperty()
    }
    
    func setupProperty() {
         
        eraserLabel.stringValue = KMLocalizedString("Line")
        eraserLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
        eraserLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
        
        eraserSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
        eraserSlider.delegate = self
        
        eraserSelect.properties = ComponentSelectProperties(size: .s,
                                                               state: .normal,
                                                               creatable: true,
                                                               text: "2",
                                                               textUnit: " pt",
                                                               regexString: "0123456789 pt")
        if true {
            var opacityItems: [ComponentMenuitemProperty] = []
            for string in ["1 pt", "3 pt", "6 pt", "9 pt", "12 pt", "15 pt", "18 pt"] {
                let item = ComponentMenuitemProperty(type: .normal, text: string)
                opacityItems.append(item)
            }
            eraserSelect.updateMenuItemsArr(opacityItems)
        }
        eraserSelect.delegate = self
        
    }
    
    
    func reloadData() {
        
        
    }
}

//MARK: - ComponentSliderDelegate
extension KMEraserController: ComponentSliderDelegate {
    func componentSliderDidUpdate(_ view: ComponentSlider) {
        if view == eraserSlider {
            let percent = view.properties.percent * 17 + 1
            CPDFAnnotationConfig.setDefaultFloatValue(percent, toKey: CEraserkNoteLineWidthKey)
        }
        reloadData()
    }
}

//MARK: - ComponentSelectDelegate
extension KMEraserController: ComponentSelectDelegate {
    func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
        if let result = menuItemProperty?.text {
            let result = result.stringByDeleteCharString(view?.properties.textUnit ?? "")
            var value = result.stringToCGFloat()
            if value > 18 {
                value = 18
            } else if value < 1 {
                value = 1
            }
            CPDFAnnotationConfig.setDefaultFloatValue(value, toKey: CEraserkNoteLineWidthKey)
            
        }
    }
    
    func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
        if let result = text {
            if view == eraserSelect {
                var value = result.stringToCGFloat()
                if value > 18 {
                    value = 18
                } else if value < 1 {
                    value = 1
                }
                CPDFAnnotationConfig.setDefaultFloatValue(value, toKey: CEraserkNoteLineWidthKey)
            }
            reloadData()
        }
    }
}