import { useEffect } from 'react';
import { useState } from 'react';
import { motion } from 'framer-motion';
import { useTranslation } from 'react-i18next';
import { IMAGE_ROOT } from '@/shared/constants/common';
import { FilterSelect } from '@/shared/ui/filter/select';
import { FilterSelectInput } from '@/shared/ui/filter/select-input';
import { FilterCalendar } from '@/shared/ui/filter/calendar';
import { FilterButtonGroups } from '@/shared/ui/filter/button-groups';
import { FilterRangeAmount } from '@/shared/ui/filter/range-amount';
import {
  getEscrowDeliveryStatusBtnGroup,
  getEscrowSettlementStatusBtnGroup,
  getEscrowSearchTypeOptionsGroup
} from '@/entities/transaction/model/contant';
import {
  EscrowFilterProps,
  EscrowSearchType,
  EscrowDeliveryStatus,
  EscrowSettlementStatus
} from '../../model/types';
import {
  FilterMotionDuration,
  FilterMotionStyle,
  FilterMotionVariants
} from '@/entities/common/model/constant';
import { useStore } from '@/shared/model/store';
import { FilterSelectMid } from '@/shared/ui/filter/select-mid';

export const EscrowFilter = ({
  filterOn,
  setFilterOn,
  mid,
  searchType,
  searchKeyword,
  startDate,
  endDate,
  deliveryStatus,
  settlementStatus,
  minAmount,
  maxAmount,
  setMid,
  setSearchType,
  setSearchKeyword,
  setStartDate,
  setEndDate,
  setDeliveryStatus,
  setSettlementStatus,
  setMinAmount,
  setMaxAmount
}: EscrowFilterProps) => {
  const { t } = useTranslation();

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterSearchType, setFilterSearchType] = useState<EscrowSearchType>(searchType);
  const [filterSearchKeyword, setFilterSearchKeyword] = useState<string>(searchKeyword);
  const [filterStartDate, setFilterStartDate] = useState<string>(startDate);
  const [filterEndDate, setFilterEndDate] = useState<string>(endDate);
  const [filterDeliveryStatus, setFilterDeliveryStatus] = useState<EscrowDeliveryStatus>(deliveryStatus);
  const [filterSettlementStatus, setFilterSettlementStatus] = useState<EscrowSettlementStatus>(settlementStatus);
  const [filterMinAmount, setFilterMinAmount] = useState<number | undefined>(minAmount);
  const [filterMaxAmount, setFilterMaxAmount] = useState<number | undefined>(maxAmount);

  const midOptions = useStore.getState().UserStore.selectOptionsMids;

  const searchTypeOptionsGroup = getEscrowSearchTypeOptionsGroup(t);
  const deliveryStatusBtnGroup = getEscrowDeliveryStatusBtnGroup(t);
  const settlementStatusBtnGroup = getEscrowSettlementStatusBtnGroup(t);

  const onClickToClose = () => {
    setFilterOn(false);
  };
  
  const setNewDate = (newDate: any) => {
    console.log(newDate)
  };

  const onClickToSetFilter = () => {
    setMid(filterMid);
    setSearchType(filterSearchType);
    setSearchKeyword(filterSearchKeyword);
    setStartDate(filterStartDate);
    setEndDate(filterEndDate);
    setDeliveryStatus(filterDeliveryStatus);
    setSettlementStatus(filterSettlementStatus);
    setMinAmount(filterMinAmount);
    setMaxAmount(filterMaxAmount);
    onClickToClose();
  };
  
  useEffect(() => {
    setFilterDeliveryStatus(deliveryStatus);
  }, [deliveryStatus]);

  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={ setMid }
              showType={ 'GID' }
            ></FilterSelectMid>
            <FilterSelectInput
              title={t('filter.orderNumberTid')}
              selectValue={ filterSearchType }
              selectSetter={ setSearchType }
              selectOptions={ searchTypeOptionsGroup }
              inputValue={ searchKeyword }
              inputSetter={ setSearchKeyword }
            ></FilterSelectInput>
            <FilterCalendar
              title={t('filter.period')}
              startDate={ filterStartDate }
              endDate={ filterEndDate }
              setStartDate={ setFilterStartDate }
              setEndDate={ setFilterEndDate }
            ></FilterCalendar>

            <FilterButtonGroups
              title={t('filter.deliveryStatus')}
              activeValue={ filterDeliveryStatus }
              btnGroups={ deliveryStatusBtnGroup }
              setter={ setFilterDeliveryStatus }
            ></FilterButtonGroups>
            <FilterButtonGroups
              title={t('filter.paymentMethod')}
              activeValue={ filterSettlementStatus }
              btnGroups={ settlementStatusBtnGroup }
              setter={ setFilterSettlementStatus }
            ></FilterButtonGroups>

            <FilterRangeAmount
              title={t('filter.transactionAmount')}
              minAmount={ filterMinAmount }
              maxAmount={ filterMaxAmount }
              setMinAmount={ setFilterMinAmount }
              setMaxAmount={ setFilterMaxAmount }
            ></FilterRangeAmount>

          </div>
          <div className="apply-row">
            <button
              className="btn-50 btn-blue flex-1"
              onClick={ () => onClickToSetFilter() }
            >{t('filter.apply')}</button>
          </div>
        </div>
      </motion.div>
    </>
  );
};