//
//  MenuController.swift
//  Tusker
//
//  Created by Shadowfacts on 11/14/20.
//  Copyright © 2020 Shadowfacts. All rights reserved.
//

import UIKit

struct MenuController {
    
    static let composeCommand: UIKeyCommand = {
        return UIKeyCommand(title: "Compose", action: #selector(MainSplitViewController.handleComposeKeyCommand), input: "n", modifierFlags: .command)
    }()
    
    static func refreshCommand(discoverabilityTitle: String?) -> UIKeyCommand {
        return UIKeyCommand(title: "Refresh", action: #selector(RefreshableViewController.refresh), input: "r", modifierFlags: .command, discoverabilityTitle: discoverabilityTitle)
    }
    
    static func sidebarCommand(item: MainSidebarViewController.Item, command: String, action: Selector) -> UIKeyCommand {
        return UIKeyCommand(
            title: item.title,
            image: UIImage(systemName: item.imageName!),
            action: action,
            input: command,
            modifierFlags: .command
        )
    }
    
    static let sidebarItemKeyCommands: [UIKeyCommand] = [
        sidebarCommand(item: .tab(.timelines), command: "1", action: #selector(MainSplitViewController.handleSidebarCommandTimelines)),
        sidebarCommand(item: .tab(.notifications), command: "2", action: #selector(MainSplitViewController.handleSidebarCommandNotifications)),
        sidebarCommand(item: .explore, command: "3", action: #selector(MainSplitViewController.handleSidebarCommandExplore)),
        sidebarCommand(item: .bookmarks, command: "4", action: #selector(MainSplitViewController.handleSidebarCommandBookmarks)),
        sidebarCommand(item: .tab(.myProfile), command: "5", action: #selector(MainSplitViewController.handleSidebarCommandMyProfile)),
    ]
    
    static let nextSubTabCommand = UIKeyCommand(title: "Next Sub Tab", action: #selector(TabbedPageViewController.selectNextPage), input: "]", modifierFlags: [.command, .shift])
    
    static let prevSubTabCommand = UIKeyCommand(title: "Previous Sub Tab", action: #selector(TabbedPageViewController.selectPrevPage), input: "[", modifierFlags: [.command, .shift])
    
    static func buildMainMenu(builder: UIMenuBuilder) {
        builder.replace(menu: .file, with: buildFileMenu(builder: builder))
        builder.insertChild(buildSubTabMenu(), atStartOfMenu: .view)
        builder.insertChild(buildSidebarShortcuts(), atStartOfMenu: .view)
    }
    
    private static func buildFileMenu(builder: UIMenuBuilder) -> UIMenu {
        var children: [UIMenuElement] = [
            composeCommand,
            refreshCommand(discoverabilityTitle: nil),
        ]
        if let close = builder.menu(for: .close) {
            children.append(close)
        }
        return UIMenu(
            title: "File",
            image: nil,
            identifier: nil,
            options: [],
            children: children
        )
    }
    
    private static func buildSubTabMenu() -> UIMenu {
        return UIMenu(
            title: "",
            image: nil,
            identifier: nil,
            options: .displayInline,
            children: [
                prevSubTabCommand,
                nextSubTabCommand,
            ]
        )
    }
    
    private static func buildSidebarShortcuts() -> UIMenu {
        return UIMenu(
            title: "",
            image: nil,
            identifier: nil,
            options: .displayInline,
            children: sidebarItemKeyCommands
        )
    }
    
}
