const getTomorrwDate = () => {
  const today = new Date();
  const tomorrow = new Date(today);

  return new Date(tomorrow.setDate(tomorrow.getDate() + 1));
};

const getCounterDate = (countDownDate: Date) => {
  const now = new Date().getTime();
  const timeleft = new Date(countDownDate).getTime() - now;

  // Calculating the days, hours, minutes and seconds left
  const d = Math.floor(timeleft / (1000 * 60 * 60 * 24));
  const h = Math.floor((timeleft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  const m = Math.floor((timeleft % (1000 * 60 * 60)) / (1000 * 60));
  const s = Math.floor((timeleft % (1000 * 60)) / 1000);
  return timeleft > 0 ? { d, h, m, s, timeleft } : { d: -1, h: -1, m: -1, s: -1, timeleft };
};

const countDownTimer = (countDownDate: Date, setTimeText: (str: string) => void) => {
  const id = setInterval(() => {
    const { d, h, m, s, timeleft } = getCounterDate(countDownDate);

    let hours: number | string = Math.floor((timeleft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    hours = hours > 9 ? hours : `0${hours}`;
    hours = Number(d) ? (Number(d) * 24 + Number(h)) : h;

    const minutes = `0${m}`.slice(-2);;
    const seconds = `0${s}`.slice(-2);;

    const text = `${hours}:${minutes}:${seconds}`;
    setTimeText(text);

    if (!timeleft || timeleft < 0) {
      clearInterval(id);
      setTimeText('00:00:00');
    }
  }, 1000);

  return { id };
};

export interface IDateFormat {
  d: number;
  h: number;
  m: number;
  s: number;
}

const countDownDayTimer = (countDownDate: Date, setDate: (date: IDateFormat) => void) => {
  const id = setInterval(() => {
    const { d, h, m, s, timeleft } = getCounterDate(countDownDate);

    setDate({ d, h, m, s });

    if (!timeleft || timeleft < 0) {
      clearInterval(id);
      setDate({ d: -1, h: -1, m: -1, s: -1 });
    }
  }, 1000);

  return { id };
};

export const getStringToDate = (str: string) => {
  const year = Number(str.substring(0, 4));
  const month = Number(str.substring(4, 6));
  const day = Number(str.substring(6, 8));
  const hour = Number(str.substring(8, 10));
  const minute = Number(str.substring(10, 12));
  const second = Number(str.substring(12, 14));

  return new Date(year, month - 1, day, hour, minute, second);
};

const getYYYYMMDDHHMMSS = (date: Date | null) => {
  if (date) {
    const year = date.getFullYear();
    const month = ('0' + (1 + date.getMonth())).slice(-2);
    const day = ('0' + date.getDate()).slice(-2);
    const hh = ('0' + date.getHours()).slice(-2);
    const mm = ('0' + date.getMinutes()).slice(-2);
    const ss = ('0' + date.getSeconds()).slice(-2);

    return `${year}/${month}/${day} ${hh}:${mm}:${ss}`;
  } else {
    return '';
  }
};

const getTimezoneToDate = (str: string) => {
  const date = str.split('T')[0];
  const time = str.split('T')[1];

  const year = date.split('-')[0];
  const month = date.split('-')[1];
  const day = date.split('-')[2];
  const hh = time.split(':')[0];
  const mm = time.split(':')[1];
  const ss = time.split(':')[2].replace('Z', '');

  return new Date(`${year}/${month}/${day} ${hh}:${mm}:${ss}`);
};

const getConvertKST = (date: Date) => {
  return new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString();
};

export default {
  getTomorrwDate,
  countDownTimer,
  countDownDayTimer,
  getStringToDate,
  getYYYYMMDDHHMMSS,
  getTimezoneToDate,
  getConvertKST
};
