"use strict";

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

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

var _child_process = require("child_process");

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 }; }

//Getting variables from flag and exe
let cwd = process.cwd();
let options = (0, _utils.getOptions)();
let {
  ssTest: {
    seleniumHub,
    referBranch,
    currentBranch,
    enableImage,
    pathToSubProjects,
    isImpactRun,
    isStore,
    dbURL,
    tableName,
    serviceName,
    tokenGit
  },
  docs: {
    server
  }
} = options;
let resultFileName = `${cwd}/result.json`;
let docsServerInstance; // let impactedComponentList = null;

let url = (0, _utils.getServerURL)(Object.assign(server, {
  host: server.iphost
}), 'ht' + 'tp');
let commitID = (0, _child_process.execSync)('git log --first-parent --pretty="format:%H" --max-count 2', {
  cwd: cwd
}).toString() || '';
let [latestCommitHash, lastCommitHash] = commitID.split('\n');
let script = 'var Objlist = {};for (i in componentList) {try { if (componentList[i].prototype.isReactComponent) Objlist[i] = componentList[i].docs.componentGroup;}catch (err) {console.log(i, err);}};if (Object.keys(Objlist).length < 5) {Objlist = false;}return Objlist;'; // Variable getting part end //

(0, _jsonHelper.jsonHelper)(resultFileName, 'tests.screenshotTest.compareBranch', referBranch);
(0, _jsonHelper.jsonHelper)(resultFileName, 'tests.screenshotTest.TestBranch', currentBranch);
(0, _jsonHelper.jsonHelper)(resultFileName, 'tests.screenshotTest.isExecuted', true);
(0, _utils.log)('Test Branch Name :- ', currentBranch); // function declaration part //

let closeServer = serverInstance => {
  (0, _utils.log)('Docs service stopped!');
  let {
    http,
    https,
    wdm
  } = serverInstance;
  http.close();
  https.close();
  wdm.close();
}; //eslint-disable-next-line


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, message) => {
  if (status) {
    (0, _jsonHelper.jsonHelper)(resultFileName, 'tests.screenshotTest.endTime', Date.now());
    (0, _jsonHelper.jsonHelper)(resultFileName, 'tests.screenshotTest.isPassed', true);
    (0, _jsonHelper.jsonHelper)(resultFileName, 'tests.screenshotTest.result', resultObject);
    (0, _jsonHelper.jsonHelper)(resultFileName, 'tests.screenshotTest.message', message);
    (0, _jsonHelper.setTestInfoStatus)(resultFileName, true);
    (0, _utils.log)(message); //'Screenshot test completed successfully.'

    closeServer(docsServerInstance);
  } else {
    (0, _jsonHelper.jsonHelper)(resultFileName, 'tests.screenshotTest.isPassed', false);
    (0, _jsonHelper.jsonHelper)(resultFileName, 'tests.screenshotTest.message', message);
    (0, _jsonHelper.setTestInfoStatus)(resultFileName, false);
    (0, _jsonHelper.jsonHelper)(resultFileName, 'testInfo.failures', {
      type: 'screenshotTest',
      message
    });
    closeServer(docsServerInstance);
    (0, _jsonHelper.jsonHelper)(resultFileName, 'tests.screenshotTest.endTime', Date.now());
    throw new Error('Component list undefined! Please check component Docs');
  }
};

let run = (listOfComponents = [], configObject) => {
  docsServerInstance = (0, _docsServerCore.default)(true);
  docsServerInstance.app.use((0, _SSTMiddleware.default)(docsServerInstance.compiler, mData => {
    if (isImpactRun) {
      let finalResultObject = (0, _utils.getComponents)(mData, listOfComponents);
      configObject.impactedComponentList = finalResultObject.impactedComponentList;
      configObject.unDocsComList = finalResultObject.unDocsComList;
    }

    _screenshotTest.default.run(configObject, (status, finalObj, message) => {
      onSStestEnd(status, finalObj, message);
    });
  }));
};

(0, _jsonHelper.jsonHelper)(resultFileName, 'tests.screenshotTest.startTime', Date.now());
(0, _utils.ssTestHack)();
let scrObject = {
  currentBranch,
  referBranch,
  latestCommitHash,
  lastCommitHash,
  seleniumHub,
  url: `${url}/docs/components.html`,
  browser: 'chrome',
  enableImage,
  dbURL,
  isImpactRun,
  isStore,
  script,
  tableName
};

if (isImpactRun) {
  if (!isStore) {
    (0, _utils.getDependenciesImpactList)(currentBranch, referBranch, tokenGit, serviceName).then(impactedList => {
      run(impactedList, scrObject);
    });
  } else {
    (0, _utils.getDependenciesImpactList)(latestCommitHash, lastCommitHash, tokenGit, serviceName).then(impactedList => {
      run(impactedList, scrObject);
    });
  }
} else {
  run([], scrObject);
}