import foldertree.FolderTree

class BasicNavigation : SimpleComponent() {

    // ==== Default values ====

    class var harbourLeftRatio = 0.2f
    class var harbourRightRatio = 0.8f
    class var harbourBottomRatio = 0.7f

    // ==== Register ====

    override meth register() {
        Registry.INSTANCE.setMenuOrder( 0, "Application", "Project", "Folder", "File", "Music", "Tools", "Tabs", "Docks" )

        addFeatures( Context.NONE, 20 ).apply {

            // Back and forward buttons (similar to those found in a web browser).
            // Moves back/forward through the current tab's history of Behaviours.
            val back = BackFeature( "Back" )
                .icon( "back" ).shortcut( Key.LEFT.alt() )
            val forward = ForwardFeature( "Forward" )
                .icon( "forward" ).shortcut( Key.RIGHT.alt() )
            val backSplit = BackSplitFeature( "Back" )
                .icon( "back" )
            val forwardSplit = ForwardSplitFeature( "Forward" )
                .icon( "forward" )

            val firstTab = FirstTabFeature( "First Tab" )
                .shortcut( Key.HOME.control().alt() )
            val prevTab = PrevTabFeature( "Previous Tab" )
                .shortcut( Key.LEFT.control().alt() )
            val nextTab = NextTabFeature( "Next Tab" )
                .shortcut( Key.RIGHT.control().alt() )
            val lastTab = LastTabFeature( "Last Tab" )
                .shortcut( Key.END.control().alt() )

            val firstSecondaryTab = FirstSecondaryTabFeature( "First Secondary Tab" )
                .shortcut( Key.HOME.alt().shift() )
            val prevSecondaryTab = PrevSecondaryTabFeature( "Previous Secondary Tab" )
                .shortcut( Key.LEFT.alt().shift() )
            val nextSecondaryTab = NextSecondaryTabFeature( "Next Secondary Tab" )
                .shortcut( Key.RIGHT.alt().shift() )
            val lastSecondaryTab = LastSecondaryTabFeature( "Last Secondary Tab" )
                .shortcut( Key.END.alt().shift() )


            val tabsMenu = MenuFeature( "Tabs",
                back, forward,
                separator,
                firstTab, prevTab, nextTab, lastTab,
                separator,
                firstSecondaryTab, prevSecondaryTab, nextSecondaryTab, lastSecondaryTab
            )

            val toggleOutputDock = Action( "Ouput Dock", ::toggleDock.curry( "output" ) )
                .icon("output").shortcut( Key.DIGIT_0.alt() )
            
            val systemDock = Action( "System Project Dock", ::dockSystemComponents )
                .icon( "code" ).shortcut( Key.DIGIT_1.alt() )
            val personalDock = Action( "Personal Project Dock", ::dockPersonalComponents )
                .icon( "code" ).shortcut( Key.DIGIT_2.alt() )
            val localDock = Action( "Local Project Dock", ::dockLocalComponents )
                .icon( "code" ).shortcut( Key.DIGIT_3.alt() )

            val docksMenu = MenuFeature( "Docks",
                toggleOutputDock,
                separator,
                systemDock, personalDock, localDock
            )

            toolbar( backSplit, forwardSplit )

            statusBar( StatusMessageFeature.INSTANCE, spacer, spacer, TextPositionFeature.INSTANCE )

            // Chances are the menu items won't be USED, but for those who like using shortcuts,
            // it is a good place to display the shortcuts that are available. YMMV.
            // If you remove the menu, but want to keep the shortcuts, remember to add the features
            // as shortcuts! Using the shortcuts( ... ) function.
            menus( tabsMenu, docksMenu )

        }
    }

    override meth start() {
        setHarbourRatio( Side.LEFT, harbourLeftRatio )
        setHarbourRatio( Side.RIGHT, harbourRightRatio )
        setHarbourRatio( Side.BOTTOM, harbourBottomRatio )
    }

    // ==== Actions ====

    func toggleDock( dockID : String ) {
        toggleDock( dockID )
    }

    func dockSystemComponents() {
        dockComponentsFolder( settings().systemComponentsFolder, "System" )
    }

    func dockPersonalComponents() {
        dockComponentsFolder( settings().personalComponentsFolder, "Personal" )
    }

    func dockLocalComponents() {
        dockComponentsFolder( settings().localComponentsFolder, "Local" )
    }

    func dockComponentsFolder( folder : File, label : String ) {
        if (folder.exists()) {
            FolderTree.folderTreeBehaviour( folder ).apply {
                title = label
                iconName = "code"
            }.dockBehaviour()
        }
    }
}
