import { lottoLabels, powerballLabels, dailyLottoLabels } from '../constants/lotto';

function zeroPadding(num, places) {
  return String(num).padStart(places, '0');
}

const months = [
  'January',
  'February',
  'March',
  'April',
  'May',
  'June',
  'July',
  'August',
  'September',
  'October',
  'November',
  'December',
];

function isValidDate(d) {
  return d instanceof Date && !Number.isNaN(d);
}

export const formatDate = (date, format = 'default') => {
  let formatedDate = date;

  if (typeof date === 'string') {
    formatedDate = new Date(date);
  }

  if (!isValidDate(formatedDate)) {
    const splittedDate = date?.replace(/\s/g, '')?.split('-');

    if (!splittedDate) {
      return '';
    }

    const year = splittedDate[2];
    const month = splittedDate[1];
    const day = splittedDate[0];
    const hourNdTime = splittedDate[3];

    return `${day} ${month} ${year} - ${hourNdTime}`;
  }

  if (format === 'default') {
    const dd = String(formatedDate.getDate()).padStart(2, '0');
    const mm = String(formatedDate.getMonth() + 1).padStart(2, '0'); // January is 0!
    const yyyy = formatedDate.getFullYear();

    return `${dd}/${mm}/${yyyy}`;
  }
  if (format === 'dd/mmm/yy' || format === 'dd/MMM/yy') {
    const day = zeroPadding(formatedDate.getDate(), 2);
    const month =
      format === 'dd/MMM/yy'
        ? months[formatedDate.getMonth()]
        : months[formatedDate.getMonth()].slice(0, 3);
    const year = formatedDate.getFullYear();

    return `${day} ${month} ${year}`;
  }
};

export const formatListOfDates = (list) => {
  if (!list) return [];
  return list.map((item) => ({ ...item, drawDate: formatDate(new Date(item.drawDate)) }));
};

export const dateExists = (date, list) => {
  for (let i = 0; i < list.length; i += 1) {
    if (list[i].drawDate === date) {
      return true;
    }
  }

  return false;
};

function getLastWeekday(weekday) {
  const todaysDate = new Date();
  todaysDate.setDate(todaysDate.getDate());
  const currentDateObj = todaysDate;
  currentDateObj.setDate(currentDateObj.getDate() - ((currentDateObj.getDay() + weekday) % 7));
  return currentDateObj;
}

export const generateCustomHeadings = (moduleName, title = '') => {
  const todaysDate = new Date();

  switch (moduleName) {
    case 'lotto': {
      const dates = [getLastWeekday(1), getLastWeekday(4)];
      return `Lotto & Lotto Plus Results and Payouts for  ${formatDate(
        dates.sort((a, b) => b - a)[0],
        'dd/MMM/yy'
      )}`;
    }
    case 'powerball': {
      const dates2 = [getLastWeekday(5), getLastWeekday(2)];
      return `Powerball & Powerball Plus Results for ${formatDate(
        dates2.sort((a, b) => b - a)[0],
        'dd/MMM/yy'
      )}`;
    }
    case 'daily_lotto':
      todaysDate.setDate(todaysDate.getDate() - 1);
      return `Daily Lotto Results for ${formatDate(todaysDate, 'dd/MMM/yy')}`;
    case 'sportstake-4': {
      const sportstake4Dates = [getLastWeekday(6), getLastWeekday(3)];
      return `${title} for ${formatDate(sportstake4Dates.sort((a, b) => b - a)[0], 'dd/MMM/yy')}`;
    }
    case 'sportstake-cricket': {
      const sportstakeCricketDates = [getLastWeekday(6)];
      return `${title} for ${formatDate(
        sportstakeCricketDates.sort((a, b) => b - a)[0],
        'dd/MMM/yy'
      )}`;
    }
    case 'sportstake-rugby': {
      const sportstakeRugbyDates = [getLastWeekday(6)];
      return `${title} for ${formatDate(
        sportstakeRugbyDates.sort((a, b) => b - a)[0],
        'dd/MMM/yy'
      )}`;
    }
    default:
      return null;
  }
};

export function getApiBasedNames(moduleName) {
  switch (moduleName) {
    case 'lotto':
      return ['lotto', 'lotto-plus-1', 'lotto-plus-2'];
    case 'powerball':
      return ['powerball', 'powerball-plus'];
    case 'daily_lotto':
      return ['daily-lotto'];
    default:
      return [];
  }
}

export function getLottoLabels(lotteryName) {
  switch (lotteryName) {
    case 'lotto':
    case 'lotto-plus-1':
    case 'lotto-plus-2':
      return lottoLabels;
    case 'powerball':
    case 'powerball-plus':
      return powerballLabels;
    case 'daily-lotto':
      return dailyLottoLabels;
    default:
      return [];
  }
}

export function getLottoCtas(lotteryName, componentName = 'rollover') {
  if (componentName === 'rollover') {
    switch (lotteryName) {
      case 'lotto':
      case 'lotto-plus-1':
      case 'lotto-plus-2':
        return 'Buy Lotto Ticket Now';
      case 'powerball':
      case 'powerball-plus':
        return 'Buy Powerball Ticket Now';
      case 'daily-lotto':
        return 'Buy Daily Lotto Ticket Now';
      default:
        return '';
    }
  } else {
    switch (lotteryName) {
      case 'lotto':
      case 'lotto-plus-1':
      case 'lotto-plus-2':
        return 'Try your luck at Lotto';
      case 'powerball':
      case 'powerball-plus':
        return 'Try your luck at Powerball';
      case 'daily-lotto':
        return 'Try your luck at Daily Lotto';
      default:
        return '';
    }
  }
}

export function addZeroes(num) {
  let newNum = Number(num);

  if (String(newNum).split('.').length < 2 || String(newNum).split('.')[1].length <= 2) {
    newNum = num.toFixed(2);
  }

  return newNum;
}

export function commaSeperate(num) {
  // if zero
  if (num === 0) return num.toFixed(2);

  // If anything other than zero
  if (!num) return '';

  num = addZeroes(num);

  const splitNumbers = num?.split('.');
  splitNumbers[0] = splitNumbers[0]?.toString()?.replace(/\B(?=(\d{3})+(?!\d))/g, ',');

  return splitNumbers?.join('.');
}
