requirejs('controls/trackListView');

// to load config window during installation and avoid crash
if (!window.TrackShortcuts) {
    window.TrackShortcuts = [
        { id: 1, order: 10, title: 'Google', },
        { id: 2, order: 20, title: 'Wikipedia', },
        { id: 3, order: 30, title: 'YouTube', },
        { id: 4, order: 40, title: 'Spotify', },
        { id: 5, order: 50, title: 'Discogs', },
        { id: 6, order: 60, title: 'AllMusic', },
        { id: 51, order: 91, title: 'Custom 1', },
        { id: 52, order: 92, title: 'Custom 2', },
        { id: 53, order: 93, title: 'Custom 3', },
        { id: 99, order: 99, title: _('Library'), },
    ];
}

window.configInfo = {

    load: function (panel, addon) {
              
        var UI = getAllUIElements(panel);

        var ds = newStringList(true);
        var sett = window.settings.get();
        var selId = sett.Options.ActiveTrackShortcutType;

        TrackShortcuts.sort(function (i1, i2) {
            var retval = i1.order - i2.order;
            return retval;
        });
        var ts;
        var fidx = 0;
        for (var i = 0; i < TrackShortcuts.length; i++) {
            ts = TrackShortcuts[i];
            ds.add(ts.title);
            if (ts.id == selId)
                fidx = i;
        };
        UI.cbShowLinksTypes.controlClass.dataSource = ds;
        UI.cbShowLinksTypes.controlClass.focusedIndex = fidx;
        
        for (let i = 1; i <= 3; i++) {
            let customURL = app.getValue(`showLinks_CustomSearchEngine_${i}`, '');
            let customTitle = app.getValue(`showLinks_CustomSearchEngineTitle_${i}`, '');
            let enableCustom = app.getValue(`showLinks_CustomSearchEngineEnabled_${i}`, false);
            
            UI[`cbEnableCustom${i}`].controlClass.checked = enableCustom;
            UI[`editCustomSearch${i}`].controlClass.value = customURL;
            UI[`editCustomSearchTitle${i}`].controlClass.value = customTitle;
        }
    },

    save: function (panel, addon) {
        var UI = getAllUIElements(panel);
        
        var sett = window.settings.get();
        sett.Options.ActiveTrackShortcutType = TrackShortcuts[UI.cbShowLinksTypes.controlClass.focusedIndex].id;
        
        window.settings.set(sett);
        app.flushState();
        
        for (let i = 1; i <= 3; i++) {

            //if customURL does not start with http, add it to the start
            let customURL = UI[`editCustomSearch${i}`].controlClass.value;
            let customTitle = UI[`editCustomSearchTitle${i}`].controlClass.value;
            let enableCustom = UI[`cbEnableCustom${i}`].controlClass.checked;
            if (customURL && !customURL.startsWith('http://') && !customURL.startsWith('https://')) {
                customURL = 'http://' + customURL;
            }
            
            app.setValue(`showLinks_CustomSearchEngine_${i}`, customURL);
            app.setValue(`showLinks_CustomSearchEngineEnabled_${i}`, enableCustom);
            app.setValue(`showLinks_CustomSearchEngineTitle_${i}`, customTitle);
        }
        
        //Update the links in main window if already installed
        var win = app.dialogs.getMainWindow();
        if (typeof win.getValue('ShowLinksSetup') == 'function') 
            win.getValue('ShowLinksSetup')();
    }
}
