//
//  SettingsViewController.swift
//  SettingsViewController
//
//  Created by James Magahern on 6/25/21.
//

import UIKit

#if targetEnvironment(macCatalyst)
fileprivate extension NSToolbarItem.Identifier {
    static let categories = NSToolbarItem.Identifier("preferences.categories")
}
#endif

#if !targetEnvironment(macCatalyst)
protocol NSToolbarDelegate {}
#endif

class SettingsViewController: UITabBarController, NSToolbarDelegate
{
    #if targetEnvironment(macCatalyst)
    let toolbar = NSToolbar(identifier: NSToolbar.Identifier("net.buzzert.attractor.preferences-toolbar"))
    #endif
    
    init(windowScene: UIWindowScene) {
        super.init(nibName: nil, bundle: nil)
        self.title = "Preferences"
        
        self.viewControllers = [
            GeneralSettingsViewController(),
            RedirectRulesSettingsViewController(),
            CodeEditorSettingsViewController(settingsKeypath: \.userScript),
            CodeEditorSettingsViewController(settingsKeypath: \.userStylesheet),
        ]
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(systemItem: .done, primaryAction: UIAction { [unowned self] _ in
            self.dismiss(animated: true, completion: nil)
        }, menu: nil)
        
        #if targetEnvironment(macCatalyst)
        configureTabsForMacIdiom(windowScene: windowScene)
        #endif
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
#if targetEnvironment(macCatalyst)
    internal func configureTabsForMacIdiom(windowScene: UIWindowScene) {
        self.tabBar.isHidden = true
        
        guard let titlebar = windowScene.titlebar else { return }
        titlebar.toolbarStyle = .preference
        
        toolbar.delegate = self 
        toolbar.displayMode = .iconAndLabel
        
        titlebar.toolbar = toolbar
        
        windowScene.title = self.title
        
        toolbar.selectedItemIdentifier = toolbarAllowedItemIdentifiers(toolbar).first
    }
    
    // MARK: NSToolbarDelegate
    @objc
    func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return viewControllers!.map { NSToolbarItem.Identifier($0.tabBarItem.title!) }
    }
    
    @objc
    func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return toolbarAllowedItemIdentifiers(toolbar)
    }
    
    @objc
    func toolbarSelectableItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return toolbarAllowedItemIdentifiers(toolbar)
    }
    
    @objc
    func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem?
    {
        guard let viewController = viewControllers!.first(where: { $0.tabBarItem.title == itemIdentifier.rawValue })
            else { return nil }
        
        let item = NSToolbarItem(itemIdentifier: itemIdentifier)
        item.label = viewController.tabBarItem.title!
        item.image = viewController.tabBarItem.image!
        item.action = #selector(didSelectToolbarItem(_:))
        
        return item
    }
    
    @objc
    internal func didSelectToolbarItem(_ sender: NSToolbarItem) {
        guard let viewController = viewControllers!.first(where: { $0.tabBarItem.title == sender.itemIdentifier.rawValue }) else { return }
        
        selectedViewController = viewController
        toolbar.selectedItemIdentifier = sender.itemIdentifier
    }
#endif // targetEnvironment(macCatalyst)
}

