export function getKnokserRank(rank: number): string {
  switch (rank) {
    case 0:
      return 'Padawan';
    case 1:
      return 'Ranger';
    case 2:
      return 'Knight';
    case 3:
      return 'Master';
    case 4:
      return 'Jedi';
    default:
      return '';
  }
}

export function convertUTCDateToLocalDate(date: Date): Date {
  let newDate: Date = new Date(date); // .getTime() + date.getTimezoneOffset() * 60 * 1000);
  const offset: number = date.getTimezoneOffset() / 60;
  const hours: number = date.getHours();
  const newHours: number = hours - offset;
  newDate.setHours(newHours);
  return newDate;
}

export function declinationWord(n: number, ...titles: any[]): any {
  n = Math.abs(n);
  return titles[(n % 10 === 1 && n % 100 !== 11) ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2];
}

export function truncateToInt(n: number): number {
  return parseInt(n.toString().match(/^-?\d+(?:\.\d{0})?/)[0], 10);
}

export function formatTimeLeft(d: Date, isUTC: boolean): string {
  console.log(d);
  const minutesTime: number = (60 * 1000);
  const currentDate: Date = new Date();
  const endDate: Date = isUTC ? convertUTCDateToLocalDate(d) : d;
  const times: number = truncateToInt((endDate.getTime() - currentDate.getTime()) / minutesTime);
  const weeksMinutes: number = 168 * 60;
  const daysMinutes: number = 24 * 60;

  let timeLeft: string = '';
  if (times >= weeksMinutes) { // >= 1 week
    const weeks: number = Math.floor(times / weeksMinutes);
    const days: number = Math.floor((times - (weeks * weeksMinutes)) / daysMinutes);
    timeLeft = `${weeks}w`;
    if (days > 0 ) {
      timeLeft =  timeLeft + `:${days}d`;
    }
  } else if (times >= daysMinutes) { // >= 1 day
    const days: number = Math.floor(times / daysMinutes);
    const hours: number = Math.floor((times - (days * daysMinutes)) / 60);
    timeLeft = `${days}d`;
    if (hours > 0 ) {
      timeLeft =  timeLeft + `:${hours}h`;
    }
  } else {
    const hours: number = Math.floor(times / 60);
    const minutes: number = Math.floor((times - (hours * 60)));
    // if (hours === 0 ) {
    //   timeLeft = `${minutes}m`;
    // } else {
      timeLeft = `${hours}h:${minutes}m`; // `${addLeftZeroPadding(hours, 2)}:${addLeftZeroPadding(minutes, 2)} hours`;
    // }
  }
  return timeLeft; // + ` left`;
}

function addLeftZeroPadding(n: number, d: number): string {
  let s: string = n.toFixed(0);
  while (s.length < d) {
    s = '0' + s;
  }
  return s;
}
