import $ from 'jquery';
import buildUrl from './build-url';
import formatNumber from './format-number';
import sendGetDaysRequestForTurnOfTheYear from './send-get-days-request-for-turn-of-the-year';

export default function sendGetDaysRequest(urlPrefix, startDate, toDate, dayLength, personId, element) {

  $(element).empty();

  if (startDate !== undefined && toDate !== undefined && startDate !== null && toDate !== null) {

    if (startDate <= toDate) {

      var startDateString = startDate.getFullYear() + '-' + padZeros(startDate.getMonth() + 1) + '-' + padZeros(startDate.getDate());
      var toDateString = toDate.getFullYear() + '-' + padZeros(toDate.getMonth() + 1) + '-' + padZeros(toDate.getDate());

      var requestUrl = urlPrefix + "/workdays";

      var url = buildUrl(requestUrl, startDateString, toDateString, dayLength, personId);

      $.get(url, function (data) {

        var workDays = data.response.workDays;

        var text;

        if(isNaN(workDays)) {
          text = "Ung&uuml;ltiger Zeitraum"
        } else if (workDays == 1) {
          text = formatNumber(workDays) + " Tag";
        } else {
          text = formatNumber(workDays) + " Tage";
        }

        $(element).html(text);

        if (startDate.getFullYear() != toDate.getFullYear()) {
          $(element).append('<span class="days-turn-of-the-year"></span>');
          sendGetDaysRequestForTurnOfTheYear(urlPrefix, startDate, toDate, dayLength, personId, element + ' .days-turn-of-the-year');
        }

      });
    }

  }

}

function padZeros(number){
  return number <10? '0'+ number:''+ number;
}

// disabling date-fns#format is ok since we're formatting dates for api requests
// eslint-disable-next-line @urlaubsverwaltung/no-date-fns
import { isAfter, format, getYear } from 'date-fns'
import buildUrl from './build-url';
import formatNumber from './format-number';
import sendGetDaysRequestForTurnOfTheYear from './send-get-days-request-for-turn-of-the-year';
import { getJSON } from '../js/fetch'

export default async function sendGetDaysRequest(urlPrefix, startDate, toDate, dayLength, personId, elementSelector) {

  const element = document.querySelector(elementSelector);
  element.innerHTML = "";

  if (!startDate && !toDate) {
    return;
  }

  if (isAfter(startDate, toDate)) {
    return;
  }

  const requestUrl = urlPrefix + "/workdays";

  const startDateString = format(startDate, "YYYY-MM-DD");
  const toDateString = format(toDate, "YYYY-MM-DD");
  const url = buildUrl(requestUrl, startDateString, toDateString, dayLength, personId);

  const data = await getJSON(url);
  const workDays = data.response.workDays;

  var text;

  if(isNaN(workDays)) {
    text = "Ung&uuml;ltiger Zeitraum"
  } else if (workDays === 1) {
    text = formatNumber(workDays) + " Tag";
  } else {
    text = formatNumber(workDays) + " Tage";
  }

  element.innerHTML = text;

  if (getYear(startDate) !== getYear(toDate)) {
    element.innerHTML += "<span class=\"days-turn-of-the-year\"></span>";
    await sendGetDaysRequestForTurnOfTheYear(urlPrefix, startDate, toDate, dayLength, personId, `${elementSelector} .days-turn-of-the-year`);
  }
}

refactor send-get-days-request js