requirejs('utils');

actions.burn2MM4 = {
    title: _('CD/DVD (burn using MM4)...'),
    hotkeyAble: true,
    icon: 'cd',
    disabled: uitools.notMediaListSelected,
    visible: window.uitools.getCanEdit,
    execute: async function () {
        var list = await uitools.getSelectedTracklist().whenLoaded();
        if (list.count === 0) {
            return;
        }

        app.utils.getTempFilePathAsync().then((tempDir) => {
            var m3uPath = getFileFolder(tempDir) + 'burn2MM4.m3u';
            app.playlists.saveToPlaylistFile(list, m3uPath, '').then(() => {
                var MM4path = app.getValue('burn2MM4exePath', 'C:\\Program Files (x86)\\MediaMonkey\\MediaMonkey.exe');
                app.filesystem.fileExistsAsync(MM4path).then((exists) => {
                    if (exists) {
                        app.utils.shellExecute(MM4path, '"' + m3uPath + '"');
                    } else {
                        messageDlg('MediaMonkey 4 cannot be found at: \n' + MM4path + '\n\n' + 'Please install MediaMonkey 4 (click Help to download the installer) or click OK to browse for the correct path of the MediaMonkey.exe file', 'Error', ['btnHelp', 'btnOK'], {
                            defaultButton: 'btnOK'
                        }, function (result) {
                            if (result.btnID === 'btnHelp') {
                                window.uitools.openWeb('http://www.mediamonkey.com/MediaMonkey-4_Setup.exe');
                            } else
                            if (result.btnID === 'btnOK') {
                                var promise = app.utils.dialogOpenFile('', 'exe', 'MediaMonkey executable file (*.exe)|*.exe', 'Select MediaMonkey.exe', false /* not - multiselect */ );
                                window.localPromise(promise).then(function (newPath) {
                                    if (newPath != '') {
                                        app.setValue('burn2MM4exePath', newPath);
                                        actions.burn2MM4.execute();
                                    }
                                });
                            }
                        });
                    }
                });
            });
        });
    }
}

window._menuItems.tools.action.submenu.push({
    action: actions.burn2MM4,
    order: 10,
    grouporder: 20
});

hotkeys.addHotkey('Ctrl+Shift+D', 'burn2MM4');

var __origSubmenu = actions.sendTo.submenu;
actions.sendTo.submenu = (params) => {
    return new Promise((resolve, reject) => {
        __origSubmenu(params).then((res) => {
            res.push({
                action: actions.burn2MM4,
                grouporder: 40
            });
            resolve(res);
        });
    });
};
