"use strict";

const {
  MAX_REAL_ESTATES_IN_EMAIL,
  APP_URL,
  STAGING
} = require("../config/appConfig");
const { AD_CATEGORY, AD_TYPE, EMAIL_FREQUENCY } = require("../common/enums");

//Tag to recognize staging from development
const stagingTag = STAGING ? "[STAGING] " : "";

const generateEmailFooter = (searchRequestId, emailFrequencyTitle) => {
  return `  <div>Trenutno ste prijavljeni da obavještenja o novim nekretninama primate <strong>${emailFrequencyTitle.toLowerCase()} </strong>.</div>
    <div>Ako želite prestati dobijati obavještenja za ovu pretragu, <a href="${APP_URL}/odjava/${searchRequestId}">odjavite ovdje</a></div>
     <div>Ako želite pogledati ili promijeniti uslove za ovu pretragu, <a href="${APP_URL}/pregled/${searchRequestId}">pogledajte ovdje</a></div>
     <br/> 
     <strong>Vaš,<br/>Kivi tim</strong>`;
};

const generateRealEstateLinks = realEstates => {
  let realEstateLinks = "";
  for (const realEstate of realEstates) {
    const { id: realEstateId, title } = realEstate;

    realEstateLinks += `<li><a href="${APP_URL}/redirect/${realEstateId}">${title}</a></li>`;
  }
  return realEstateLinks;
};

const generateNotificationEmail = (
  realEstates,
  searchRequestId,
  noAllRealEstates,
  dailyNotification = false
) => {
  const truncateList = realEstates.length > MAX_REAL_ESTATES_IN_EMAIL;

  const realEstatesToShow = truncateList
    ? realEstates.slice(0, MAX_REAL_ESTATES_IN_EMAIL)
    : realEstates;

  const allRealEstatesLink = `${APP_URL}/nekretnine/${searchRequestId}`;

  const emailFrequencyTitle = dailyNotification
    ? EMAIL_FREQUENCY.DAILY.title
    : EMAIL_FREQUENCY.ASAP.title;

  const realEstateLinks = generateRealEstateLinks(realEstatesToShow);
  const moreRealEstates = `<div>Kompletan spisak nekretnina (${noAllRealEstates}) možete pogledati na <a href="${allRealEstatesLink}">listi nekretnina</a><div>`;
  const emailFooter = generateEmailFooter(searchRequestId, emailFrequencyTitle);
  const asapMessageBody =
    realEstates.length > 1
      ? "Pronašli smo nekretnine koje odgovaraju Vašoj pretrazi"
      : "Pronašli smo nekretninu koja odgovara Vašoj pretrazi";

  const dailyMessageBody =
    realEstates.length > 1
      ? "U posljednja 24h objavljene su sljedeće nekretnine koje odgovaraju uslovima Vaše pretrage"
      : "U posljednja 24h objavljena je sljedeća nekretnina koja odgovara uslovima Vaše pretrage";

  const messageBody = dailyNotification ? dailyMessageBody : asapMessageBody;

  return `<h3>${stagingTag}Zdravo</h3>
  <h4>${messageBody}</h4>
  <div>
  ${realEstateLinks}
  <div/>
  ${moreRealEstates}
  </div>
  <br/>
  ${emailFooter}`;
};

const generateNewSearchRequestEmail = (searchRequest, matchingRealEstates) => {
  const realEstateType = AD_CATEGORY[searchRequest.realEstateType];
  let adTypeTitle = "";
  switch (searchRequest.adType) {
    case AD_TYPE.AD_TYPE_SALE.stringId:
      adTypeTitle = AD_TYPE.AD_TYPE_SALE.title;
      break;
    case AD_TYPE.AD_TYPE_RENT.stringId:
      adTypeTitle = AD_TYPE.AD_TYPE_RENT.title;
      break;
    default:
      adTypeTitle = "-";
      break;
  }
  let emailFrequencyTitle;
  switch (searchRequest.emailFrequency) {
    case EMAIL_FREQUENCY.ASAP.stringId:
      emailFrequencyTitle = EMAIL_FREQUENCY.ASAP.title;
      break;
    case EMAIL_FREQUENCY.DAILY.stringId:
      emailFrequencyTitle = EMAIL_FREQUENCY.DAILY.title;
      break;
  }

  const {
    id,
    gardenSizeMin,
    gardenSizeMax,
    sizeMin,
    sizeMax,
    priceMin,
    priceMax
  } = searchRequest;

  const realEstateLinks = generateRealEstateLinks(matchingRealEstates);

  const instantRealEstatesText = `<br/>
       <div>
        U međuvremenu pogledajte neke od nedavno objavljenih nekretnina koje odgovaraju Vašim uslovima pretrage :<br/>
        ${realEstateLinks}
       </div>`;

  const gardenSize = realEstateType.hasGardenSize
    ? `<div><strong>Kvadratura okućnice: Od ${gardenSizeMin} do ${gardenSizeMax} m2</strong></div>`
    : ``;

  const emailFooter = generateEmailFooter(id, emailFrequencyTitle);

  return `<h3>${stagingTag}Zdravo</h3>
  <div>Naručili ste da Vam javimo ako se nekretnina sa navedenim uslovima pojavi u oglasima:</div>
  <br/>
  <div>
    <div><strong>Tip nekretnine: </strong>${realEstateType.title}</div>
    <div><strong>Vrsta oglasa: </strong>${adTypeTitle}</div>
    <div><strong>Kvadratura nekretnine:</strong> Od ${sizeMin} do ${sizeMax} m2</div>
    ${gardenSize}
    <div><strong>Cijena:</strong> ${priceMin} do ${priceMax} KM</div>
  </div>
  ${matchingRealEstates.length > 0 ? instantRealEstatesText : ""}
  <br/>
  ${emailFooter}`;
};

const generateEmailSubject = (numberOfRealEstates, singleRealEstateTitle) => {
  if (numberOfRealEstates === 1) {
    return `${stagingTag}Kivi: ${singleRealEstateTitle}`;
  }

  const leastSignificantDigit = numberOfRealEstates % 10;
  const numberWithoutLastDigit = Math.floor(numberOfRealEstates / 10);
  const secondLeastSignificantDigit = numberWithoutLastDigit % 10;

  if (leastSignificantDigit === 1 && secondLeastSignificantDigit !== 1) {
    return `${stagingTag}Kivi : ${numberOfRealEstates} nova nekretnina`;
  }

  if (
    leastSignificantDigit >= 2 &&
    leastSignificantDigit <= 4 &&
    secondLeastSignificantDigit !== 1
  ) {
    return `${stagingTag}Kivi: ${numberOfRealEstates} nove nekretnine`;
  }

  return `${stagingTag}Kivi: ${numberOfRealEstates} novih nekretnina`;
};

const generateCheckUpEmail = searchRequest => {
  const realEstateType = AD_CATEGORY[searchRequest.realEstateType];
  const {
    id,
    gardenSizeMin,
    gardenSizeMax,
    sizeMin,
    sizeMax,
    priceMin,
    priceMax
  } = searchRequest;

  const gardenSize = realEstateType.hasGardenSize
    ? `<div><strong>Kvadratura okućnice: Od ${gardenSizeMin} do ${gardenSizeMax} m2</strong></div>`
    : ``;

  const emailFooter = generateEmailFooter(id);

  return `<h3>Zdravo</h3>
  <div><strong>Kivi tim traži nekretnine za Vas i kada to ne vidite.</strong></div>
  <br />
  <div>Vaša trenutno aktivna pretraga je:</div>
  <br/>
  <div>
    <div><strong>Tip nekretnine: </strong>${realEstateType.title}</div>
    <div><strong>Kvadratura nekretnine:</strong> Od ${sizeMin} do ${sizeMax} m2</div>
    ${gardenSize}
    <div><strong>Cijena:</strong> ${priceMin} do ${priceMax} KM</div>
  </div>
  <br/>
  ${emailFooter}`;
};

module.exports = {
  generateNotificationEmail,
  generateNewSearchRequestEmail,
  generateEmailSubject,
  generateCheckUpEmail
};
