/* eslint-disable no-plusplus */
const applyAllWinnersBonus = (outcomes, totalReturn, bookmakerBonus) => {
  const voidOrLost = outcomes.filter((outcome) => outcome === 'lost' || outcome === 'void');

  if (voidOrLost.length === 0) {
    totalReturn += (Number(bookmakerBonus) / 100) * totalReturn;
  }

  return totalReturn;
};

const applyOneLoserBonus = (outcomes, totalReturn, oneLoserBonus) => {
  let lostOutcomesCounter = 0;
  let voidOutcomesCounter = 0;

  outcomes.forEach((outcome) => {
    if (outcome === 'lost') {
      lostOutcomesCounter++;
    }
    if (outcome === 'void') {
      voidOutcomesCounter++;
    }
  });

  if (lostOutcomesCounter === 1 && voidOutcomesCounter === 0) {
    totalReturn += (Number(oneLoserBonus) / 100) * totalReturn;
  }

  return totalReturn;
};

const applyOneWinnerConsolation = (outcomes, totalReturn) => {
  let lostOutcomesCounter = 0;
  let voidOutcomesCounter = 0;

  outcomes.forEach((outcome) => {
    if (outcome === 'lost') {
      lostOutcomesCounter++;
    }
    if (outcome === 'void') {
      voidOutcomesCounter++;
    }
  });

  if (lostOutcomesCounter === outcomes.length - 1 && voidOutcomesCounter === 0) {
    // totalReturn +=
    //   (Number($("#oneWinnerConsolation").val()) );
    // TODO::::::
  }

  return totalReturn;
};

const applyBookmakerBonuses = (
  betType,
  outcomes,
  stake,
  totalReturn,
  bookmakerBonus,
  oneWinnerConsolation,
  oneLoserBonus
) => {
  switch (betType.name) {
    case 'Alphabet':
      return applyAllWinnersBonus(outcomes, totalReturn, bookmakerBonus);
    case 'Lucky15':
      totalReturn = applyAllWinnersBonus(outcomes, totalReturn, bookmakerBonus);
      return totalReturn;
    case 'Lucky31':
    case 'Lucky63':
      totalReturn = applyAllWinnersBonus(outcomes, totalReturn, bookmakerBonus);
      totalReturn = applyOneLoserBonus(outcomes, totalReturn, oneLoserBonus);
      totalReturn = applyOneWinnerConsolation(outcomes, stake, totalReturn, oneWinnerConsolation);
      return totalReturn;
    default:
      return totalReturn;
  }
};

module.exports = {
  applyBookmakerBonuses,
};
