//
//  KMWatermarkPropertyCreateController.swift
//  PDF Master
//
//  Created by tangchao on 2022/12/16.
//

import Cocoa

class KMWatermarkPropertyCreateController: KMWatermarkAdjectivePropertyBaseController {
    
    var isEdit: Bool = false
    var editType: Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.backButtton.title = NSLocalizedString("", comment: "")
        self.showBack(show: true)
        self.titleLabel.stringValue = NSLocalizedString("Add Watermark", 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.items = [NSLocalizedString("Text", comment: ""),NSLocalizedString("File", comment: "")]
        self.segementControl.itemClick = {
            [self] (index: Int) in
            if (self.isEdit) {
                self.segementControl.selectItemIndex = self.editType
            } else {
                self.tabView.selectTabViewItem(at: index)
            }
        }
        
        if (self.isEdit) {
            self.titleLabel.stringValue = NSLocalizedString("Edit Watermark", 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])
        } else {
            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])
        }
        
        let item = NSTabViewItem(identifier: "tabID")
        item.label = "tab1";
        let controller = KMWatermarkPropertyInfoController(nibName: "KMWatermarkPropertyInfoController", bundle: nil)
        controller.type = .text
        item.viewController = controller
        controller.model = self.model as! KMWatermarkModel
        controller.delegate = self

        let item2 = NSTabViewItem(identifier: "tablID2")
        item2.label = "tab2";
        let controller2 = KMWatermarkPropertyInfoController(nibName: "KMWatermarkPropertyInfoController", bundle: nil)
        controller2.type = .file
        item2.viewController = controller2
        controller2.model = self.model as! KMWatermarkModel
        controller2.delegate = self

        self.tabView.addTabViewItem(item)
        self.tabView.addTabViewItem(item2)
        
        childViewController.append(controller)
        childViewController.append(controller2)
        
        if (self.isEdit) {
            self.segementControl.isHidden = true
            self.segementControlHeightConst.constant = 0
        }
    }
    
    func updateModel(model: KMWatermarkModel, type: Int) {
        self.editType = type
        self.selectTabIndex(index: type)
        
        self.model = model
        var count: Int = 0
        for controller in childViewController {
            let infoController: KMWatermarkPropertyInfoController = controller as! KMWatermarkPropertyInfoController
            if (count == type) {
                infoController.updateModel(model: model)
            }
            count += 1
        }
    }
    
    override func resume() {
        if (self.flagModel == nil) {
            return
        }

        if (childViewController.count <= 0) {
            return
        }
        
        let controller: KMWatermarkPropertyInfoController = childViewController[0] as! KMWatermarkPropertyInfoController
        controller.updateModel(model: self.flagModel!.copy() as! KMWatermarkModel)
    }
}

extension KMWatermarkPropertyCreateController: KMWatermarkAdjectivePropertyDelegate {
    func propertyInfoDidChange(model: AnyObject) {
        if (self.delegate != nil) {
            self.delegate!.propertyInfoDidChange(model: model)
        }
    }
}