/**
 * Module for Menu functions.
 */
import { app, Menu, MenuItem, dialog } from 'electron';
import path from 'path';
import { fileURLToPath } from 'url';
import settingsService from './settings-service.js';
import { mainWindow } from '../main.js'; // this is bad practice, however i dont have any other good way of doing it
let zoom = 1;

const menuService = {};

menuService.createMenu = (window) => {
    let converse;
    const application = new Menu();

    application.append(new MenuItem({
        label: 'RS Desktop',
        submenu: (converse = Menu.buildFromTemplate([
            {
                label: 'Reconnect',
                accelerator: 'CmdOrCtrl+R',
                click: () => {
                    window.show();
                    window.loadFile('index.html').catch((reason) => {
                        console.log(reason);
                        app.isQuitting = true;
                        app.quit();
                    });
                }
            },
            { type: 'separator' },
            {
                label: 'Minimize on close',
                type: 'checkbox',
                id: 'minimize-on-close',
                checked: settingsService.get('minimizeOnClose'),
                click: () => {
                    settingsService.set('minimizeOnClose', converse.getMenuItemById('minimize-on-close').checked);
                }
            },
            {
                label: 'Hide Menubar',
                type: 'checkbox',
                id: 'hide-menubar',
                checked: settingsService.get('hideMenubar'),
                accelerator: 'alt', // just felt like this would be a good addition in case the user accidentally hides the alt bar
                click: () => {
                    const menuItem = converse.getMenuItemById('hide-menubar');
                    settingsService.set('hideMenubar', menuItem.checked);
                    window.setAutoHideMenuBar(menuItem.checked);
                }
            },
            {
                label: 'Encrypt by default',
                type: 'checkbox',
                id: 'encrypt-by-default',
                checked: settingsService.get('omemo_default'),
                click: () => {
                    const menuItem = converse.getMenuItemById('encrypt-by-default');
                    settingsService.set('omemo_default', menuItem.checked);
                }
            },
            { type: 'separator' },
            {
                label: 'Help', // migrate this here
                      submenu: Menu.buildFromTemplate([
                          {
                             label: 'Debug info',
                              accelerator: 'F12',
                              click: () => {
                                 window.webContents.openDevTools();
                              }
                          },
                      ])},
            
                {
                    label: 'Navigation', // migrate this here
                          submenu: Menu.buildFromTemplate([
                            {
                                label: '<',
                                accelerator: 'Alt+Left', // for this it was alt left arrow key
                                click() {
                                  if (mainWindow.webContents.canGoBack()) {
                                    mainWindow.webContents.goBack();
                                  }
                                }
                              },
                              {
                                  label: '>',
                                  accelerator: 'Alt+Right', // shortcut for this was alt and right arrow key i think
                                  click() {
                                    if (mainWindow.webContents.canGoForward()) {
                                        mainWindow.webContents.goForward();
                                    }
                                  }
                                },
                              {
                                label: '🔄',
                                click() {
                                    mainWindow.reload();
                                }
                              },
                          ])},
                          { type: 'separator' },
                {
                label: 'Reset Zoom',
                accelerator: 'Ctrl+0', // shortcut like in chrome/firefox
                click() {
                  zoom = 1
                  mainWindow.webContents.setZoomFactor(zoom)
                }
              },
            { type: 'separator' },
            {
                label: 'Quit',
                accelerator: 'CmdOrCtrl+Q',
                click: () => {
                    app.isQuitting = true;
                    app.quit();
                },
            },
        ]))
    }));

    application.append(new MenuItem({
        label: 'Edit', // i dont know what the point of this is, we have keyboards and they use the same shortcuts, but i will leave this alone for now
        submenu: Menu.buildFromTemplate([
            { label: 'Undo', accelerator: 'CmdOrCtrl+Z', role: 'undo' },
            { label: 'Redo', accelerator: 'Shift+CmdOrCtrl+Z', role: 'redo' },
            { label: 'Cut', accelerator: 'CmdOrCtrl+X', role: 'cut' },
            { label: 'Copy', accelerator: 'CmdOrCtrl+C', role: 'copy' },
            { label: 'Paste', accelerator: 'CmdOrCtrl+V', role: 'paste' },
            { label: 'Select All', accelerator: 'CmdOrCtrl+A', role: 'selectAll' },
        ])
    }));

    application.append(new MenuItem({
        label: 'Themes',
        submenu: Menu.buildFromTemplate([
            {
                label: 'Coming soon',
                click: () => {
                    window.webContents.openDevTools(); // TODO: theme switching
                }
            
          //  {
         ///       label: 'Night',
          //      click: () => {
           //         window.webContents.openDevTools(); // TODO: proper dark mode
          //      }
          //  },
          //  {
         //       label: 'Orange',
         //       click: () => {
          //          window.webContents.openDevTools(); // Orange & white theme
          //      }
         //   },
         //   {
          ////      label: 'Orange + Night',
          //      click: () => {
           //         window.webContents.openDevTools(); // Orange & dark theme
          //      }
           // },
           // {
            //    label: 'Fruit Tiger Aero',
            // //   click: () => {
                  //  window.webContents.openDevTools(); // Blue & green theme
                
            }
        ])
    }));

    Menu.setApplicationMenu(application);
};

export default menuService;
