//
//  CDistanceMeasureInfoWindowController.swift
//  Cisdem PDFMaster
//
//  Created by wanjun on 2024/7/16.
//

import Cocoa

protocol CDistanceMeasureInfoWindowControllerDelegate: AnyObject {
    func distanceMeasureInfoWindowControllerSetting(_ distanceMeasureInfoWindowController: CDistanceMeasureInfoWindowController)
    func cancelMeasureInfoWindowControllerSetting(_ distanceMeasureInfoWindowController: CDistanceMeasureInfoWindowController)
}

@objcMembers
class CDistanceMeasureInfoWindowController: NSWindowController {
    
    @IBOutlet weak var titleLabel: NSTextField!
    @IBOutlet weak var scaleTitleLabel: NSTextField!
    @IBOutlet weak var lengthTitleLabel: NSTextField!
    @IBOutlet weak var precisionTitleLabel: NSTextField!
    @IBOutlet weak var angleTitleLabel: NSTextField!
    @IBOutlet weak var xTitleLabel: NSTextField!
    @IBOutlet weak var yTitleLabel: NSTextField!

    @IBOutlet weak var scaleLabel: NSTextField!
    @IBOutlet weak var lengthLabel: NSTextField!
    @IBOutlet weak var precisionLabel: NSTextField!
    @IBOutlet weak var angleLabel: NSTextField!
    @IBOutlet weak var xLabel: NSTextField!
    @IBOutlet weak var yLabel: NSTextField!
    @IBOutlet weak var cancelButton: NSButton!
    @IBOutlet weak var settingsButton: NSButton!

    weak var delegate: CDistanceMeasureInfoWindowControllerDelegate?
    
    var measureInfo: CPDFDistanceMeasureInfo = CPDFDistanceMeasureInfo()
    
    convenience init() {
        self.init(windowNibName: "CDistanceMeasureInfoWindowController")
    }
    
    override func windowDidLoad() {
        super.windowDidLoad()
        
        cancelButton.stringValue = NSLocalizedString("Cancel", comment: "")

        self.window?.level = .floating
        commonInit()
        configurationLanguage()
    }
    
    private func configurationLanguage() {
        titleLabel.stringValue = NSLocalizedString("Distance Measurement", comment: "")

        scaleTitleLabel.stringValue = NSLocalizedString("Scale:", comment: "")
        lengthTitleLabel.stringValue = NSLocalizedString("Distance", comment: "") + ":"
        precisionTitleLabel.stringValue = NSLocalizedString("Precision", comment: "") + ":"
        angleTitleLabel.stringValue = NSLocalizedString("Angle", comment: "") + ":"
        xTitleLabel.stringValue = NSLocalizedString("X", comment: "") + ":"
        yTitleLabel.stringValue = NSLocalizedString("Y", comment: "") + ""
        settingsButton.title = NSLocalizedString("Settings", comment: "")
    }
    
    func hideFloatingWindow() {
        self.window?.orderOut(nil)
    }
    
    func reloadData(with measureInfo: CPDFDistanceMeasureInfo) {
        self.measureInfo = measureInfo
        updateLabels()
    }
    
    func clearData() {
        self.lengthLabel.stringValue = ""
        self.angleLabel.stringValue = ""
        self.xLabel.stringValue = ""
        self.yLabel.stringValue = ""
        
        self.measureInfo = CPDFDistanceMeasureInfo()
        
        updateLabels()
    }
        
    private func commonInit() {
        updateLabels()
    }
    
    private func updateLabels() {
        scaleLabel.stringValue = String(format: "%0.0f %@ = %0.0f %@", measureInfo.rulerBase, measureInfo.rulerBaseUnit, measureInfo.rulerTranslate, measureInfo.rulerTranslateUnit)
        
        switch measureInfo.precision {
        case 1:
            precisionLabel.stringValue = "1"
        case 10:
            precisionLabel.stringValue = "0.1"
        case 100:
            precisionLabel.stringValue = "0.01"
        case 1000:
            precisionLabel.stringValue = "0.001"
        case 10000:
            precisionLabel.stringValue = "0.0001"
        default:
            precisionLabel.stringValue = ""
        }
        
        let formatValue = measureInfo.formatValue
        if formatValue.count > 0 {
            let range = formatValue.index(formatValue.startIndex, offsetBy: 4)..<formatValue.endIndex
            lengthLabel.stringValue = String(formatValue[range])
        }
    }
    
    // MARK: Action
    
    @IBAction func buttonItemClick_Setting(_ sender: Any) {
        delegate?.distanceMeasureInfoWindowControllerSetting(self)
    }

    @IBAction func buttonItemClick_Cancel(_ sender: Any) {
        delegate?.cancelMeasureInfoWindowControllerSetting(self)
    }
}