//
//  KMBatchWindowController.swift
//  PDF Master
//
//  Created by lizhe on 2023/1/12.
//

import Cocoa

class KMBatchWindowController: NSWindowController {

    @IBOutlet weak var batchPrecessingView: KMBatchProcessingView!
    @IBOutlet weak var batchCollectionView: KMBatchCollectionView!
    @IBOutlet weak var batchHandleView: NSView!
    @IBOutlet weak var settingView: KMBatchSettingView!
    @IBOutlet weak var titleLabel: NSTextField!
    
    var batchData: [KMBatchProcessingTableViewModel]?
    var inputType: DataNavigationViewButtonActionType? {
        didSet {
            var type: KMBatchCollectionViewType = .convertPDF
            switch inputType {
            case .BatchRemove:
                type = .batchRemove
            case .Background:
                type = .background
            case .Watermark:
                type = .watermark
            case .Compression:
                type = .compress
            case .Security:
                type = .security
            case.HeaderAndFooter:
                type = .headerAndFooter
            case .BatesCode:
                type = .batesNumber
            default:
                type = .convertPDF
            }
            self.type = type
        }
    }
    
    var inputSubType: Any?
    
    var type: KMBatchCollectionViewType = .convertPDF {
        didSet {
            self.reloadData()
        }
    }
    
    deinit {
        print("KMImageToPDFWindowController 释放")
    }
    
    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.
        self.window?.title = NSLocalizedString("Batch", comment: "")
        self.setup()
        self.reloadData()
    }
    
    
    func setup() {
        self.window?.contentView?.wantsLayer = true
        self.window?.contentView?.layer?.backgroundColor = NSColor.white.cgColor
        
        self.titleLabel.font = NSFont.SFProTextRegular(16.0)
        self.titleLabel.textColor = NSColor(hex: "#252629")
        
        self.batchCollectionView.delegate = self
        self.batchPrecessingView.delegate = self
        
        self.settingView.type = .convertPDF
    }
    
    func reloadData() {
        self.batchPrecessingView.inputType = self.inputType
        self.batchCollectionView.inputType = self.type
        self.settingView.type = self.type
        self.settingView.subType = self.inputSubType
    }
    
    //MARK: 打开文件
    static func openFiles(window: NSWindow) {
        if KMBatchWindowController.isSampleController() {
            print("存在相同文件")
            let controller: KMBatchWindowController = self.fetchSampleController()
            controller.inputType = .Batch
            controller.showWindow(window)
        } else {
            KMBatchProcessingView.openfiles(window: window) { openPanel in
                openPanel.title = "选择图片"
                openPanel.canChooseDirectories = false
                openPanel.canChooseFiles = true
                openPanel.allowsMultipleSelection = true
                openPanel.allowedFileTypes = KMOCRModel.supportedTypes()
            } completion: { (panel ,data) in
                if data.count != 0 {
                    let batchWindowController: KMBatchWindowController = KMBatchWindowController.init(windowNibName: "KMBatchWindowController")
                    batchWindowController.showWindow(window)
                    batchWindowController.batchPrecessingView.inputData = data
                    batchWindowController.inputType = .Batch
                }
            }
        }
    }
    
    //MARK: 打开文件
    static func openFile(_ url: URL?, _ type: DataNavigationViewButtonActionType, _ subType: Any = "") {
        if KMBatchWindowController.isSampleController() {
            let controller: KMBatchWindowController = self.fetchSampleController()
            print("存在相同文件")
            controller.inputSubType = subType
            controller.inputType = type
            controller.showWindow(NSApp.mainWindow)
        } else {
            let batchWindowController: KMBatchWindowController = KMBatchWindowController.init(windowNibName: "KMBatchWindowController")
            batchWindowController.showWindow(NSApp.mainWindow)
            if url != nil {
                batchWindowController.batchPrecessingView.inputData = [url!]
            }
            
            batchWindowController.inputSubType = subType
            batchWindowController.inputType = type
        }
    }
    
    static func isSampleController() -> Bool {
        for window in NSApp.windows {
            let controller = window.windowController
            if controller is KMBatchWindowController {
                return true
            }
        }
        return false
    }
    
    static func fetchSampleController() -> KMBatchWindowController {
        for window in NSApp.windows {
            let controller = window.windowController
            if controller is KMBatchWindowController {
                return controller as! KMBatchWindowController
            }
        }
        
        return NSWindowController() as! KMBatchWindowController
    }
}

extension KMBatchWindowController: KMBatchProcessingViewDelegate {
    func reloadData(data: [KMBatchProcessingTableViewModel]) {
        self.batchData = data
        self.settingView.filesData = self.batchData
    }
}

extension KMBatchWindowController: KMBatchCollectionViewDelegate {
    func didSelect(index: IndexPath, data: KMBatchCollectionViewModel) {
        print(data.type.rawValue)
        let dataType: KMBatchCollectionViewType = data.type
        self.settingView.type = dataType
        
        var type: DataNavigationViewButtonActionType = self.inputType ?? .ConvertPDF
        switch dataType {
        case .batchRemove:
            type = .BatchRemove
        case .background:
            type = .Background
        case .watermark:
            type = .Watermark
        case .compress:
            type = .Compression
        case .security:
            type = .Security
        case.headerAndFooter:
            type = .HeaderAndFooter
        case .batesNumber:
            type = .BatesCode
        default:
            type = .ConvertPDF
        }
        self.batchPrecessingView.inputType = type
    }
}