//
//  WCCompWindowController.swift
//  PDF Reader Pro
//
//  Created by Niehaoyu on 2024/8/22.
//

import Cocoa
import KMComponentLibrary

class WCCompWindowController: NSWindowController {
    
    @IBOutlet weak var tableView: NSTableView!
    @IBOutlet weak var rigntBox: NSBox!

    @IBOutlet weak var appjsonLabel: NSTextField!
    @IBOutlet weak var brandJsonLabel: NSTextField!
    @IBOutlet weak var resetButton: NSButton!
    
    
    var buttonVC: ButtonDemoVC = ButtonDemoVC()
    var dividerVC: DividerDemoVC = DividerDemoVC()
    var dropdownVC: DropdownDemoVC = DropdownDemoVC()
    var navigationVC: NavigationDemoVC = NavigationDemoVC()
    var tabbarVC: TabbarDemoVC = TabbarDemoVC()
    var sidebarVC: SidebarDemoVC = SidebarDemoVC()
    
    var inputVC: InputDemoVC = InputDemoVC()
    var inputNumberVC: InputNumberVC = InputNumberVC()
    
    var checkBoxVC: CheckBoxVC = CheckBoxVC()
    var sliderVC: SliderVC = SliderVC()
    var selectVC: SelectVC = SelectVC()
    var controllerVC: ControllerVC = ControllerVC()
    var progressVC: ProgressVC = ProgressVC()
    var segmentedVC: SegmentedVC = SegmentedVC()
    var tooltipVC: TooltipVC = TooltipVC()
    var tabsVC: TabsVC = TabsVC()
    var messageVC: MessageVC = MessageVC()
    var alertVC: AlertControllerVC = AlertControllerVC()
    var emptyVC: EmptyVC = EmptyVC()
    var notiVC: NotiVC = NotiVC()
    var cardVC: CardVC = CardVC()
    var paginationVC: PaginationVC = PaginationVC()
    var listVC: ListVC = ListVC()
    var modalVC: ModalVC = ModalVC()
    var treeVC: TreeVC = TreeVC()
   
    let tools = ["Button", "Divider", "Dropdown", "Navigation", "Input", "InputNumber", "CheckBox", "Radio", "Slider", "Select", "Controller", "Progress", "Segmented", "Tooltip", "Tabs", "Message", "Alert & Tag", "Empty", "notificationVC", "Card", "Pagination", "Sidebar", "List & FloatingToolbar", "Modal", "Tree"]
    
    
    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.
        ComponentLibrary.shared.parserExcel(withPath: "")
        
        rigntBox.contentView = buttonVC.view
        
        self.reloadData()
    }
    
    func reloadData() {
        var isEnable = true
        if let appjsonURL = ComponentLibrary.shared.appJsonURL {
            let path = appjsonURL.path
            self.appjsonLabel.stringValue = path
        } else {
            self.appjsonLabel.stringValue = "-"
            isEnable = false
        }
        
        if let brandJsonURL = ComponentLibrary.shared.brandJsonURL {
            let path = brandJsonURL.path
            self.brandJsonLabel.stringValue = path
        } else {
            self.brandJsonLabel.stringValue = "-"
            isEnable = false
        }
        
        self.resetButton.isEnabled = isEnable
        
    }
    
    @IBAction func chooseAPPJson(_ sender: Any) {
        let openPanel = NSOpenPanel()
        openPanel.canChooseDirectories = true
        openPanel.canChooseFiles = true
        openPanel.allowsMultipleSelection = false;
        openPanel.allowedFileTypes = ["json"]
        openPanel.beginSheetModal(for: self.window!) { result in
            if result == .OK {
                let fileURL = openPanel.urls.first
                
                ComponentLibrary.shared.appJsonURL = fileURL
                
            }
            self.reloadData()
        }
    }
    
    @IBAction func changeBrandJson(_ sender: Any) {
        
        let openPanel = NSOpenPanel()
        openPanel.canChooseDirectories = true
        openPanel.canChooseFiles = true
        openPanel.allowsMultipleSelection = false;
        openPanel.allowedFileTypes = ["json"]
        openPanel.beginSheetModal(for: self.window!) { result in
            if result == .OK {
                let fileURL = openPanel.urls.first
                
                ComponentLibrary.shared.brandJsonURL = fileURL
                
            }
            self.reloadData()
        }
        
    }
    
    @IBAction func resetAction(_ sender: Any) {
        ComponentLibrary.shared.parserExcel(withPath: "")
    }
    
    
}


extension WCCompWindowController: NSTableViewDataSource, NSTableViewDelegate {
    
    // MARK: NSTableViewDelegate
    
    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        let cellIdentifier = NSUserInterfaceItemIdentifier("TableViewCell")
        guard let cell = tableView.makeView(withIdentifier: cellIdentifier, owner: self) as? NSTableCellView else {
            return nil
        }
        cell.textField?.stringValue = tools[row]
        return cell
    }

    func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
        return 50.0
    }
    
    func tableViewSelectionDidChange(_ notification: Notification) {
        let row = self.tableView.selectedRow
        if row == -1 { return }
        
        if tools[row] == "Button" {
            rigntBox.contentView = buttonVC.view
        } else if tools[row] == "Divider" {
            rigntBox.contentView = dividerVC.view
        } else if tools[row] == "Dropdown" {
            rigntBox.contentView = dropdownVC.view
        } else if tools[row] == "Navigation" {
            rigntBox.contentView = navigationVC.view
        } else if tools[row] == "Tabbar" {
            rigntBox.contentView = tabbarVC.view
        } else if tools[row] == "Sidebar" {
            rigntBox.contentView = sidebarVC.view
        } else if tools[row] == "Input" {
            rigntBox.contentView = inputVC.view
        } else if tools[row] == "InputNumber" {
            rigntBox.contentView = inputNumberVC.view
        } else if tools[row] == "CheckBox" {
            rigntBox.contentView = checkBoxVC.view
            checkBoxVC.typeBox.selectItem(at: 0)
            checkBoxVC.reloadData()
        } else if tools[row] == "Radio" {
            rigntBox.contentView = checkBoxVC.view
            checkBoxVC.typeBox.selectItem(at: 1)
            checkBoxVC.reloadData()
        } else if tools[row] == "Slider" {
            rigntBox.contentView = sliderVC.view
        } else if tools[row] == "Select" {
            rigntBox.contentView = selectVC.view
        } else if tools[row] == "Controller" {
            rigntBox.contentView = controllerVC.view
        } else if tools[row] == "Progress" {
            rigntBox.contentView = progressVC.view
        } else if tools[row] == "Segmented" {
            rigntBox.contentView = segmentedVC.view
        } else if tools[row] == "Tooltip" {
            rigntBox.contentView = tooltipVC.view
        } else if tools[row] == "Tabs" {
            rigntBox.contentView = tabsVC.view
        } else if tools[row] == "Message" {
            rigntBox.contentView = messageVC.view
        } else if tools[row] == "Alert & Tag" {
            rigntBox.contentView = alertVC.view
        } else if tools[row] == "Empty" {
            rigntBox.contentView = emptyVC.view
        } else if tools[row] == "notificationVC" {
            rigntBox.contentView = notiVC.view
        } else if tools[row] == "Card" {
            rigntBox.contentView = cardVC.view
        } else if tools[row] == "Pagination" {
            rigntBox.contentView = paginationVC.view
        } else if tools[row] == "List & FloatingToolbar" {
            rigntBox.contentView = listVC.view
        } else if tools[row] == "Modal" {
            rigntBox.contentView = modalVC.view
        } else if tools[row] == "Tree" {
            rigntBox.contentView = treeVC.view
        }
        
        
        
    }
    
    // MARK: NSTableViewDataSource
    
    func numberOfRows(in tableView: NSTableView) -> Int {
        return tools.count
    }
}