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

import Cocoa

typealias KMCreateHeaderFooterModelDidChange = (_ model: KMHeaderFooterObject) -> ()
class KMCreateHeaderFooterController: KMWatermarkAdjectivePropertyBaseController {
    
    /// 是否是编辑类型
    var isEdit: Bool = false
    /// 是否已编辑
    var isEdited: Bool = false
    
    var modelDidChange: KMCreateHeaderFooterModelDidChange!
    
    override func viewWillAppear() {
        super.viewWillAppear()
        
        let controller: KMHeaderFooterPropertyInfoController = childViewController[0] as! KMHeaderFooterPropertyInfoController
        controller.model = self.model as! KMHeaderFooterObject
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.backButtton.title = NSLocalizedString("", comment: "")
        self.showBack(show: true)
        if (self.isEdit) {
            self.addButton.attributedTitle = NSAttributedString(string: NSLocalizedString("", comment: ""), attributes: [NSAttributedString.Key.foregroundColor : NSColor(red: 24.0/255.0, green: 160.0/255.0, blue: 251.0/255.0, alpha: 1.0),NSAttributedString.Key.font : NSFont.systemFont(ofSize: 12), NSAttributedString.Key.underlineStyle : true])
            self.titleLabel.stringValue = NSLocalizedString("Edit HeaderFoote", comment: "")
        } else {
            self.titleLabel.stringValue = NSLocalizedString("Add HeaderFooter", comment: "")
            self.addButton.attributedTitle = NSAttributedString(string: NSLocalizedString("", comment: ""), attributes: [NSAttributedString.Key.foregroundColor : NSColor(red: 24.0/255.0, green: 160.0/255.0, blue: 251.0/255.0, alpha: 1.0),NSAttributedString.Key.font : NSFont.systemFont(ofSize: 12), NSAttributedString.Key.underlineStyle : true])
        }
        

        self.segementControl.isHidden = true
        self.segementControlHeightConst.constant = 0
        
        let item = NSTabViewItem(identifier: "tabID")
        item.label = "tab1";
        let controller = KMHeaderFooterPropertyInfoController()
        item.viewController = controller
        controller.delegate = self

        self.tabView.addTabViewItem(item)

        childViewController.append(controller)
    }
    
    override func resume() {
        if (self.needResume() == false) {
            return
        }
        
        (childViewController[0] as! KMHeaderFooterPropertyInfoController).model = self.flagModel?.copy() as! KMHeaderFooterObject
    }
}

extension KMCreateHeaderFooterController: KMWatermarkAdjectivePropertyDelegate {
    func propertyInfoDidChange(model: AnyObject) {
        self.isEdited = true
        
        guard let callback = modelDidChange else {
            return
        }
        
        callback(model as! KMHeaderFooterObject)
    }
}