//
//  KMHeaderFooterDateInfoView.swift
//  PDF Reader Pro
//
//  Created by tangchao on 2022/12/27.
//

import Cocoa

class KMHeaderFooterDateInfoView: KMHeaderFooterAdjectiveInfoBaseView {

    var comboBox = NSComboBox()
    
    override func initSubviews() {
        super.initSubviews()
        
        self.addSubview(self.comboBox)
        
        self.titleLabel.stringValue = NSLocalizedString("Date:", comment: "")
        
        self.comboBox.addItems(withObjectValues: KMWatermarkAdjectiveTools.getDateFormats())
        self.comboBox.isEditable = false
        self.comboBox.delegate = self
    }
    
    override func layout() {
        super.layout()
        
        self.comboBox.frame = NSMakeRect(self.contentInset.left, self.titleLabel.frame.maxY+10, NSWidth(self.bounds)-self.contentInset.left-self.contentInset.right, 22)
    }
    
    override var model: KMHeaderFooterAdjectiveModel {
        get {
            return super.model
        }
        set {
            super.model = newValue
            
            let myModel: KMHeaderFooterModel = newValue as! KMHeaderFooterModel
            if (myModel.dateFormatString.isEmpty) {
                self.comboBox.stringValue = KMWatermarkAdjectiveTools.getDateFormats().first!
            } else {
                self.comboBox.stringValue = myModel.dateFormatString
            }
        }
    }
}

extension KMHeaderFooterDateInfoView: NSComboBoxDelegate {
    func comboBoxSelectionDidChange(_ notification: Notification) {
        if (self.comboBox.isEqual(to: notification.object)) {
            guard let callback = self.itemClick else {
                return
            }
            
            var index: Int = self.comboBox.indexOfSelectedItem
            if (index < 0) {
                index = 0
            }
            
            callback(1, KMWatermarkAdjectiveTools.getDateFormats()[index])
        }
    }
}