//
//  KMBatesPreviewController.swift
//  PDF Reader Pro
//
//  Created by tangchao on 2022/12/28.
//

import Cocoa

/// Bates码 预览控制器
class KMBatesPreviewController: KMWatermarkAdjectivePreViewBaseController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let itemTitles = [[NSLocalizedString("Add Bates Number", comment: ""), NSLocalizedString("Delete Bates Number", comment: "")], [NSLocalizedString("Batch", comment: "")]]
        var itemModels: Array<Array<KMWatermarkAdjectiveTopBarItemModel>> = []
        for items in itemTitles {
            var array: Array<KMWatermarkAdjectiveTopBarItemModel> = []
            for title in items {
                let model = KMWatermarkAdjectiveTopBarItemModel()
                model.iconName = ""
                model.itemTitle = title
                array.append(model)
            }
            itemModels.append(array)
        }
        
        self.topBarView.initItemData(itemArrays: itemModels)
        self.topBarView.selectTopItem(index: 0)
        
        let preView: KMWatermarkPDFView_OC = KMWatermarkPDFView_OC()
        self.preView = preView
        self.preView.frame = self.preViewBox.contentView!.bounds
        self.preView.autoresizingMask = [.width, .height]
        self.preViewBox.contentView?.addSubview(self.preView)
        self.preView.setDisplay(.singlePage)
        
        let controller = KMBatesPropertyHomeController()
        controller.view.frame = self.rightBox.contentView!.bounds
        controller.view.autoresizingMask = [.width, .height]
        self.right_gotoViewController(viewController: controller)
        controller.modelDidChange = { [weak self] model in
            if (model == nil || (model! as! KMHeaderFooterObject).hasVaild == false) {
                self!.topBarView.isCanApply(can: false)
                self?.model = nil
            } else {
                self!.topBarView.isCanApply(can: true)
                self!.model = model as! KMWatermarkAdjectiveBaseModel
                self!.model?.pageCount = Int((self?.preView.document.pageCount)!)
            }
            
            (self?.preView as! KMWatermarkPDFView_OC).model = model
        }
    }
    
    override func viewDidAppear() {
        super.viewDidAppear()
        
        (self.rightViewController as! KMBatesPropertyHomeController).pageCount = Int(self.preView.document.pageCount)
    }
    
    override func topItemClick(index: Int) {
        if (index == 0) { /// 添加
            return
        }
        if (index == 2) { /// 批量
//            KMBatchWindowController.openFile(nil, .BatesCode)
            return
        }
        
        /// 移除
        self.beginLoading()
        self.deleteBates(toPath: self.preView.document.documentURL.path) {
            result in
            
            self.endLoading()
            if (result) {
                DispatchQueue.main.async {
                    self.preView.document = CPDFDocument(url: self.preView.document.documentURL)
                }
            } else {
                let alert = NSAlert()
                alert.alertStyle = .critical
                alert.messageText = NSLocalizedString("Failure", comment: "")
                alert.runModal()
            }
        }
    }
    
    override func applyAction() {
        self.view.window?.makeFirstResponder(nil)
        
        if (self.model == nil) {
            let alert = NSAlert()
            alert.alertStyle = .critical
            alert.messageText = NSLocalizedString("Failure", comment: "")
            alert.runModal()
            return
        }
        
        if ((self.model as! KMHeaderFooterObject).hasVaild == false) {
            let alert = NSAlert()
            alert.alertStyle = .critical
            alert.messageText = NSLocalizedString("Failure", comment: "")
            alert.runModal()
            return
        }
        
        self.beginLoading()
        self.addBates(model: self.model! as! KMHeaderFooterObject, toPath: self.preView.document.documentURL.path, completion: {
            result in
            DispatchQueue.main.async {
                self.preView.layoutDocumentView()
                self.preView.setNeedsDisplayForVisiblePages()
            }
            self.endLoading()
            if (result) {
                guard let callback = self.itemClick else {
                    return
                }
                callback(1, nil)
            } else {
                let alert = NSAlert()
                alert.alertStyle = .critical
                alert.messageText = NSLocalizedString("Failure", comment: "")
                alert.runModal()
            }
        })
    }
    
    private func addBates(model: KMHeaderFooterObject, toPath: String, completion: @escaping (_ result: Bool) -> ()) {
        DispatchQueue.global().async {
            let document: CPDFDocument = self.applyDocument!
            var property = document.bates()
            
            var fontSize = 0.0
            var fontName: String = ""
            switch self.model!.textFont {
            case .font(name: let name, size: let size):
                fontName = name
                fontSize = size
                break
            default:
                break
            }
            
            let font = NSFont.boldSystemFont(ofSize:fontSize)
            let style = NSMutableParagraphStyle()
            style.alignment = .center
            style.lineBreakMode = .byCharWrapping
            let size: NSSize = "text".boundingRect(with: NSSize(width: 1000, height: 1000), options: NSString.DrawingOptions(rawValue: 3), attributes: [NSAttributedString.Key.font : font, NSAttributedString.Key.paragraphStyle : style]).size
            
            property?.margin = NSEdgeInsetsMake(max(CGFloat(model.topMargin)-size.height, 0), CGFloat(model.leftMargin), max(CGFloat(model.bottomMargin)-size.height, 0), CGFloat(model.rightMargin))
            
            let strings = [model.topLeftString, model.topCenterString, model.topRightString, model.bottomLeftString, model.bottomCenterString, model.bottomRightString]
            var count: Int = 0
            
            var color: NSColor!
            switch model.textColor {
            case .color(red: let red, green: let green, blue: let blue, alpha: let alpha):
                color = NSColor(red: red, green: green, blue: blue, alpha: alpha)
            default:
                break
            }
            
            if (color == nil) {
                color = NSColor.black
            }
            
            for text in strings {
                property?.setText(text, at: UInt(count))
                property?.setTextColor(color, at: UInt(count))
                property?.setFontSize(fontSize, at: UInt(count))
                property?.setFontName(fontName, at: UInt(count))
                count += 1
            }
            
            let pagesString = self.findPagesString(model)
            if (pagesString.isEmpty) {
                property?.pageString = "0-\(document.pageCount-1)"
            } else {
                property?.pageString = pagesString
            }
           
            property?.update()
            
            /// 保存到临时路径
            let documentPath = NSTemporaryDirectory()
            let tempPath: String = "\(documentPath)/\(toPath.lastPathComponent)"
            if (FileManager.default.fileExists(atPath: tempPath)) {
                try?FileManager.default.removeItem(atPath: tempPath)
            }
            
            let result = document.write(to: URL(fileURLWithPath: tempPath))
            if (result) {
                if (FileManager.default.fileExists(atPath: toPath)) {
                    try?FileManager.default.removeItem(atPath: toPath)
                }
                
                try?FileManager.default.moveItem(atPath: tempPath, toPath: toPath)
            } else {
                try?FileManager.default.removeItem(atPath: tempPath)
            }
            
            DispatchQueue.main.async {
                completion(result)
            }
        }
    }
    
    func deleteBates(toPath: String, completion: @escaping (_ result: Bool) -> ()) {
        if (self.preView.document.allowsPrinting == false || self.preView.document.allowsCopying == false) {
            let alert = NSAlert()
            alert.alertStyle = .critical
            alert.messageText = NSLocalizedString("This PDF document's user permissions does not allow modifying, content copying and printing.", comment: "")
            alert.runModal()
            
            completion(false)
            return
        }
        
        DispatchQueue.global().async {
            let document: CPDFDocument = self.preView.document
            var property = document.bates()
            property?.clear()
            
            /// 保存到临时路径
            let documentPath = NSTemporaryDirectory()
            let tempPath: String = "\(documentPath)/\(toPath.lastPathComponent)"
            if (FileManager.default.fileExists(atPath: tempPath)) {
                try?FileManager.default.removeItem(atPath: tempPath)
            }
            
            let result = document.write(to: URL(fileURLWithPath: tempPath))
            if (result) {
                if (FileManager.default.fileExists(atPath: toPath)) {
                    try?FileManager.default.removeItem(atPath: toPath)
                }
                
                try?FileManager.default.moveItem(atPath: tempPath, toPath: toPath)
            } else {
                try?FileManager.default.removeItem(atPath: tempPath)
            }
            
            DispatchQueue.main.async {
                completion(result)
            }
        }
    }
}