import { motion } from 'framer-motion';
import { useTranslation } from 'react-i18next';
import { IMAGE_ROOT } from '@/shared/constants/common';
import { VatReturnReceiptType, VatReturnTargetType } from '../../model/types';
import { FilterMotionDuration, FilterMotionStyle, FilterMotionVariants } from '@/entities/common/model/constant';
import { FilterSelect } from '@/shared/ui/filter/select';
import { useState } from 'react';
import { FilterButtonGroups } from '@/shared/ui/filter/button-groups';
import { getVatReturnReceiptTypeBtnGroup, getVatReturnTargetTypeBtnGroup } from '../../model/contant';
import { FilterCalendarMonth } from '@/shared/ui/filter/calendar-month';
import { useStore } from '@/shared/model/store';
import { FilterSelectMid } from '@/shared/ui/filter/select-mid';

export interface ListFilterProps {
  filterOn: boolean;
  setFilterOn: (filterOn: boolean) => void;
  mid: string;
  startMonth: string;
  endMonth: string;
  receiptType: VatReturnReceiptType;
  targetType: VatReturnTargetType;
  setMid: (mid: string) => void;
  setStartMonth: (startMonth: string) => void;
  setEndMonth: (endMonth: string) => void;
  setReceiptType: (receiptType: VatReturnReceiptType) => void;
  setTargetType: (targetType: VatReturnTargetType) => void;
};

export const ListFilter = ({
  filterOn,
  setFilterOn,
  mid,
  startMonth,
  endMonth,
  receiptType,
  targetType,
  setMid,
  setStartMonth,
  setEndMonth,
  setReceiptType,
  setTargetType
}: ListFilterProps) => {
  const { t } = useTranslation();

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterStartMonth, setFilterStartMonth] = useState<string>(startMonth);
  const [filterEndMonth, setFilterEndMonth] = useState<string>(endMonth);
  const [filterReceiptType, setFIlterReceiptType] = useState<VatReturnReceiptType>(receiptType);
  const [filterTargetType, setFilterTargetType] = useState<VatReturnTargetType>(targetType);

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

   const onClickToSetFilter = () => {
    setMid(filterMid);
    setStartMonth(filterStartMonth);
    setEndMonth(filterEndMonth);
    setReceiptType(filterReceiptType);
    setTargetType(filterTargetType);
    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
                className="full-menu-close"
                onClick={ () => onClickToClose() }
              >
                <img
                  src={ IMAGE_ROOT + '/ico_close.svg' }
                  alt={t('common.close')}
                />
              </button>
            </div>
          </div>

          <div className="option-list pt-16 pb-86">
            <FilterSelectMid
              title={t('merchant.title')}
              selectSetter={ setMid }
              showType={ 'GID' }
            ></FilterSelectMid>
            <FilterCalendarMonth
              title={t('vatReturn.issuanceMonth')}
              startMonth={ filterStartMonth }
              endMonth={ filterEndMonth }
              setStartMonth={ setFilterStartMonth }
              setEndMonth={ setFilterEndMonth }
            ></FilterCalendarMonth>
            <FilterButtonGroups
              title={t('vatReturn.receiptType')}
              activeValue={ filterReceiptType }
              btnGroups={ getVatReturnReceiptTypeBtnGroup(t) }
              setter={ setFIlterReceiptType }
            ></FilterButtonGroups>
            <FilterButtonGroups
              title={t('vatReturn.issueTarget')}
              activeValue={ filterTargetType }
              btnGroups={ getVatReturnTargetTypeBtnGroup(t) }
              setter={ setFilterTargetType }
            ></FilterButtonGroups>
          </div>
          <div className="apply-row">
            <button
              className="btn-50 btn-blue flex-1"
              onClick={ onClickToSetFilter }
            >{t('filter.apply')}</button>
          </div>
        </div>
      </motion.div>
    </>
  );
};