'use strict';

// Make sure to execute everything in an anonymous function 
//	so that local variables don't get caught up with other extensions/scripts
(() => {
    var toolbarIcon;
    var settings = app.getValue('refreshSkinButton_config', {
        showToolbarIcon: true,
        autoRegisterHotkey: true,
        defaultAction: 0, // 0 is soft reload, 1 is window reload, 2 is full app reload
    });
    
    // Define functions attached to window, so that config.js can access it
    window.refreshSkinButton_autoRegisterHotkey = function(sett) {
        
        if (sett.autoRegisterHotkey) {
            // Register hotkeys (It automatically checks for duplicates and for manual deletion)
            window.hotkeys.addHotkey('Shift+F5', 'lessReload', false);
            window.hotkeys.addHotkey('Ctrl+Shift+F5', 'windowReload', false);
            window.hotkeys.addHotkey('Ctrl+Alt+F5', 'fullAppReload', false);
        }
    }
    
    window.refreshSkinButton_showToolbarIcon = function(sett) {
        // Remove toolbarIcon first if it exists
        if (toolbarIcon) {
            qid('righttoolbuttons').removeChild(toolbarIcon);
            toolbarIcon = undefined;
        }
        // Add icon [parameters are: location name, icon name, callback, tooltip]
        if (sett.showToolbarIcon) {
            // tooltip depends on which action is set to default
            let tooltip = '';
            switch (sett.defaultAction) {
                case 0: tooltip = 'Refresh skin'; break;
                case 1: tooltip = 'Refresh skin (Quick window reload)'; break;
                case 2: tooltip = 'Reload MediaMonkey'; break;
            }
            
            toolbarIcon = window.uitools.addToolButton('righttoolbuttons', 'refresh-skin' /* icon */ , function () {
                // Execute the proper reload function that corresponds to the user-set default action
                switch (sett.defaultAction) {
                    case 0: actions.lessReload.execute(); break;
                    case 1: actions.windowReload.execute(); break;
                    case 2: actions.fullAppReload.execute(); break;
                }
            }, tooltip);
            // Context menu for our icon
            toolbarIcon.controlClass.contextMenu = new Menu([
                {
                    title: 'Soft reload (Just reload LESS)',
                    icon: 'rightArrow',
                    execute: () => {
                        actions.lessReload.execute();
                    }
                },
                {
                    title: 'Quick window reload',
                    icon: 'next',
                    execute: () => {
                        actions.windowReload.execute();
                    }
                },
                {
                    title: 'Full app reload',
                    icon: 'refresh',
                    execute: () => {
                        actions.fullAppReload.execute();
                    }
                }
            ])
        }
    }
    
    // Run the functions with this locally-accessible settings object
    window.refreshSkinButton_autoRegisterHotkey(settings);
    window.refreshSkinButton_showToolbarIcon(settings);
})();