import React, { ReactNode } from 'react';
import { LocalizationProvider as MuiLocalizationProvider, MuiPickersAdapter } from '@mui/x-date-pickers';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalali';
import { enUS, faIR } from 'date-fns/locale';
import { isNil, isString } from 'lodash';

import { useLocalization } from '../../../localization';

import { jalaliFaIR } from './JalaliFaIr';

export interface PickersLocalizationProviderProps<TLocale = any> {
	dateAdapter?: 'jalali' | 'gregorian' | MuiPickersAdapter<TLocale>;
	adapterLocale?: 'fa' | 'en' | TLocale;
	children?: ReactNode;
}

const PickersLocalizationProvider = (props: PickersLocalizationProviderProps) => {
	const { children, adapterLocale, dateAdapter } = props;

	const { translate, lang } = useLocalization();

	const language = isString(adapterLocale) ? adapterLocale : lang;

	const _dateAdapter = React.useMemo(() => {
		if (isNil(dateAdapter)) {
			if (language === 'fa') {
				return AdapterDateFnsJalali;
			}

			return AdapterDateFns;
		}

		if (isString(dateAdapter)) {
			if (dateAdapter === 'jalali') {
				return AdapterDateFnsJalali;
			}

			return AdapterDateFns;
		}

		return dateAdapter;
	}, [dateAdapter, language]);

	const _adapterLocale = React.useMemo(() => {
		if (isNil(adapterLocale)) {
			if (language === 'fa') {
				if (dateAdapter === 'gregorian') {
					return faIR;
				}

				return jalaliFaIR;
			}

			return enUS;
		}

		if (adapterLocale === 'fa') {
			if (dateAdapter === 'gregorian') {
				return faIR;
			}

			return jalaliFaIR;
		}

		if (adapterLocale === 'en') {
			return enUS;
		}

		return adapterLocale;
	}, [adapterLocale, dateAdapter, language]);

	return (
		<MuiLocalizationProvider
			dateAdapter={_dateAdapter as any}
			adapterLocale={_adapterLocale}
			localeText={{
				timePickerToolbarTitle: translate('datePicker.timePickerToolbarTitle'),
				dateTimePickerToolbarTitle: translate('datePicker.dateTimePickerToolbarTitle'),
				dateRangePickerToolbarTitle: translate('datePicker.dateRangePickerToolbarTitle'),
				datePickerToolbarTitle: translate('datePicker.datePickerToolbarTitle'),
				cancelButtonLabel: translate('datePicker.cancelButtonLabel'),
				clearButtonLabel: translate('datePicker.clearButtonLabel'),
				okButtonLabel: translate('datePicker.okButtonLabel'),
				todayButtonLabel: translate('datePicker.todayButtonLabel'),
				dateTableLabel: translate('datePicker.dateTableLabel'),
				previousMonth: translate('datePicker.previousMonth'),
				nextMonth: translate('datePicker.nextMonth'),
				startDate: translate('datePicker.startDate'),
				endDate: translate('datePicker.endDate'),
				start: translate('datePicker.start'),
				fieldClearLabel: translate('datePicker.fieldClearLabel'),
				year: translate('datePicker.year'),
				empty: translate('datePicker.empty'),
				end: translate('datePicker.end'),
				day: translate('datePicker.day'),
				endTime: translate('datePicker.endTime'),
				hours: translate('datePicker.hours'),
				minutes: translate('datePicker.minutes'),
				month: translate('datePicker.month'),
				seconds: translate('datePicker.seconds'),
				startTime: translate('datePicker.startTime'),
				weekDay: translate('datePicker.weekDay'),
				timeTableLabel: translate('datePicker.timeTableLabel'),
				openNextView: translate('datePicker.openNextView'),
				meridiem: translate('datePicker.meridiem'),
				openPreviousView: translate('datePicker.openPreviousView'),
				calendarWeekNumberHeaderLabel: translate('datePicker.calendarWeekNumberHeaderLabel'),
				calendarWeekNumberHeaderText: translate('datePicker.calendarWeekNumberHeaderText'),

				fieldYearPlaceholder: (params) => translate('datePicker.fieldYearPlaceholder', params),
				fieldMonthPlaceholder: (params) => translate('datePicker.fieldMonthPlaceholder', params),
				fieldDayPlaceholder: (params) => translate('datePicker.fieldDayPlaceholder', params),
				fieldHoursPlaceholder: (params) => translate('datePicker.fieldHoursPlaceholder', params),
				fieldMinutesPlaceholder: (params) => translate('datePicker.fieldMinutesPlaceholder', params),
				fieldSecondsPlaceholder: (params) => translate('datePicker.fieldSecondsPlaceholder', params)
			}}
		>
			{children}
		</MuiLocalizationProvider>
	);
};

export default PickersLocalizationProvider;
