//
//  KMPageEditSplitWindowController.swift
//  PDF Reader Pro
//
//  Created by tangchao on 2023/1/12.
//

class KMPageEditSplitWindowController: KMPageEditSettingBaseWindowController {
    var hasPreView = false
    
    override func initSubViews() {
        super.initSubViews()
        
        if (self.hasPreView) {
            let controller = KMPDFInsertPreviewViewController(nibName: "KMPDFInsertPreviewViewController", bundle: nil)
            controller.pdfDocument = CPDFDocument(url: self.model?.documentURL)
            self.contentBox.contentView?.addSubview(controller.view)
            controller.view.frame = NSMakeRect(0, 40, 176+16*2, 300)
            controller.view.autoresizingMask = [.maxYMargin]
            
            let settingView: KMPageEditSplitSettingView = KMPageEditSplitSettingView.createFromNib()!
            self.contentBox.contentView?.addSubview(settingView)
            let settingViewLeft: CGFloat = 176+16+8
            settingView.frame = NSRect(x: settingViewLeft, y: 0, width: NSWidth(self.contentBox.frame)-settingViewLeft, height: NSHeight(self.contentBox.frame))
            settingView.autoresizingMask = [.width, .height]
            self.settingView = settingView
            settingView.pageCount = self.model!.pageCount
            settingView.type = (self.model as! KMPageEditSplitSettingModel).type
            if ((self.model as! KMPageEditSplitSettingModel).type == 2) {
                settingView.setPageRangeString((self.model as! KMPageEditSplitSettingModel).pageRangeString)
            }
            settingView.updateName(self.model!.outputFileName)
        } else {
            let settingView: KMPageEditSplitSettingView = KMPageEditSplitSettingView.createFromNib()!
            self.contentBox.contentView = settingView
            self.settingView = settingView
            settingView.pageCount = self.model!.pageCount
            settingView.type = (self.model as! KMPageEditSplitSettingModel).type
            if ((self.model as! KMPageEditSplitSettingModel).type == 2) {
                settingView.setPageRangeString((self.model as! KMPageEditSplitSettingModel).pageRangeString)
            }
            settingView.updateName(self.model!.outputFileName)
        }
        
        self.settingView?.itemClick = { [unowned self] index, value in
            if (index == 1) { /// 类型切换
                (self.model as! KMPageEditSplitSettingModel).type = value as! Int
                return
            }
            if (index == 2) { /// 平均每几页拆分
                (self.model as! KMPageEditSplitSettingModel).avgNumberForOneFile = value as! Int
                return
            }
            if (index == 3) { /// 平均拆分几个文件
                (self.model as! KMPageEditSplitSettingModel).avgSplitNumberFile = value as! Int
                return
            }
            if (index == 4) { /// 页面范围切换
                (self.model as! KMPageEditSplitSettingModel).pageRangeType = value as! Int
                return
            }
            if (index == 5) { /// 页面范围输入
                var pageCount = 0
                if let _pageCount = self.model?.pageCount {
                    pageCount = _pageCount
                }
                if let data = value as? String, let _value = Int(data) {
                    if (_value < 1 || _value > pageCount) {
                        let alert = NSAlert()
                        alert.messageText = NSLocalizedString("Page range must between 1 and \(pageCount)", comment: "")
                        alert.informativeText = NSLocalizedString("Out of page range", comment: "")
                        alert.runModal()
                        (self.settingView as? KMPageEditSplitSettingView)?.setPageRangeString("1")
                        (self.model as! KMPageEditSplitSettingModel).pageRangeType = 2
                        (self.model as! KMPageEditSplitSettingModel).pageRangeString = "1"
                        return
                    }
                }
                (self.model as! KMPageEditSplitSettingModel).pageRangeType = 2
                (self.model as! KMPageEditSplitSettingModel).pageRangeString = value as! String
                return
            }
            if (index == 6) { /// 将原始文档名前置
                (self.model as! KMPageEditSplitSettingModel).origFileNameIsPrefix = value as! Bool
                
                (self.settingView as! KMPageEditSplitSettingView).updateName(self.model!.outputFileName)
                return
            }
            if (index == 7) { /// 标签
                (self.model as! KMPageEditSplitSettingModel).hasTag = value as! Bool
                
                (self.settingView as! KMPageEditSplitSettingView).updateName(self.model!.outputFileName)
                return
            }
            if (index == 8) { /// 分隔符
                (self.model as! KMPageEditSplitSettingModel).hasSep = value as! Bool
                
                (self.settingView as! KMPageEditSplitSettingView).updateName(self.model!.outputFileName)
                return
            }
            
            if (index == 9) { /// 分隔符
                (self.model as! KMPageEditSplitSettingModel).tagString = value as! String
                
                (self.settingView as! KMPageEditSplitSettingView).updateName(self.model!.outputFileName)
                return
            }
            
            if (index == 10) { /// 分隔符
                (self.model as! KMPageEditSplitSettingModel).sepString = value as! String
                
                (self.settingView as! KMPageEditSplitSettingView).updateName(self.model!.outputFileName)
                return
            }
        }
    }
    
    override func initDefaultValue() {
        super.initDefaultValue()
        
        if (self.hasPreView) {
            self.setWindowSize(NSSize(width: 583, height: 481))
            self.titleLabel.stringValue = NSLocalizedString("Split", comment: "")
        } else {
            self.setWindowSize(NSSize(width: 480, height: 450))
            self.titleLabel.stringValue = NSLocalizedString("Split Page", comment: "")
        }
        
        self.funcVC?.stringValue = NSLocalizedString("Split", comment: "")
        self.funcVC?.updateUI()
    }
    
    override func funcButtonAction() {
        guard let _model = self.model as? KMPageEditSplitSettingModel else {
            super.funcButtonAction()
            return
        }
        
        if (_model.pageRangeType == 2) {
            let pages = KMPageRangeTools.findSelectPage(pageRangeString: _model.pageRangeString, pageCount: _model.pageCount)
            if (pages.isEmpty) {
                let alert = NSAlert()
                alert.messageText = NSLocalizedString("Page range must between 1 and \(_model.pageCount)", comment: "")
                alert.informativeText = NSLocalizedString("Out of page range", comment: "")
                alert.runModal()
                (self.settingView as? KMPageEditSplitSettingView)?.setPageRangeString("1")
                
                _model.pageRangeString = "1"
                return
            }
        }
        super.funcButtonAction()
    }
}