import { useEffect, useState } from 'react';
import { FundAccountReceiveAccountNameNo, FundAccountResultStatus, FundAccountSearchCl, FundAccountSearchDateType } from '../../model/fund-account/types';
import moment from 'moment';
import { motion } from 'framer-motion';
import { FilterMotionDuration, FilterMotionStyle, FilterMotionVariants } from '@/entities/common/model/constant';
import { IMAGE_ROOT } from '@/shared/constants/common';
import { FilterSelect } from '@/shared/ui/filter/select';
import { FilterCalendar } from '@/shared/ui/filter/calendar';
import { getFundAccountResultSearchDateTypeBtnGroup, getFundAccountResultStatusBtnGroup, getFundAccountSearchClOptionsGroup, getFundAccountStatusBtnGroup } from '../../model/fund-account/constant';
import { FilterButtonGroups } from '@/shared/ui/filter/button-groups';
import { FilterSelectInput } from '@/shared/ui/filter/select-input';
import { useStore } from '@/shared/model/store';
import { FilterSelectMid } from '@/shared/ui/filter/select-mid';
import { useTranslation } from 'react-i18next';

export interface FundAccountResultFilterProps {
  filterOn: boolean;
  setFilterOn: (filterOn: boolean) => void;
  mid: string;
  searchCl: FundAccountSearchCl;
  searchValue: string;
  searchDateType: FundAccountSearchDateType;
  fromDate: string;
  toDate: string;
  bankCode: string;
  resultStatus: FundAccountResultStatus;
  setMid: (mid: string) => void;
  setSearchCl: (searchCl: FundAccountSearchCl) => void;
  setSearchValue: (searchValue: string) => void;
  setSearchDateType: (searchDateType: FundAccountSearchDateType) => void;
  setFromDate: (fromDate: string) => void;
  setToDate: (toDate: string) => void;
  setBankCode: (bankCode: string) => void;
  setResultStatus: (resultStatus: FundAccountResultStatus) => void;
};
export const FundAccountResultFilter = ({
  filterOn,
  setFilterOn,
  mid,
  searchCl,
  searchValue,
  searchDateType,
  fromDate,
  toDate,
  bankCode,
  resultStatus,
  setMid,
  setSearchCl,
  setSearchValue,
  setSearchDateType,
  setFromDate,
  setToDate,
  setBankCode,
  setResultStatus
}: FundAccountResultFilterProps) => {
  const { t } = useTranslation();

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterSearchCl, setFilterSearchCl] = useState<FundAccountSearchCl>(searchCl);
  const [filterSearchValue, setFilterSearchValue] = useState<string>(searchValue);
  const [filterSearchDateType, setFilterSearchDateType] = useState<FundAccountSearchDateType>(searchDateType);
  const [filterFromDate, setFilterFromDate] = useState<string>(moment(fromDate).format('YYYYMMDD'));
  const [filterToDate, setFilterToDate] = useState<string>(moment(toDate).format('YYYYMMDD'));
  const [filterBankCode, setFilterBankCode] = useState<string>(bankCode);
  const [filterResultStatus, setFilterResultStatus] = useState<FundAccountResultStatus>(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);
    if (filterSearchCl === FundAccountSearchCl.ACCOUNT_NAME) {
      setSearchCl(filterSearchCl);
      setSearchValue(filterSearchValue);
    }
    else if (filterSearchCl === FundAccountSearchCl.ACCOUNT_NO) {
      setSearchCl(filterSearchCl);
      setSearchValue(filterSearchValue);
    }
    setSearchDateType(filterSearchDateType);
    setFromDate(filterFromDate);
    setToDate(filterToDate);
    setBankCode(filterBankCode);
    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.fundAccount.recipientOrAccountNumber')}
              selectValue={filterSearchCl}
              selectSetter={setFilterSearchCl}
              selectOptions={getFundAccountSearchClOptionsGroup(t)}
              inputValue={filterSearchValue}
              inputSetter={setFilterSearchValue}
            ></FilterSelectInput>
            <FilterButtonGroups
              title={t('additionalService.fundAccount.searchCriteria')}
              activeValue={filterSearchDateType}
              btnGroups={getFundAccountResultSearchDateTypeBtnGroup(t)}
              setter={setFilterSearchDateType}
            ></FilterButtonGroups>
            <FilterCalendar
              title={t('filter.period')}
              startDate={filterFromDate}
              endDate={filterToDate}
              setStartDate={setFilterFromDate}
              setEndDate={setFilterToDate}
            ></FilterCalendar>
            <FilterSelect
              title={t('additionalService.accountHolderSearch.bank')}
              selectValue={filterBankCode}
              selectSetter={setFilterBankCode}
              selectOptions={bankOptions}
            ></FilterSelect>
            <FilterButtonGroups
              title={t('additionalService.fundAccount.processingResult')}
              activeValue={filterResultStatus}
              btnGroups={getFundAccountResultStatusBtnGroup(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>
    </>
  );
};