import { useEffect, useState } from 'react';
import { FundAccountReceiveAccountNameNo, FundAccountSearchCl, FundAccountStatus } 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 { 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 FundAccountTransactionFilterProps {
  filterOn: boolean;
  setFilterOn: (filterOn: boolean) => void;
  mid: string;
  searchCl: FundAccountSearchCl;
  searchValue: string;
  fromDate: string;
  toDate: string;
  bankCode: string;
  status: FundAccountStatus;
  setMid: (mid: string) => void;
  setSearchCl: (searchCl: FundAccountSearchCl) => void;
  setSearchValue: (searchValue: string) => void;
  setFromDate: (fromDate: string) => void;
  setToDate: (toDate: string) => void;
  setBankCode: (bankCode: string) => void;
  setStatus: (status: FundAccountStatus) => void;
};
export const FundAccountTransactionFilter = ({
  filterOn,
  setFilterOn,
  mid,
  searchCl,
  searchValue,
  fromDate,
  toDate,
  bankCode,
  status,
  setMid,
  setSearchCl,
  setSearchValue,
  setFromDate,
  setToDate,
  setBankCode,
  setStatus
}: FundAccountTransactionFilterProps) => {
  const { t } = useTranslation();

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterSearchCl, setFilterSearchCl] = useState<FundAccountSearchCl>(searchCl);
  const [filterSearchValue, setFilterSearchValue] = useState<string>(searchValue);
  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 [filterStatus, setFilterStatus] = useState<FundAccountStatus>(status);
  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);
    }

    setFromDate(filterFromDate);
    setToDate(filterToDate);
    setBankCode(filterBankCode);
    setStatus(filterStatus);
    onClickToClose();
  };

  useEffect(() => {
    setFilterStatus(status);
  }, [status]);

  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>
            <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={filterStatus}
              btnGroups={getFundAccountStatusBtnGroup(t)}
              setter={setFilterStatus}
            ></FilterButtonGroups>
          </div>
          <div className="apply-row">
            <button
              className="btn-50 btn-blue flex-1"
              onClick={() => onClickToSetFilter()}
            >{t('filter.apply')}</button>
          </div>
        </div>
      </motion.div>
    </>
  );
};