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 { FilterSelectInput } from '@/shared/ui/filter/select-input';
import { FilterCalendar } from '@/shared/ui/filter/calendar';
import { FilterButtonGroups } from '@/shared/ui/filter/button-groups';
import { FilterMotionDuration, FilterMotionStyle, FilterMotionVariants } from '@/entities/common/model/constant';
import { useStore } from '@/shared/model/store';
import { AccountHolderSearchFilterProps, AccountHolderSearchCl, AccountHolderResultStatus } from '@/entities/additional-service/model/account-holder-search/types';
import { getResultStatusBtnGroup, getSearchTypeOption } from '@/entities/additional-service/model/account-holder-search/constant';
import { FilterSelectMid } from '@/shared/ui/filter/select-mid';
import { useTranslation } from 'react-i18next';

export const AccountHolderSearchFilter = ({
	filterOn,
	setFilterOn,
	mid,
	searchType,
	searchKeyword,
	startDate,
	endDate,
	bank,
	resultStatus,
	setMid,
	setSearchType,
	setSearchKeyword,
	setStartDate,
	setEndDate,
	setBank,
	setResultStatus
}: AccountHolderSearchFilterProps) => {
	const { t } = useTranslation();

	const [filterMid, setFilterMid] = useState<string>(mid);
	const [filterSearchType, setFilterSearchType] = useState<AccountHolderSearchCl>(searchType);
	const [filterSearchKeyword, setFilterSearchKeyword] = useState<string>(searchKeyword);
	const [filterStartDate, setFilterStartDate] = useState<string>(moment(startDate).format('YYYYMMDD'));
	const [filterEndDate, setFilterEndDate] = useState<string>(moment(endDate).format('YYYYMMDD'));
	const [filterBank, setFilterBank] = useState<string>(bank)
	const [filterResultStatus, setFilterResultStatus] = useState<AccountHolderResultStatus>(resultStatus);

	const bankList = useStore.getState().CommonStore.bankList;
	const bankOptions = [
		{ name: t('additionalService.accountHolderSearch.select'), value: '' },
		...bankList
			.filter((bank) => bank.code1 !== '****')
			.map((bank) => ({ name: bank.desc1, value: bank.code1 }))
	];

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

	const onClickToSetFilter = () => {
		setMid(filterMid);
		setSearchType(filterSearchType);
		setSearchKeyword(filterSearchKeyword);
		setStartDate(filterStartDate);
		setEndDate(filterEndDate);
		setBank(filterBank);
		setResultStatus(filterResultStatus);
		onClickToClose();
	};

	useEffect(() => {
		setFilterResultStatus(resultStatus);
	}, [resultStatus]);

	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>

						<FilterSelectInput
							title={t('additionalService.accountHolderSearch.accountHolderOrAccountNumber')}
							selectValue={filterSearchType}
							selectSetter={setFilterSearchType}
							selectOptions={getSearchTypeOption(t)}
							inputValue={filterSearchKeyword}
							inputSetter={setFilterSearchKeyword}
						></FilterSelectInput>
						<FilterCalendar
							startDate={filterStartDate}
							endDate={filterEndDate}
							setStartDate={setFilterStartDate}
							setEndDate={setFilterEndDate}
						></FilterCalendar>

						<FilterSelect
							title={t('additionalService.accountHolderSearch.bank')}
							selectValue={filterBank}
							selectSetter={setFilterBank}
							selectOptions={bankOptions}
						></FilterSelect>

						<FilterButtonGroups
							title={t('additionalService.accountHolderSearch.searchResult')}
							activeValue={filterResultStatus}
							btnGroups={getResultStatusBtnGroup(t)}
							setter={setFilterResultStatus}
						></FilterButtonGroups>
					</div>
					<div className="apply-row">
						<button
							className="btn-50 btn-blue flex-1"
							onClick={() => onClickToSetFilter()}
						>{t('filter.apply')}</button>
					</div>
				</div>
			</motion.div>
		</>
	);
}