// Native
import path from 'path';
import fs from 'fs-extra';

// Packages
import test from 'ava';
import {_electron, ElectronApplication} from 'playwright';

let app: ElectronApplication;

test.before(async () => {
  let pathToBinary;

  switch (process.platform) {
    case 'linux':
      pathToBinary = path.join(__dirname, '../dist/linux-unpacked/hyper');
      break;

    case 'darwin':
      pathToBinary = path.join(__dirname, '../dist/mac/Hyper.app/Contents/MacOS/Hyper');
      break;

    case 'win32':
      pathToBinary = path.join(__dirname, '../dist/win-unpacked/Hyper.exe');
      break;

    default:
      throw new Error('Path to the built binary needs to be defined for this platform in test/index.js');
  }

  app = await _electron.launch({
    executablePath: pathToBinary
  });
  await app.firstWindow();
  await new Promise((resolve) => setTimeout(resolve, 5000));
});

test.after(async () => {
  await app
    .evaluate(async ({BrowserWindow, desktopCapturer, screen}) => {
      // eslint-disable-next-line prefer-const
      let {width, height, ...position} = BrowserWindow.getFocusedWindow()!.getBounds();
      const {scaleFactor} = screen.getDisplayNearestPoint(position);
      width *= scaleFactor;
      height *= scaleFactor;
      const sources = await desktopCapturer.getSources({types: ['window'], thumbnailSize: {width, height}});
      return sources[0].thumbnail.toPNG().toString('base64');
    })
    .then((img) => Buffer.from(img || '', 'base64'))
    .then(async (imageBuffer) => {
      await fs.writeFile(`dist/tmp/${process.platform}_test.png`, imageBuffer);
    });
  await app.close();
});

test('see if dev tools are open', async (t) => {
  t.false(await app.evaluate(({webContents}) => webContents.getFocusedWebContents().isDevToolsOpened()));
});
