import moment from 'moment';
import 'moment/dist/locale/th';
import 'moment/dist/locale/en-gb';
import { useI18n } from 'vue-i18n';

moment.locale('en-gb');

export function setLocale(locale: string) {
  moment.locale(locale);
}

export function dateFormat(
  date?: string | Date | null,
  fullmonth = false,
  time = false,
  number = false,
) {
  const m = moment(date);

  if (!m.isValid()) return '';

  let yearOffset = 0;
  if (moment.locale() === 'th') {
    yearOffset = 543;
  }

  if (time) return m.format('HH:mm');
  if (number) {
    let formattedNumberDate = m.format('L');
    if (yearOffset) {
      const adjustedYear = m.year() + yearOffset;
      formattedNumberDate = formattedNumberDate.replace(
        m.year().toString(),
        adjustedYear.toString(),
      );
    }
    return formattedNumberDate;
  }

  const monthFormat = fullmonth ? 'MMMM' : 'MMM';
  const formattedDate = m.format(
    `DD ${monthFormat} YYYY ${time ? ' HH:mm' : ''}`,
  );

  // Adjust year for Buddhist calendar if locale is 'th'
  if (yearOffset) {
    const adjustedYear = m.year() + yearOffset;
    return formattedDate.replace(m.year().toString(), adjustedYear.toString());
  }

  return formattedDate;
}

function pad(n: number) {
  return `${Math.floor(Math.abs(n))}`.padStart(2, '0');
}

function getTimezoneOffset(date: Date) {
  const tzOffset = -date.getTimezoneOffset();
  const diff = tzOffset >= 0 ? '+' : '-';
  return diff + pad(tzOffset / 60) + ':' + pad(tzOffset % 60);
}

export function toISOStringWithTimezone(date: Date) {
  return (
    date.getFullYear() +
    '-' +
    pad(date.getMonth() + 1) +
    '-' +
    pad(date.getDate()) +
    'T' +
    pad(date.getHours()) +
    ':' +
    pad(date.getMinutes()) +
    ':' +
    pad(date.getSeconds()) +
    getTimezoneOffset(date)
  );
}

export function calculateAge(
  birthDate: Date | null | string,
  only?: 'year' | 'months' | 'days',
) {
  if (!birthDate) return null;

  const birthDateTimeStamp = new Date(birthDate).getTime();
  const now = new Date();
  const diff = now.getTime() - birthDateTimeStamp;

  const ageDate = new Date(diff);
  const years = ageDate.getUTCFullYear() - 1970;
  const months = ageDate.getUTCMonth();
  const days = ageDate.getUTCDate() - 1;

  if (only) {
    return only === 'year' ? years : only === 'months' ? months : days;
  }

  const { locale } = useI18n();
  if (locale.value === 'tha') {
    return `${years} ปี ${months !== 0 ? months + ' เดือน' : ''}  ${days !== 0 ? days + ' วัน' : ''} `;
  } else {
    return `${years} years ${months !== 0 ? months + ' months' : ''}  ${days !== 0 ? days + ' days' : ''} `;
  }
}

export function disabledAfterToday(date: Date) {
  const today = new Date();
  today.setHours(0, 0, 0, 0);
  return date > today;
}

export function parseAndFormatDate(
  value: string | number | undefined,
  locale: string,
) {
  if (!value) return;

  if (value && value.toString().length === 10) {
    const [date, month, year] = value.toString().split('/');
    if (locale === 'tha') {
      const adjustedYear = Number(year) - 543;
      return new Date(`${adjustedYear}-${month}-${date}T00:00:00.000Z`);
    } else {
      return new Date(`${year}-${month}-${date}T00:00:00.000Z`);
    }
  }
  return null;
}
