//
//  KMHistoryFileDeleteWindowController.swift
//  PDF Master
//
//  Created by wanjun on 2022/11/8.
//

import Cocoa

typealias historyFileDeleteCallback = (_ indexPaths: [URL], _ windowController: KMHistoryFileDeleteWindowController) -> Void

class KMHistoryFileDeleteWindowController: NSWindowController {

    @IBOutlet weak var deleteOptionsLabel: NSTextField!
    @IBOutlet weak var noLongerPromptView: NSView!
    @IBOutlet weak var cancelBox: NSBox!
    @IBOutlet weak var deleteBox: NSBox!
    
    var cancelVC: KMDesignButton!
    var deleteVC: KMDesignButton!
    var noLongerPromptVC: KMDesignButton!
        
    var deleteCallback: historyFileDeleteCallback?
    var indexPaths: [URL] = []
    
    override func windowDidLoad() {
        super.windowDidLoad()
        
        self.cancelVC = KMDesignButton.init(withType: .Text)
        self.deleteVC = KMDesignButton.init(withType: .Text)
        self.noLongerPromptVC = KMDesignButton.init(withType: .CheckBox)
        
        initLocalization()
        initializeUI()
    }
    
    // MARK: Init
    
    func initializeUI() {
        self.window?.backgroundColor = .white
        deleteOptionsLabel.font = NSFont(name: "PingFangSC-Semibold", size: 15)
        deleteOptionsLabel.textColor = .black
        
        cancelBox.fillColor = NSColor.km_init(hex: "#F5F5F5")
        cancelBox.cornerRadius = 5.0
        cancelBox.contentView = cancelVC.view
        cancelVC.target = self
        cancelVC.action = #selector(cancelAction(_:))
        cancelVC.updateUI()
        
        deleteBox.fillColor = .clear
        deleteBox.contentView = deleteVC.view
        deleteVC.target = self
        deleteVC.action = #selector(deleteAction(_:))
        deleteVC.button(type: .Cta, size: .m)
        
        noLongerPromptView.addSubview(noLongerPromptVC.view)
        noLongerPromptVC.view.mas_makeConstraints { (make) in
            make?.top.bottom().offset()(0)
            make?.centerX.offset()(0)
        }
        noLongerPromptVC.target = self
        noLongerPromptVC.action = #selector(deleteHistoryFileAction(_:))
        noLongerPromptVC.checkbox_radio()
    }
    
    func initLocalization() {
        let firstPath = indexPaths.first?.path.lastPathComponent ?? ""
//        deleteOptionsLabel.stringValue = String(format: NSLocalizedString("Remove\"%@\"from your Recent Files?", comment: ""), firstPath)
        let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
        if urls.count == indexPaths.count {
            deleteOptionsLabel.stringValue = NSLocalizedString("Clear All Recents", comment: "")
        } else {
            deleteOptionsLabel.stringValue = NSLocalizedString("Delete selected file from the recents", comment: "")
        }

        noLongerPromptVC.stringValue = NSLocalizedString("Don't ask again", comment: "")
        cancelVC.stringValue = NSLocalizedString("Cancel", comment: "")
        deleteVC.stringValue = NSLocalizedString("Delete", comment: "")
    }
    
    // MARK: Private Methods
        
    @objc func cancelAction(_ sender: NSButton) -> Void {
        NSApp.mainWindow!.endSheet(self.window!)
        self.window?.orderOut(self)
    }
    
    @objc func deleteAction(_ sender: NSButton) -> Void {
        if let callback = deleteCallback {
            callback(indexPaths, self)
        }
        NSApp.mainWindow!.endSheet(self.window!)
        self.window?.orderOut(self)
    }
    
    // MARK: Action Methods
    
    
    @IBAction func deleteHistoryFileAction(_ sender: NSButton) {
        if noLongerPromptVC.state != .Checked {
            noLongerPromptVC.state = .Checked
            UserDefaults.standard.setValue(true, forKey: "kHistoryDeleteNOReminderKey")
        } else {
            noLongerPromptVC.state = .Norm
            UserDefaults.standard.setValue(false, forKey: "kHistoryDeleteNOReminderKey")
        }
        UserDefaults.standard.synchronize()
    }
}