//
//  KMSecurityWindowController.swift
//  PDF Reader Pro
//
//  Created by lizhe on 2023/11/13.
//

import Cocoa

typealias KMSecurityWindowControllerDoneAction = (_ controller: NSWindowController, _ option: [CPDFDocumentWriteOption : Any], _ attribute: [CPDFDocumentAttribute: Any]) -> Void

class KMSecurityWindowController: KMBaseWindowController {
    @IBOutlet weak var securityView: KMSecurityView!
    
    var batchAction: KMBatchActionBlock?
    var doneAction: KMSecurityWindowControllerDoneAction?
    
    
    var documentURL: URL? //{
//        didSet{
//            if self.password.count != 0 {
//                self.securityView.documentURL = self.documentURL
//            } else {
//                KMBaseWindowController.checkPassword(url: documentURL, type: .owner) { [unowned self] success, paasswordString in
//                    if success {
//                        self.securityView.documentURL = self.documentURL
//                    } else {
//                        guard let callBack = self.itemClick else { return }
//                        
//                        callBack()
//                    }
//                }
//            }
//        }
//    }
    
    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.
        securityView.cancelAction = { [unowned self] view in
            self.cancelAction?(self)
        }
        
        securityView.batchAction = { [unowned self] view, files in
            if files.count == 0 {
                let file: KMFileAttribute = KMFileAttribute()
                file.filePath = self.documentURL?.path ?? self.pdfDocument?.documentURL.path ?? ""
                self.batchAction?(self, [file])
            } else {
                self.batchAction?(self, files)
            }
        }
        
        securityView.doneAction = { [weak self] view, model, files in
            self?.setPassword(model: model, files: files)
        }
    }
}

extension KMSecurityWindowController {
    func setPassword(model: KMSecureEncryptModel, files: [KMFileAttribute]) {
        if pdfDocument == nil && documentURL == nil{
            guard let filePath = files.first?.filePath else { return }
        }
        
//        let myDocument = CPDFDocument(url: NSURL(fileURLWithPath: filePath) as URL)
        if (pdfDocument != nil || documentURL != nil) {
            var options: [CPDFDocumentWriteOption : Any] = [:]
            var attribute: [CPDFDocumentAttribute : Any] = [:]
            if model.openPasswordOn && model.ownerPasswordOn { ///  开启密码 & 权限密码
                if (!model.openPassword.isEmpty) {
                    options.updateValue(model.openPassword, forKey: .userPasswordOption)
                }
                
                if (!model.ownerPassword.isEmpty) {
                    options.updateValue(model.ownerPassword, forKey: .ownerPasswordOption)
                }
                
                /// 允许打印
                if model.printEnabled {
                    if model.printAllowed == false {
                        options.updateValue(false, forKey: .allowsPrintingOption)
                    } else {
                        options.updateValue(true, forKey: .allowsPrintingOption)
                        if model.printSelectedIndex == 2 {
                            options.updateValue(true, forKey: .allowsHighQualityPrintingOption)
                        }
                    }
                }
                
                /// 允许更改
                if model.editEnabled {
                    if model.editSelectedIndex == 1 {
                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
                        options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
                    } else if model.editSelectedIndex == 2 {
                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
                        options.updateValue(true, forKey: .allowsFormFieldEntryOption)
                    } else if model.editSelectedIndex == 3 {
                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
                        options.updateValue(true, forKey: .allowsFormFieldEntryOption)
                        options.updateValue(true, forKey: .allowsCommentingOption)
                    } else if model.editAllowed == true {
                        options.updateValue(true, forKey: .allowsCopyingOption)
                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
                        options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
                        options.updateValue(true, forKey: .allowsCommentingOption)
                        options.updateValue(true, forKey: .allowsFormFieldEntryOption)
                    } else {
                        options.updateValue(false, forKey: .allowsCopyingOption)
                    }
                }
                /// 加密层级 sdk 缺接口
            } else if model.openPasswordOn { /// 开启密码
                if (!model.openPassword.isEmpty) {
                    options.updateValue(model.openPassword, forKey: .userPasswordOption)
                }
                
                /// 加密层级 sdk 缺接口
            } else if model.ownerPasswordOn { /// 权限密码
                if (!model.ownerPassword.isEmpty) {
                    options.updateValue(model.ownerPassword, forKey: .ownerPasswordOption)
                }
                
                /// 允许打印
                if model.printEnabled {
                    if model.printAllowed == false {
                        options.updateValue(false, forKey: .allowsPrintingOption)
                    } else {
                        options.updateValue(true, forKey: .allowsPrintingOption)
                        if model.printSelectedIndex == 2 {
                            options.updateValue(true, forKey: .allowsHighQualityPrintingOption)
                        }
                    }
                }
                
                /// 允许更改
                if model.editEnabled {
                    if model.editSelectedIndex == 1 {
                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
                        options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
                    } else if model.editSelectedIndex == 2 {
                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
                        options.updateValue(true, forKey: .allowsFormFieldEntryOption)
                    } else if model.editSelectedIndex == 3 {
                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
                        options.updateValue(true, forKey: .allowsFormFieldEntryOption)
                        options.updateValue(true, forKey: .allowsCommentingOption)
                    } else if model.editAllowed == true {
                        options.updateValue(true, forKey: .allowsCopyingOption)
                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
                        options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
                        options.updateValue(true, forKey: .allowsCommentingOption)
                        options.updateValue(true, forKey: .allowsFormFieldEntryOption)
                    } else {
                        options.updateValue(false, forKey: .allowsCopyingOption)
                    }
                }
                
                /// 加密层级 sdk 缺接口
            }
            
            attribute.updateValue(model.title, forKey: .titleAttribute)
            attribute.updateValue(model.author, forKey: .authorAttribute)
            attribute.updateValue(model.subject, forKey: .subjectAttribute)
            attribute.updateValue(model.keywords, forKey: .keywordsAttribute)
            
            //            let result = myDocument.write(to: documentURL, withOptions: options)
            //            myDocument.setPasswordOptions(options)
            guard let callback = doneAction else {
                return
            }
            
            callback(self, options, attribute)
        }
    }
}