const { createLogEvent } = require("../web-sockets/createLogEvent");

const IMEX_CDK_USER = process.env.IMEX_CDK_USER,
  IMEX_CDK_PASSWORD = process.env.IMEX_CDK_PASSWORD;
const CDK_CREDENTIALS = {
  password: IMEX_CDK_PASSWORD,
  username: IMEX_CDK_USER
};

exports.CDK_CREDENTIALS = CDK_CREDENTIALS;
const cdkDomain =
  process.env.NODE_ENV === "production" ? "https://3pa.dmotorworks.com" : "https://uat-3pa.dmotorworks.com";

function CheckCdkResponseForError(socket, soapResponse) {
  if (!soapResponse[0]) {
    //The response was null, this might be ok, it might not.
    createLogEvent(
      socket,
      "warn",
      `Warning detected in CDK Response - it appears to be null. Stack: ${new Error().stack}`
    );
    return;
  }

  const ResultToCheck = soapResponse[0].return;

  if (Array.isArray(ResultToCheck)) {
    ResultToCheck.forEach((result) => checkIndividualResult(socket, result));
  } else {
    checkIndividualResult(socket, ResultToCheck);
  }
}

exports.CheckCdkResponseForError = CheckCdkResponseForError;

function checkIndividualResult(socket, ResultToCheck) {
  if (
    ResultToCheck.errorLevel === 0 ||
    ResultToCheck.errorLevel === "0" ||
    ResultToCheck.code === "success" ||
    (!ResultToCheck.code && !ResultToCheck.errorLevel)
  )
    //TODO:  Verify that this is the best way to detect errors.
    return;
  else {
    createLogEvent(socket, "ERROR", `Error detected in CDK Response - ${JSON.stringify(ResultToCheck, null, 2)}`);

    throw new Error(`Error found while validating CDK response for ${JSON.stringify(ResultToCheck, null, 2)}:`);
  }
}

exports.checkIndividualResult = checkIndividualResult;

//const cdkDomain = "https://uat-3pa.dmotorworks.com";
exports.default = {
  // VehicleSearch: `${cdkDomain}/pip-vehicle/services/VehicleSearch?wsdl`,
  HelpDataBase: `${cdkDomain}/pip-help-database-location/services/HelpDatabaseLocation?wsdl`,
  AccountingGLInsertUpdate: `${cdkDomain}/pip-accounting-gl/services/AccountingGLInsertUpdate?wsdl`,
  VehicleInsertUpdate: `${cdkDomain}/pip-vehicle/services/VehicleInsertUpdate?wsdl`,
  CustomerInsertUpdate: `${cdkDomain}/pip-customer/services/CustomerInsertUpdate?wsdl`,
  CustomerSearch: `${cdkDomain}/pip-customer/services/CustomerSearch?wsdl`,
  VehicleSearch: `${cdkDomain}/pip-vehicle/services/VehicleSearch?wsdl`,
  ServiceHistoryInsert: `${cdkDomain}/pip-service-history-insert/services/ServiceHistoryInsert?wsdl`
};

// The following login credentials will be used for all PIPs and all environments (User Acceptance Testing and Production).
// Only the URLs will change from https://uat-3pa.dmoto... to https://3pa.dmoto... or https://api-dit.connect... to https://api.connect...
// Accounting GL/Accounting GL WIP Update - https://uat-3pa.dmotorworks.com/pip-accounting-gl/services/AccountingGLInsertUpdate?wsdl
// Customer Insert Update - https://uat-3pa.dmotorworks.com/pip-customer/services/CustomerInsertUpdate?wsdl
// Help Database Location - https://uat-3pa.dmotorworks.com/pip-help-database-location/services/HelpDatabaseLocation?wsdl
// Parts Inventory Insert Update - https://uat-3pa.dmotorworks.com/pip-parts-inventory/services/PartsInventoryInsertUpdate?wsdl
// Purchase Order Insert - https://uat-3pa.dmotorworks.com/pip-purchase-order/services/PurchaseOrderInsert?wsdl
// Repair Order MLS Insert Update - https://uat-3pa.dmotorworks.com/pip-repair-order-mls/services/RepairOrderMLSInsertUpdate?wsdl
// Repair Order Parts Insert Update - https://uat-3pa.dmotorworks.com/pip-repair-order-parts/services/RepairOrderPartsInsertUpdate?wsdl
// Service History Insert - https://uat-3pa.dmotorworks.com/pip-service-history-insert/services/ServiceHistoryInsert?wsdl
// Service Repair Order Update - https://uat-3pa.dmotorworks.com/pip-service-repair-order/services/ServiceRepairOrderUpdate?wsdl
// Service Vehicle Insert Update - https://uat-3pa.dmotorworks.com/pip-vehicle/services/VehicleInsertUpdate?wsdl
