import moment from 'moment';
import { useState } from 'react';
import { motion } from 'framer-motion';
import { IMAGE_ROOT } from '@/shared/constants/common';
import { FilterSelect } from '@/shared/ui/filter/select';
import { FilterCalendar } from '@/shared/ui/filter/calendar';
import { FilterButtonGroups } from '@/shared/ui/filter/button-groups';
import { FilterRangeAmount } from '@/shared/ui/filter/range-amount';
import { FilterMotionDuration, FilterMotionStyle, FilterMotionVariants } from '@/entities/common/model/constant';
import { useStore } from '@/shared/model/store';
import { KeyInPaymentFilterProps, KeyInPaymentTansactionType } from '@/entities/additional-service/model/key-in/types';
import { getKeyInPaymentPaymentStatusBtnGroup } from '@/entities/additional-service/model/key-in/constant';
import { FilterSelectMid } from '@/shared/ui/filter/select-mid';
import { useTranslation } from 'react-i18next';

export const KeyInPaymentFilter = ({
	filterOn,
	setFilterOn,
	mid,
	startDate,
	endDate,
	transactionStatus,
	minAmount,
	maxAmount,
	setMid,
	setStartDate,
	setEndDate,
	setTransactionStatus,
	setMinAmount,
	setMaxAmount
}: KeyInPaymentFilterProps) => {
	const { t } = useTranslation();

	const [filterMid, setFilterMid] = useState<string>(mid);
	const [filterStartDate, setFilterStartDate] = useState<string>(startDate);
	const [filterEndDate, setFilterEndDate] = useState<string>(endDate);
	const [filterTransactionStatus, setFilterTransactionStatus] = useState<KeyInPaymentTansactionType>(transactionStatus);
	const [filterMinAmount, setFilterMinAmount] = useState<number | undefined>(minAmount);
	const [filterMaxAmount, setFilterMaxAmount] = useState<number | undefined>(maxAmount);

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

	const onClickToSetFilter = () => {
		setMid(filterMid);
		setStartDate(filterStartDate);
		setEndDate(filterEndDate);
		setTransactionStatus(filterTransactionStatus);
		setMinAmount(filterMinAmount);
		setMaxAmount(filterMaxAmount);
		onClickToClose();
	};

	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
							title={t('filter.period')}
							startDate={filterStartDate}
							endDate={filterEndDate}
							setStartDate={setFilterStartDate}
							setEndDate={setFilterEndDate}
						></FilterCalendar>

						<FilterButtonGroups
							title={t('filter.transactionStatus')}
							activeValue={filterTransactionStatus}
							btnGroups={getKeyInPaymentPaymentStatusBtnGroup(t)}
							setter={setFilterTransactionStatus}
						></FilterButtonGroups>

						<FilterRangeAmount
							title={t('additionalService.keyIn.productPrice')}
							minAmount={filterMinAmount}
							maxAmount={filterMaxAmount}
							setMinAmount={setFilterMinAmount}
							setMaxAmount={setFilterMaxAmount}
						></FilterRangeAmount>
					</div>
					<div className="apply-row">
						<button
							className="btn-50 btn-blue flex-1"
							onClick={() => onClickToSetFilter()}
						>{t('filter.apply')}</button>
					</div>
				</div>
			</motion.div>
		</>
	);
};