import moment from 'moment';
import { IMAGE_ROOT } from '@/shared/constants/common';
import { motion } from 'framer-motion';
import { useEffect, useState } from 'react';
import { FilterSelect } from '@/shared/ui/filter/select';
import { FilterCalendar } from '@/shared/ui/filter/calendar';
import { FilterButtonGroups } from '@/shared/ui/filter/button-groups';
import { getAuthStatusBtnGroup } from '@/entities/additional-service/model/account-holder-auth/constant';
import { FilterMotionDuration, FilterMotionStyle, FilterMotionVariants } from '@/entities/common/model/constant';
import { useStore } from '@/shared/model/store';
import { AccountHolderAuthFilterProps, AccountHolderAuthStatus } from '@/entities/additional-service/model/account-holder-auth/types';
import { FilterSelectMid } from '@/shared/ui/filter/select-mid';
import { useTranslation } from 'react-i18next';

export const AccountHolderAuthFilter = ({
	filterOn,
	setFilterOn,
	mid,
	startDate,
	endDate,
	authStatus,
	setMid,
	setStartDate,
	setEndDate,
	setAuthStatus
}: AccountHolderAuthFilterProps) => {
	const { t } = useTranslation();

	const [filterMid, setFilterMid] = useState<string>(mid);
	const [filterStartDate, setFilterStartDate] = useState<string>(moment(startDate).format('YYYYMMDD'));
	const [filterEndDate, setFilterEndDate] = useState<string>(moment(endDate).format('YYYYMMDD'));
	const [filterAuthStatus, setFilterAuthStatus] = useState<AccountHolderAuthStatus>(authStatus);

	const onClickToClose = () => {
		setFilterOn(false);
	};

	const onClickToSetFilter = () => {
		setMid(filterMid);
		setStartDate(filterStartDate);
		setEndDate(filterEndDate);
		setAuthStatus(filterAuthStatus);
		onClickToClose();
	};

	useEffect(() => {
		setFilterAuthStatus(authStatus);
	}, [authStatus]);
	return (
		<>
			<motion.div
				className="full-menu-modal"
				initial="hidden"
				animate={(filterOn) ? 'visible' : 'hidden'}
				variants={FilterMotionVariants}
				transition={FilterMotionDuration}
				style={FilterMotionStyle}
			>
				<div className="full-menu-container">
					<div className="full-menu-header">
						<div className="full-menu-title center">{t('filter.filter')}</div>
						<div className="full-menu-actions">
							<button
								id="closeFullMenu"
								className="full-menu-close"
							>
								<img
									src={IMAGE_ROOT + '/ico_close.svg'}
									alt={t('filter.close')}
									onClick={() => onClickToClose()}
								/>
							</button>
						</div>
					</div>

					<div className="option-list pt-16 pb-86">
						<FilterSelectMid
							title={t('filter.merchant')}
							selectSetter={setFilterMid}
							showType={'GID'}
						></FilterSelectMid>

						<FilterCalendar
							startDate={filterStartDate}
							endDate={filterEndDate}
							setStartDate={setFilterStartDate}
							setEndDate={setFilterEndDate}
						></FilterCalendar>

						<FilterButtonGroups
							title={t('additionalService.payout.paymentStatus')}
							activeValue={filterAuthStatus}
							btnGroups={getAuthStatusBtnGroup(t)}
							setter={setFilterAuthStatus}
						></FilterButtonGroups>
					</div>
					<div className="apply-row">
						<button
							className="btn-50 btn-blue flex-1"
							onClick={() => onClickToSetFilter()}
						>{t('filter.apply')}</button>
					</div>
				</div>
			</motion.div>
		</>
	);
}
