import { useEffect, useState } from 'react';
import { motion } from 'framer-motion';
import { useTranslation } from 'react-i18next';
import { IMAGE_ROOT } from '@/shared/constants/common';
import { useStore } from '@/shared/model/store';
import {
  SettlementPaymentMethod,
  SettlementPeriodType
} from '../../model/types';
import {
  FilterMotionDuration,
  FilterMotionStyle,
  FilterMotionVariants
} from '@/entities/common/model/constant';
import { FilterSelect } from '@/shared/ui/filter/select';
import { FilterCalendar } from '@/shared/ui/filter/calendar';
import { getSettlementPaymentMethodOptionsGroup, getSettlementPeriodTypeBtnGroup } from '../../model/constant';
import { FilterButtonGroups } from '@/shared/ui/filter/button-groups';
import { FilterSelectMid } from '@/shared/ui/filter/select-mid';

export interface ListFilterProps {
  filterOn: boolean;
  setFilterOn: (filterOn: boolean) => void;
  mid: string;
  periodType: SettlementPeriodType;
  startDate: string;
  endDate: string;
  paymentMethod: SettlementPaymentMethod;
  setMid: (mid: string) => void;
  setPeriodType: (periodType: SettlementPeriodType) => void;
  setStartDate: (startDate: string) => void;
  setEndDate: (endDate: string) => void;
  setPaymentMethod: (paymentMethod: SettlementPaymentMethod) => void;
};

export const ListFilter = ({
  filterOn,
  setFilterOn,
  mid,
  periodType,
  startDate,
  endDate,
  paymentMethod,
  setMid,
  setPeriodType,
  setStartDate,
  setEndDate,
  setPaymentMethod
}: ListFilterProps) => {
  const { t } = useTranslation();

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterPeriodType, setFilterPeriodType] = useState<SettlementPeriodType>(periodType);
  const [filterStartDate, setFilterStartDate] = useState<string>(startDate);
  const [filterEndDate, setFilterEndDate] = useState<string>(endDate);
  const [filterPaymentMethod, setFilterPaymentMethod] = useState<SettlementPaymentMethod>(paymentMethod);

  const midOptions = useStore.getState().UserStore.selectOptionsMids;
  
  const onClickToClose = () => {
    setFilterOn(false);
  };

  const onClickToSetFilter = () => {
    setMid(filterMid);
    setPeriodType(filterPeriodType);
    setStartDate(filterStartDate);
    setEndDate(filterEndDate);
    setPaymentMethod(filterPaymentMethod);
    onClickToClose();
  };

  useEffect(() => {
    setFilterPeriodType(periodType);
  }, [periodType]);

  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('common.close')}
                  onClick={ () => onClickToClose() }
                />
              </button>
            </div>
          </div>
          <div className="option-list pt-16 pb-86">
            <FilterSelectMid
              title={t('filter.merchant')}
              selectSetter={ setMid }
              showType={ 'GID' }
            ></FilterSelectMid>
            <FilterButtonGroups
              title={t('settlement.searchCriteria')}
              activeValue={ filterPeriodType }
              btnGroups={ getSettlementPeriodTypeBtnGroup(t) }
              setter={ setFilterPeriodType }
            ></FilterButtonGroups>
            <FilterCalendar
              title={t('settlement.searchPeriod')}
              startDate={ filterStartDate }
              endDate={ filterEndDate }
              setStartDate={ setFilterStartDate }
              setEndDate={ setFilterEndDate }
            ></FilterCalendar>
            <FilterSelect
              title={t('filter.paymentMethod')}
              selectValue={ filterPaymentMethod }
              selectSetter={ setFilterPaymentMethod }
              selectOptions={ getSettlementPaymentMethodOptionsGroup(t) }
            ></FilterSelect>
          </div>
          <div className="apply-row">
            <button
              className="btn-50 btn-blue flex-1"
							onClick={ () => onClickToSetFilter() }
            >{t('filter.apply')}</button>
          </div>
        </div>
      </motion.div>
    </>
  );
};