//
//  KMNBaseWindowController.swift
//  PDF Reader Pro
//
//  Created by tangchao on 2023/5/11.
//

import Cocoa

class KMNBaseWindowController: NSWindowController {
        
    public var parentWindow: NSWindow?
    public var handler: ((String?) -> Void)!

    deinit {
        KMPrint(self.className + " deinit.")
        
        self.removeNotification()
    }
    
    override func windowDidLoad() {
        super.windowDidLoad()
    
        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
        self.initContentView()
        self.initNotification()
    }
    
    func initNotification() {
        updateUIThemeColor()
        NotificationCenter.default.addObserver(self, selector: #selector(updateUIThemeColor), name: APPAppearanceChangedNotificationName, object: nil)
        
        updateUILanguage()
        NotificationCenter.default.addObserver(self, selector: #selector(updateUILanguage), name: APPLanguageChangedNotificationName, object: nil)
    }
    
    func removeNotification() {
        DistributedNotificationCenter.default().removeObserver(self)
    }
        
    @objc func updateUIThemeColor() {}
    
    @objc func updateUILanguage() {}
    
    func beginSheetFinish() {}
    
    func initContentView() {}

    func own_beginSheetModal(for window: NSWindow?, completionHandler handler: ((String?) -> Void)?) {
        if window != nil {
            parentWindow = window
            window!.beginSheet(self.window!) { ModalResponse in
                self.handler?(nil)
            }
        }
        self.handler = handler
        
        beginSheetFinish()
    }
    
    func own_closeEndSheet() {
        parentWindow?.endSheet(self.window!)
    }
    
    override func mouseDown(with event: NSEvent) {
        super.mouseDown(with: event)
        
        window?.makeFirstResponder(nil)
    }
}