//
//  KMPDFViewPanelSetViewController.swift
//  PDF Master
//
//  Created by lxy on 2023/1/17.
//

import Cocoa

protocol KMPDFViewPanelSetViewControllerDelegate: AnyObject {
    func controller(_ controller: KMPDFViewPanelSetViewController, dispayDidChange dispay: KMPDFDisplayType)
    func controller(_ controller: KMPDFViewPanelSetViewController, pageBreaksClick pageBreaks: Bool)
    func controller(_ controller: KMPDFViewPanelSetViewController, rotateClick rotateType: KMRotateType)
}

extension KMPDFViewPanelSetViewControllerDelegate {
    func controller(_ controller: KMPDFViewPanelSetViewController, pageBreaksClick pageBreaks: Bool) {
        
    }
    func controller(_ controller: KMPDFViewPanelSetViewController, rotateClick rotateType: KMRotateType) {
        
    }
}

class KMPDFViewPanelSetViewController: NSViewController {
    
    @IBOutlet weak var contentView: NSView!
    @IBOutlet weak var shouModelTitleLabel: NSTextField!
    @IBOutlet weak var mainTitleLabel: NSTextField!
    @IBOutlet weak var slideLabel: NSTextField!
    @IBOutlet weak var bookLabel: NSTextField!
    @IBOutlet weak var doublePageLabel: NSTextField!
    @IBOutlet weak var singlePageLabel: NSTextField!
    @IBOutlet weak var readLabel: NSTextField!
    @IBOutlet weak var fullScreenLabel: NSTextField!
    
    @IBOutlet weak var rightRotationButton: NSButton!
    @IBOutlet weak var leftRotationButton: NSButton!
    @IBOutlet weak var rotationLabel: NSTextField!
    @IBOutlet weak var continuousRollingButton: NSButton!
    @IBOutlet weak var pageBreakButton: NSButton!
    
    @IBOutlet weak var pptImageView: NSImageView!
    @IBOutlet weak var fullSceenImageView: NSImageView!
    @IBOutlet weak var readImageView: NSImageView!
    @IBOutlet weak var bookImageView: NSImageView!
    @IBOutlet weak var singlePageImageView: NSImageView!
    @IBOutlet weak var twoPageImageView: NSImageView!
    
    weak var listView : CPDFListView?
    weak var delegate: KMPDFViewPanelSetViewControllerDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setup()
        self.updateUI()
        self.updateLanguage()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    func addNotification() {
        NotificationCenter.default.addObserver(self, selector: #selector(displayModeChangedNotification), name: NSNotification.Name.CPDFViewDisplayModeChanged, object:nil)
    }
    
    func setup() {
        
    }
    
    func updateLanguage() {
        self.shouModelTitleLabel.stringValue = NSLocalizedString("Display Model", comment: "")
        self.mainTitleLabel.stringValue = NSLocalizedString("View", comment: "")
        self.slideLabel.stringValue = NSLocalizedString("Read Mode", comment: "")
        self.bookLabel.stringValue = NSLocalizedString("Book Mode", comment: "")
        self.doublePageLabel.stringValue = NSLocalizedString("Two Pages", comment: "")
        self.singlePageLabel.stringValue = NSLocalizedString("Single Page", comment: "")
        self.readLabel.stringValue = NSLocalizedString("PPT", comment: "")
        self.rotationLabel.stringValue = NSLocalizedString("Rotate", comment: "")
        self.fullScreenLabel.stringValue = NSLocalizedString("Full Screen", comment: "")
        self.continuousRollingButton.title = NSLocalizedString("Continuous Scroll", comment: "")
        self.pageBreakButton.title = NSLocalizedString("Page Breaks", comment: "")
    }
    
    func updateUI() {
        self.contentView!.backgroundColor(NSColor(hex: "#F7F8FA"))

        self.rotationLabel.textColor = NSColor(hex: "#616469")
        self.rotationLabel.font = NSFont.SFProTextRegular(11.0)
        
        self.shouModelTitleLabel.textColor = NSColor(hex: "#616469")
        self.shouModelTitleLabel.font = NSFont.SFProTextRegular(11.0)
        
        self.singlePageLabel.textColor = NSColor(hex: "#616469")
        self.singlePageLabel.font = NSFont.SFProTextRegular(11.0)
        
        self.doublePageLabel.textColor = NSColor(hex: "#616469")
        self.doublePageLabel.font = NSFont.SFProTextRegular(11.0)
        
        self.bookLabel.textColor = NSColor(hex: "#616469")
        self.bookLabel.font = NSFont.SFProTextRegular(11.0)
        
        self.readLabel.font = NSFont.SFProTextRegular(11.0)
        self.readLabel.font = NSFont.SFProTextRegular(11.0)
        
        self.fullScreenLabel.font = NSFont.SFProTextRegular(11.0)
        self.fullScreenLabel.font = NSFont.SFProTextRegular(11.0)
        
        self.slideLabel.font = NSFont.SFProTextRegular(11.0)
        self.slideLabel.font = NSFont.SFProTextRegular(11.0)
        
        self.leftRotationButton.wantsLayer = true
        self.leftRotationButton.layer?.borderWidth = 1
        self.leftRotationButton.layer?.cornerRadius = 4
        self.leftRotationButton.layer?.borderColor = NSColor(hex: "#DFE1E5").cgColor
        self.leftRotationButton.layer?.backgroundColor = NSColor.white.cgColor
        
        self.rightRotationButton.wantsLayer = true
        self.rightRotationButton.layer?.borderWidth = 1
        self.rightRotationButton.layer?.cornerRadius = 4
        self.rightRotationButton.layer?.borderColor = NSColor(hex: "#DFE1E5").cgColor
        self.rightRotationButton.layer?.backgroundColor = NSColor.white.cgColor
        self.reloadListViewModel()
    }
    
    func reloadListViewModel() {
        if(self.listView?.displayTwoUp == true) {
            if self.listView?.displaysAsBook == true {
                self.updateDispalyMode(currentImgae: bookImageView)
            } else {
                self.updateDispalyMode(currentImgae: twoPageImageView)
            }
        } else {
            self.updateDispalyMode(currentImgae: singlePageImageView)
        }

        if(self.listView?.displayDirection == .vertical) {
            self.continuousRollingButton.state = .on
        } else {
            self.continuousRollingButton.state = .off
        }

        if self.listView?.displaysPageBreaks == true {
            self.pageBreakButton.state = .on
        } else {
            self.pageBreakButton.state = .off
        }
    }
    
    @IBAction func setPDFViewShow_Click(_ sender: NSButton) {
        let continous = self.continuousRollingButton.state == .on
        switch sender.tag {
        case 0:
            self.delegate?.controller(self, dispayDidChange: continous ? .singlePageContinuous : .singlePage)
            self.updateDispalyMode(currentImgae: singlePageImageView)
            break
        case 1:
            self.delegate?.controller(self, dispayDidChange: continous ? .twoUpContinuous : .twoUp)
            self.updateDispalyMode(currentImgae: twoPageImageView)
            break
        case 2:
            self.delegate?.controller(self, dispayDidChange: continous ? .bookContinuous : .bookMode)
            self.updateDispalyMode(currentImgae: bookImageView)
            break
        case 3:
            self.delegate?.controller(self, dispayDidChange: continous ? .readContinuous : .readModel)
            break
        case 4:
            self.view.window?.toggleFullScreen(nil)
            break
        case 5:
            break
        default:
            break
        }
    }
    
    private func updateDispalyMode(currentImgae:NSImageView) {
        let images : [NSImageView] = [singlePageImageView,twoPageImageView,bookImageView,readImageView,fullSceenImageView,pptImageView]
        let imageNor : [String] = ["KMImageNamePDFViewSignleNor","KMImageNamePDFViewTwoNor","KMImageNamePDFViewBookNor","KMImageNamePDFViewReadNor","KMImageNamePDFViewFullScreenNor","KMImageNamePDFViewTTSNor"]
        let imageSel : [String] = ["KMImageNamePDFViewSignleSel","KMImageNamePDFViewTwoSel","KMImageNamePDFViewBookSel","KMImageNamePDFViewReadSel","KMImageNamePDFViewFullScreenSel","KMImageNamePDFViewTTSSel"]
        for index in 0 ..< images.count - 1 {
            let image = images[index]
            image.wantsLayer = true
            image.layer?.borderWidth = 1.0
            image.layer?.cornerRadius = 4.0
            if currentImgae == image {
                image.layer?.backgroundColor = NSColor(hex: "#CED0D4",alpha:0.6).cgColor
                image.layer?.borderColor = NSColor(hex: "#CED0D4").cgColor
                image.image = NSImage(named: imageSel[index])
            } else {
                image.layer?.backgroundColor = NSColor.clear.cgColor
                image.layer?.borderColor = NSColor.clear.cgColor
                image.image = NSImage(named: imageNor[index])
            }
        }
  
    }
    
    @IBAction func singlePageAction(_ sender: Any) {
//        mainVC?.singlePageScreen(isSinglePage: true, doublePagesScreen: false)
    }
    
    @IBAction func verticalSplitAction(_ sender: Any) {
//        mainVC?.singlePageScreen(isSinglePage: false, doublePagesScreen: false)
    }
    
    @IBAction func horizontalSplitAction(_ sender: Any) {
//        mainVC!.singlePageScreen(isSinglePage: false, doublePagesScreen: true)
    }
    
    
    @IBAction func setSplitModel_Click(_ sender: Any) {
        
    }
    
    @IBAction func continuousRollingButton_Click(_ sender: Any) {
        let continous = self.continuousRollingButton.state == .on
        if self.listView?.displayMode() == .singlePage {
            if (continous) {
                self.delegate?.controller(self, dispayDidChange: .singlePageContinuous)
            }

        } else if self.listView?.displayMode() == .singlePageContinuous {
            if (continous == false) {
                self.delegate?.controller(self, dispayDidChange: .singlePage)
            }
        } else if self.listView?.displayMode() == .twoUp {
            if (continous) {
                self.delegate?.controller(self, dispayDidChange: .twoUpContinuous)
            }
        } else if self.listView?.displayMode() == .twoUpContinuous {
            if (continous == false) {
                self.delegate?.controller(self, dispayDidChange: .twoUp)
            }
        }
    }
    
    @IBAction func pageBreakButton_Click(_ sender: Any) {
        self.delegate?.controller(self, pageBreaksClick: self.pageBreakButton.state == .on)
    }
    
    @IBAction func rotationButton_Click(_ sender: Any) {
        self.delegate?.controller(self, rotateClick: .clockwise)
    }
    
    @IBAction func rotationButtonRight_Click(_ sender: Any) {
        self.delegate?.controller(self, rotateClick: .anticlockwise)
    }
    
    @objc func displayModeChangedNotification(_ notification: Notification) -> Void {
        if notification.object is CPDFListView {
            let pdfview = notification.object as! CPDFListView
            if(pdfview.document == self.listView?.document) {
                self.reloadListViewModel()
            }
        }
    }
}