//
//  KMSideViewController.swift
//  PDF Reader Pro
//
//  Created by lxy on 2022/11/17.
//

import Cocoa

class KMSideViewController: NSViewController {
    @IBOutlet weak var searchField: NSSearchField!
    @IBOutlet weak var currentView: NSView!
    
    var isAnimating = false
    weak var listView : CPDFListView?
    weak var mainViewController : KMMainViewController?
    
    private let duration_ = 0.7
    
    deinit {
        KMPrint("KMSideViewController deinit.")
        
        NotificationCenter.default.removeObserver(self)
    }
    
    override func loadView() {
        super.loadView()
        
//        [gradientView setAutoTransparent:YES];
//        [gradientView setMinSize:NSMakeSize(GRADIENT_MIN_WIDTH, NSHeight([gradientView frame]))];
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
    }
    
    func replaceSideView(_ newView: NSView, animate: Bool) {
        if (newView.window != nil) {
            return
        }
        
        var animated = animate
        if (UserDefaults.standard.bool(forKey: SKDisableAnimationsKey)) {
            animated = false
        }
        
        let oldView = self.currentView
        self.currentView = newView
        let contentView = oldView?.superview
        
        var firstResponder = oldView?.window?.firstResponder
        if let data = oldView {
            let des = (firstResponder as? NSView)?.isDescendant(of: data) ?? false
            if des {
                firstResponder = newView
            } else {
                firstResponder = nil
            }
        } else {
            firstResponder = nil
        }
        
        newView.frame = oldView?.frame ?? .zero
        if (animated == false) {
            if let data = oldView {
                contentView?.replaceSubview(data, with: newView)
            }
            firstResponder?.km_window()?.makeFirstResponder(firstResponder)
            contentView?.window?.recalculateKeyViewLoop()
        } else {
            self.isAnimating = true
            contentView?.wantsLayer = true
            contentView?.displayIfNeeded()

            NSAnimationContext.runAnimationGroup { context in
                context.duration = self.duration_
                if let data = oldView {
                    contentView?.animator().replaceSubview(data, with: newView)
                }
            } completionHandler: {
                contentView?.wantsLayer = false
                firstResponder?.km_window()?.makeFirstResponder(firstResponder)
                contentView?.window?.recalculateKeyViewLoop()
                self.isAnimating = false
            }
        }
    }
    
    /*
     #define GRADIENT_MIN_WIDTH 111.0
     */
}

// MARK: - listView

extension KMSideViewController {
    func pageCount() -> Int {
        return Int(self.pdfDocument()?.pageCount ?? 0)
    }
    
    func pdfDocument() -> CPDFDocument? {
        return self.listView?.document
    }
    
    func hideNotes() -> Bool {
        return self.listView?.hideNotes ?? false
    }
    
    func allowsNotes() -> Bool {
        return self.listView?.allowsNotes() ?? false
    }
    
    func currentPage() -> CPDFPage? {
        return self.listView?.currentPage()
    }
    
    func currentDestination() -> CPDFDestination? {
        return self.listView?.currentDestination
    }
    
    func currentPageIndex() -> Int {
        return self.listView?.currentPageIndex ?? NSNotFound
    }
    
    func isLocked() -> Bool {
        return self.pdfDocument()?.isLocked ?? true
    }
    
    func scaleFactor() -> Float {
        return Float(self.listView?.scaleFactor ?? 0)
    }
    
    func displayBox() -> CPDFDisplayBox {
        return self.listView?.displayBox ?? .cropBox
    }
    
    func allowsPrinting() -> Bool {
        return self.pdfDocument()?.allowsPrinting ?? true
    }
    
    func bookmarks() -> [CPDFBookmark]? {
        return self.pdfDocument()?.bookmarks()
    }
    
    func outlineRoot() -> CPDFOutline? {
        return self.pdfDocument()?.outlineRoot()
    }
    
    func setNewOutlineRoot() -> CPDFOutline? {
        return self.pdfDocument()?.setNewOutlineRoot()
    }
    
    func layoutDocumentView() {
        self.listView?.layoutDocumentView()
    }
}