import electron from 'electron';
import path from 'path';
import url from 'url';
import Observable from '../observable';

const app = electron.app;
const BrowserWindow = electron.BrowserWindow;

/* disgusting hack to prevent electron from backgrounding */

electron.powerSaveBlocker.start('prevent-app-suspension');

class EventCapture extends Observable {

  createWindow () {
    this.window = new BrowserWindow({
      width: 500,
      height: 500,
      frame: false,
      show: false,
      resizable: false,
      movable: false,
      minimizable: false,
      maximizable: false,
      fullscreenable: false,
      skipTaskbar: true,
      hasShadow: false,
      transparent: true,
      backgroundColor: '#00000000',
      webPreferences: {
        nodeIntegration: true,
        contextIsolation: false,
        enableRemoteModule: true,
      },
    });
    this.window.loadURL(url.format({
      pathname: path.join(__dirname, 'event-capture.html'),
      protocol: 'file:',
      slashes: true,
    }));
    this.window.on('closed', () => {
      this.window = null;
    });
    this.window.webContents.on('before-input-event', (event, input) => {
      if (!input || input.type !== 'keyDown') {
        return;
      }
      const modifiers = input.modifiers || [];
      const key = String(input.key || '').toLowerCase();
      const command = input.meta || modifiers.indexOf('meta') !== -1 || modifiers.indexOf('command') !== -1;
      const control = input.control || modifiers.indexOf('control') !== -1;
      const alt = input.alt || modifiers.indexOf('alt') !== -1 || modifiers.indexOf('option') !== -1;
      if (command && alt && key === 'c') {
        event.preventDefault();
        this.trigger('shortcut', 'clipboard-copy');
        return;
      }
      if (command && alt && key === 'v') {
        event.preventDefault();
        this.trigger('shortcut', 'clipboard-paste');
        return;
      }
      if (command && key === 'tab') {
        event.preventDefault();
        this.trigger('shortcut', input.shift ? 'command-shift-tab' : 'command-tab');
        return;
      }
      const direction = {
        arrowup: 'up',
        up: 'up',
        arrowdown: 'down',
        down: 'down',
        arrowleft: 'left',
        left: 'left',
        arrowright: 'right',
        right: 'right',
      }[key];
      if (control && direction) {
        event.preventDefault();
        this.trigger('shortcut', `control-${direction}`);
      }
    });
    this.window.setMenuBarVisibility(false);
    this.window.setAlwaysOnTop(true, 'screen-saver');
    if (this.window.setVisibleOnAllWorkspaces) {
      this.window.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
    }
    this.window.setPosition(250, 250);
    this.window.hide();
  }

  constructor() {
    super();
    this.window = null;
    app.on('ready', this.createWindow.bind(this));
    app.on('window-all-closed', function () {
      if (process.platform !== 'darwin') {
        app.quit();
      }
    });
    app.on('activate', function () {
      if (this.window === null) {
        this.createWindow();
      }
    });
  }

  start(options = {}) {
    if (this.window) {
      const shouldFocus = options.focus !== false;
      if (shouldFocus) {
        this.window.show();
        this.window.focus();
      } else if (typeof this.window.showInactive === 'function') {
        this.window.showInactive();
      } else {
        this.window.show();
        this.window.blur();
      }
    }
  }

  stop() {
    if (this.window) {
      this.window.hide();
    }
  }

}

export default (new EventCapture);
