//
//  KMBatchOperateRemoveHeaderFooterViewController.swift
//  PDF Reader Pro
//
//  Created by tangchao on 2023/11/6.
//

import Cocoa

class KMBatchOperateRemoveHeaderFooterViewController: KMBatchOperateBaseViewController {
    
    var isBates = false
    
    @IBOutlet var actionButton: NSButton!
    private var _haveFiles = false

    deinit {
        NotificationCenter.default.removeObserver(self)
        DistributedNotificationCenter.default().removeObserver(self)
        KMPrint("KMBatchOperateRemoveHeaderFooterViewController deinit.")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self._localizedlanguage()
        self._configuiUI()
        DistributedNotificationCenter.default.addObserver(self, selector: #selector(_themeChanged), name: Notification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: Notification.Name(rawValue: "KMBatchFilesCountNotification"), object: nil)
    }
    
    override var interfaceStatus: KMBatchOperateInterfaceStatus? {
        set {
            if (newValue == .PrepareProcess) {
                super.interfaceStatus = newValue
                DispatchQueue.main.asyncAfter(deadline: .now()+0.4) {
                    var files: [URL] = []
                    for url in self.successFilePathURLArray ?? [] {
                        if (FileManager.default.fileExists(atPath: url.path)) {
                            files.append(url)
                        }
                    }
                    if(files.count > 0) {
                        let workspace = NSWorkspace.shared
                        workspace.activateFileViewerSelecting(files)
                    }
                }
                self.actionButton.tag = 1
                if (self.isBates) {
                    self.actionButton.title = KMLocalizedString("Remove Bates Numbers", nil)
                } else {
                    self.actionButton.title = KMLocalizedString("Remove Header & Footer", nil)
                }
                
                self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
            } else {
                self.actionButton.tag = 0
                self.actionButton.title = KMLocalizedString("Cancel", nil)
                self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
            }
        }
        get {
            return super.interfaceStatus
        }
    }
    
    @IBAction func buttonClicked_Action(_ sender: NSButton) {
        if (!self._haveFiles) {
            return
        }

        if (sender.tag == 1) {
            self.beginBatchOperation()
        } else if (sender.tag == 0) {
            self.cancelBatchOperation()
        }
    }
    @objc func batchFilesCountNotification(notification: Notification) {
        let arr: Array? = notification.object as? [KMBatchOperateFile]
        self.files? = arr ?? []
        if arr?.count ?? 0 > 0{
            self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
            self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
        }else {
            self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
            self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
        }
    }
}

// MARK: - Private Methods

extension KMBatchOperateRemoveHeaderFooterViewController {
    private func _localizedlanguage() {
        if (self.isBates) {
            self.actionButton.title = KMLocalizedString("Remove Bates Numbers", nil)
        } else {
            self.actionButton.title = KMLocalizedString("Remove Header & Footer", nil)
        }
    }
    
    private func _configuiUI() {
        self.view.wantsLayer = true
        self.actionButton.wantsLayer = true
        if let cnt = self.files?.count, cnt > 0 {
            self._haveFiles = true
        } else {
            self._haveFiles = false
        }
        self.actionButton.layer?.cornerRadius = 1.0
        self._updateViewColor()
        
        
        NotificationCenter.default.addObserver(self, selector: #selector(_batchFilesCountNotification), name: Notification.Name("KMBatchFilesCountNotification"), object: nil)
    }
    
    private func _updateViewColor() {
        if(KMAppearance.isDarkMode()){
            self.view.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
        } else {
            self.view.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1).cgColor
        }
        self._updateBtnColor()
    }
    
    @objc private func _batchFilesCountNotification(_ notification: Notification) {
        let files = notification.object as? NSArray
        
        if let cnt = files?.count, cnt > 0 {
            self._haveFiles = true
        } else {
            self._haveFiles = false
        }
        self._updateBtnColor()
    }
    
    private func _updateBtnColor() {
        if(KMAppearance.isDarkMode()){
            if (self._haveFiles) {
                self.actionButton.layer?.backgroundColor = NSColor(red: 0.306, green: 0.498, blue: 0.859, alpha: 1).cgColor
                self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
            } else {
                self.actionButton.layer?.backgroundColor = NSColor(red: 0.306, green: 0.498, blue: 0.859, alpha: 0.6).cgColor
                self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
            }
        }else {
            if (self._haveFiles) {
                self.actionButton.layer?.backgroundColor = NSColor(red: 0.153, green: 0.235, blue: 0.384, alpha: 1).cgColor
                self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
            } else {
                self.actionButton.layer?.backgroundColor = NSColor(red: 0.153, green: 0.235, blue: 0.384, alpha: 0.6).cgColor
                self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
            }
        }
    }
    
    @objc private func _themeChanged(_ notification: NSNotification) {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
            self._updateViewColor()
        }
    }
}

//extension KMBatchOperateRemoveHeaderFooterViewController: KMBatchOperateProtocol {
    
//}