import moment from 'moment';
import { useEffect } from 'react';
import { IMAGE_ROOT } from '@/shared/constants/common';
import { motion } from 'framer-motion';
import { ChangeEvent, useState } from 'react';
import {
	ProcessResult
} from "../../../model/types";
import { FilterSelect } from '@/shared/ui/filter/select';
import { FilterSelectInput } from '@/shared/ui/filter/select-input';
import { FilterDateOptions } from '@/entities/common/model/types';
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 { LinkPaymentHistoryFilterProps, LinkPaymentPaymentStatus, LinkPaymentSearchCl, LinkPaymentSendMethod, LinkPaymentSendStatus } from '@/entities/additional-service/model/link-pay/types';
import { FilterSelectMid } from '@/shared/ui/filter/select-mid';
import { useTranslation } from 'react-i18next';

export const LinkPaymentHistoryFilter = ({
	filterOn,
	setFilterOn,
	mid,
	searchCl,
	searchValue,
	fromDate,
	toDate,
	paymentStatus,
	sendStatus,
	sendMethod,
	setMid,
	setSearchType,
	setSearchKeyword,
	setStartDate,
	setEndDate,
	setPaymentStatus,
	setSendStatus,
	setSendMethod
}: LinkPaymentHistoryFilterProps) => {
	const { t } = useTranslation();

	const [filterMid, setFilterMid] = useState<string>(mid);
	const [filterSearchCl, setFilterSearchCl] = useState<LinkPaymentSearchCl>(searchCl);
	const [filterSearchValue, setFilterSearchValue] = useState<string>(searchValue);
	const [filterStartDate, setFilterStartDate] = useState<string>(fromDate);
	const [filterEndDate, setFilterEndDate] = useState<string>(toDate);
	const [filterTransactionStatus, setFilterTransactionStatus] = useState<LinkPaymentPaymentStatus>(paymentStatus)
	const [filterSendStatus, setFilterSendStatus] = useState<LinkPaymentSendStatus>(sendStatus);
	const [filterSendMethod, setFilterSendMethod] = useState<LinkPaymentSendMethod>(sendMethod);

	let searchTypeOption = [
		{ name: t('additionalService.linkPayment.phoneNumber'), value: LinkPaymentSearchCl.PHONE },
		{ name: t('additionalService.linkPayment.email'), value: LinkPaymentSearchCl.EMAIL },
	];

	let transactionStatusOption = [
		{ name: t('additionalService.linkPayment.all'), value: LinkPaymentPaymentStatus.ALL },
		{ name: t('additionalService.linkPayment.incompleteActive'), value: LinkPaymentPaymentStatus.ACTIVATE },
		{ name: t('additionalService.linkPayment.depositRequest'), value: LinkPaymentPaymentStatus.DEPOSIT_REQUEST },
		{ name: t('additionalService.linkPayment.paymentComplete'), value: LinkPaymentPaymentStatus.PAYMENT_COMPLETE },
		{ name: t('additionalService.linkPayment.paymentFailed'), value: LinkPaymentPaymentStatus.PAYMENT_FAIL },
		{ name: t('additionalService.linkPayment.paymentStopped'), value: LinkPaymentPaymentStatus.INACTIVE },
	];

	let processResultOption = [
		{ name: t('additionalService.linkPayment.all'), value: LinkPaymentSendStatus.ALL },
		{ name: t('additionalService.linkPayment.success'), value: LinkPaymentSendStatus.SUCCESS },
		{ name: t('additionalService.linkPayment.fail'), value: LinkPaymentSendStatus.FAIL },
	];

	let sendMethodOption = [
		{ name: t('additionalService.linkPayment.all'), value: LinkPaymentSendMethod.ALL },
		{ name: 'SMS', value: LinkPaymentSendMethod.SMS },
		{ name: t('additionalService.linkPayment.email'), value: LinkPaymentSendMethod.EMAIL },
		{ name: t('additionalService.linkPayment.kakao'), value: LinkPaymentSendMethod.KAKAO },
	];

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

	const onClickToSetFilter = () => {
		setMid(filterMid);
		setSearchType(filterSearchCl);
		setSearchKeyword(filterSearchValue);
		setStartDate(filterStartDate);
		setEndDate(filterEndDate);
		setPaymentStatus(filterTransactionStatus);
		setSendStatus(filterSendStatus);
		setSendMethod(filterSendMethod);
		onClickToClose();
	};

	useEffect(() => {
		setFilterTransactionStatus(paymentStatus);
	}, [paymentStatus]);
	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('common.filter')}</div>
						<div className="full-menu-actions">
							<button
								id="closeFullMenu"
								className="full-menu-close"
							>
								<img
									src={IMAGE_ROOT + '/ico_close.svg'}
									alt={t('common.close')}
									onClick={() => onClickToClose()}
								/>
							</button>
						</div>
					</div>

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

						<FilterSelectInput
							title={t('additionalService.linkPayment.phoneNumberEmail')}
							selectValue={filterSearchCl}
							selectSetter={setFilterSearchCl}
							selectOptions={searchTypeOption}
							inputValue={searchValue}
							inputSetter={setFilterSearchValue}
						></FilterSelectInput>
						<FilterCalendar
							startDate={filterStartDate}
							endDate={filterEndDate}
							setStartDate={setFilterStartDate}
							setEndDate={setFilterEndDate}
						></FilterCalendar>

						<FilterButtonGroups
							title={t('additionalService.linkPayment.transactionStatus')}
							activeValue={filterTransactionStatus}
							btnGroups={transactionStatusOption}
							setter={setFilterTransactionStatus}
						></FilterButtonGroups>

						<FilterButtonGroups
							title={t('additionalService.linkPayment.sendResult')}
							activeValue={filterSendStatus}
							btnGroups={processResultOption}
							setter={setFilterSendStatus}
						></FilterButtonGroups>

						<FilterButtonGroups
							title={t('additionalService.linkPayment.sendMethod')}
							activeValue={filterSendMethod}
							btnGroups={sendMethodOption}
							setter={setFilterSendMethod}
						></FilterButtonGroups>
					</div>
					<div className="apply-row">
						<button
							className="btn-50 btn-blue flex-1"
							onClick={() => onClickToSetFilter()}
						>{t('additionalService.linkPayment.apply')}</button>
					</div>
				</div>
			</motion.div>
		</>
	)
}