//
//  KMBaseXibView.swift
//  PDF Master
//
//  Created by lizhe on 2023/2/15.
// xib初始化

import Cocoa

class KMBaseXibView: NSView {

    @IBOutlet var contentView: NSView!

    // MARK: 初始化
    public required init?(coder decoder: NSCoder) {
        super.init(coder: decoder)
        self.initContentView()
        self.setup()
        self.updateUI()
        self.updateLanguage()
        self.reloadData()
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.initContentView()
        self.setup()
        self.updateUI()
        self.updateLanguage()
        self.reloadData()
        self.addNotification()
    }
    
    private func initContentView() {
        let isExist = Bundle.main.path(forResource: String(describing:self.classForCoder.self), ofType: "nib")
        if isExist != nil {
            var topLevelArray: NSArray? = nil
            //绑定xib
            let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
                                 bundle: nil)
            if resource != nil {
                if (resource!.instantiate(withOwner: self, topLevelObjects: &topLevelArray)) {
                    for view in topLevelArray! {
                        if view is NSView {
                            contentView = view as? NSView
                            break
                        }
                    }
                }
                
                if contentView == nil {
                    contentView = NSView()
                }
                addSubview(contentView)
                contentView.translatesAutoresizingMaskIntoConstraints = false
                NSLayoutConstraint.activate([
                    contentView.topAnchor.constraint(equalTo: topAnchor),
                    contentView.leftAnchor.constraint(equalTo: leftAnchor),
                    contentView.rightAnchor.constraint(equalTo: rightAnchor),
                    contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
                contentView.updateConstraintsForSubtreeIfNeeded()
            }
        }
    }
    
    func setup() {
        
    }
    
    //刷新界面UI 和 数据
    func reloadData() {

    }
    
    func updateLanguage() {
        
    }
    
    func updateUI() {
        
    }
    
    func resetData() {
        
    }
    
    func addNotification() {
        
    }
    
    func removeNotification() {
        
    }
}