//
//  KMBatchRemoveBackgroundOperation.swift
//  EaseUS PDF Editor
//
//  Created by liujiajie on 2023/12/1.
//

import Foundation

class KMBatchRemoveBackgroundOperation: KMBatchOperation{
    var pdfDocument: CPDFDocument?
    
    override init(file: KMBatchOperateFile) {
        super.init(file: file)
       
    }
    override func start() {
        self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: self.operateFile?.filePath ?? ""))
        if let data = self.pdfDocument?.isLocked, data {
            self.pdfDocument?.unlock(withPassword: self.operateFile?.password)
        }
        if !self.isCancelled {
            self.delegate?.fileBeginOperate?(self.operateFile!, info: self.operateFile!.removeBackgroundInfo)
            willChangeValue(forKey: "isExecuting")
            self.hasExcuting = true
            didChangeValue(forKey: "isExecuting")
            if !FileManager.default.fileExists(atPath: self.operateFile!.filePath) {
                self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("File Not Exist", nil)), info: self.operateFile!.removeBackgroundInfo)
                
                self.willChangeValue(forKey: "isFinished")
                self.hasFinished = true
                self.didChangeValue(forKey: "isFinished")
                return
            }

            if self.pdfDocument == nil {
                self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", nil)), info: self.operateFile!.removeBackgroundInfo)
                self.willChangeValue(forKey: "isFinished")
                self.hasFinished = true
                self.didChangeValue(forKey: "isFinished")
                return
            }

            if !self.pdfDocument!.allowsPrinting || !self.pdfDocument!.allowsCopying {
                self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("This is a secured document. Editing is not permitted.", nil)), info: self.operateFile!.removeBackgroundInfo)
                self.willChangeValue(forKey: "isFinished")
                self.hasFinished = true
                self.didChangeValue(forKey: "isFinished")
                return
            }
            KMBatchRemoveBackgroundOperation.saveAsPDFRemoveAllBackground(self.pdfDocument!, password: self.operateFile?.password, toPath: self.operateFile!.removeBackgroundInfo.fetchDestinationFilepath()!) { result in
                if result == 1 {
                    self.delegate?.fileOperateSuccessed?(self.operateFile!, info: self.operateFile!.removeBackgroundInfo)
                } else {
                    self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("Failed", nil)) as NSError, info: self.operateFile!.removeBackgroundInfo)
                }
                self.willChangeValue(forKey: "isFinished")
                self.hasFinished = true
                self.didChangeValue(forKey: "isFinished")
            }
        }else {
            willChangeValue(forKey: "isFinished")
            willChangeValue(forKey: "isExecuting")
            hasExcuting = false
            hasFinished = true
            didChangeValue(forKey: "isExecuting")
            didChangeValue(forKey: "isFinished")
        }
    }
    override func cancel() {
//        super.cancel()
        if isExecuting {
            operateFile!.removeBackgroundInfo.status = .Waiting
            if FileManager.default.fileExists(atPath: operateFile!.removeBackgroundInfo.outPutPath!) {
                try? FileManager.default.removeItem(atPath: operateFile!.removeBackgroundInfo.outPutPath!)
            }
            self.delegate?.fileOperateCanceled?(self.operateFile!, info: self.operateFile!.removeBackgroundInfo)
           
           willChangeValue(forKey: "isFinished")
           hasFinished = true
           didChangeValue(forKey: "isFinished")
       } else {
           willChangeValue(forKey: "isCancelled")
           hasCanceled = true
           didChangeValue(forKey: "isCancelled")
       }
    }
    
    func defaultError() -> Error {
        let errorDict: [String: Any] = [NSLocalizedDescriptionKey: NSLocalizedString("Failed", comment: "")]
        let error = NSError(domain: "PDFReaderBatchErrorDomain", code: -1, userInfo: errorDict)
        return error
    }
    class func saveAsPDFRemoveAllBackground(_ pdfDocument: CPDFDocument, password: String?, toPath path: String, completionHandler handler: @escaping (Int) -> Void) {
        let document = CPDFDocument(url: pdfDocument.documentURL)
        if let password = password {
            document?.unlock(withPassword: password)
        }
        let tBackground = document?.background()
        tBackground?.clear()
        
        let documentPath = NSTemporaryDirectory()
        let tempPath = documentPath.stringByAppendingPathComponent(path.lastPathComponent)
        if FileManager.default.fileExists(atPath: tempPath) {
            try?FileManager.default.removeItem(atPath: tempPath)
        }
        
        let result = document?.write(to: URL(fileURLWithPath: tempPath))
        if result ?? false {
            if FileManager.default.fileExists(atPath: path) {
                try?FileManager.default.removeItem(atPath: path)
            }
            try?FileManager.default.moveItem(atPath: tempPath, toPath: path)
        } else {
            try?FileManager.default.removeItem(atPath: tempPath)
        }
        DispatchQueue.main.async {
            if let result = result {
                handler(result ? 1 : 0)
            }
        }
    }
}