"use strict";

var _screenshotTest = _interopRequireDefault(require("@zohodesk/screenshot-test"));

var _jsonHelper = require("../utils/jsonHelper");

var _utils = require("../utils");

var _docsServerCore = _interopRequireDefault(require("./docsServerCore"));

var _SSTMiddleware = _interopRequireDefault(require("../middlewares/SSTMiddleware"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

let options = (0, _utils.getOptions)();
let impactedComponentObject = null;
let docsServerInstance;
let {
  ssTest: {
    seleniumHub,
    referBranch,
    currentBranch,
    enableImage,
    pathToSubProjects
  },
  docs: {
    server
  }
} = options;

function closeServer(serverInstance) {
  (0, _utils.log)('Docs service stopped!');
  let {
    http,
    https,
    wdm
  } = serverInstance;
  http.close();
  https.close();
  wdm.close();
}

(0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, 'tests.screenshotTest.compareBranch', referBranch);
(0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, 'tests.screenshotTest.TestBranch', currentBranch);
(0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, 'tests.screenshotTest.isExecuted', true);
(0, _utils.log)('Test Branch Name :- ', currentBranch);

let afterFirstScreenShotCollected = () => {
  closeServer(docsServerInstance);
  (0, _utils.switchBranch)('git', referBranch).then(() => {
    (0, _utils.log)('Reference Branch Name :- ', (0, _utils.getCurrentBranch)());
    (0, _utils.reinstallDependencies)(pathToSubProjects).then(() => {
      (0, _utils.ssTestHack)();
    });
  });
};

let onSStestEnd = (status, resultObject, isLastRun, runSSTest) => {
  if (status) {
    if (!isLastRun) {
      (0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, 'tests.screenshotTest.result', resultObject);
      runSSTest(afterFirstScreenShotCollected, true, 'reference', referBranch, impactedComponentObject);
    } else {
      (0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, 'tests.screenshotTest.endTime', Date.now());
      (0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, 'tests.screenshotTest.isPassed', true);
      (0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, 'tests.screenshotTest.result', resultObject);
      (0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, 'tests.screenshotTest.message', 'Screenshot test succesfully completed.');
      (0, _jsonHelper.setTestInfoStatus)(`${process.cwd()}/result.json`, true);
      (0, _utils.log)('Screenshot test completed succesfully.');
      closeServer(docsServerInstance);
    }
  } else {
    (0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, 'tests.screenshotTest.isPassed', false);
    (0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, 'tests.screenshotTest.message', 'Component list undefined! Please check component Docs');
    (0, _jsonHelper.setTestInfoStatus)(`${process.cwd()}/result.json`, false);
    (0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, 'testInfo.failures', {
      type: 'screenshotTest',
      message: 'Component list undefined! Please check component Docs'
    });
    closeServer(docsServerInstance);
    (0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, 'tests.screenshotTest.endTime', Date.now());
    throw new Error('Component list undefined! Please check component Docs');
  }
};

(0, _utils.ssTestHack)();

let getDocsServerInstance = beforeJobs => {
  if (typeof beforeJobs === 'function') {
    beforeJobs();
  }

  return (0, _docsServerCore.default)(true);
};

let runSSTest = (onBefore, isLastRun, mode, branch, listOfComponents) => {
  let url = (0, _utils.getServerURL)(Object.assign(server, {
    host: server.iphost
  }), 'ht' + 'tp');
  docsServerInstance = getDocsServerInstance(onBefore);
  docsServerInstance.app.use((0, _SSTMiddleware.default)(docsServerInstance.compiler, mData => {
    impactedComponentObject = (0, _utils.getComponents)(mData, listOfComponents);

    _screenshotTest.default.run({
      branch,
      seleniumHub: seleniumHub,
      url: `${url}/docs/components.html`,
      browserList: ['chrome'],
      mode,
      enableImage,
      impactedComponentObject,
      script: 'var Objlist = {};for (i in Component) {try { if (Component[i].prototype.isReactComponent) Objlist[i] = Component[i].docs.componentGroup;}catch (err) {console.log(i, err);}};if (Object.keys(Objlist).length < 5) {Objlist = false;}return Objlist;'
    }, (status, finalObj) => {
      onSStestEnd(status, finalObj, isLastRun, runSSTest);
    });
  }));
};

(0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, 'tests.screenshotTest.startTime', Date.now());
(0, _utils.getDependenciesImpactList)(currentBranch, referBranch).then(impactedList => {
  (0, _utils.log)('from here ');
  runSSTest(null, null, 'test', currentBranch, impactedList);
});