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 {
  getBillingSearchTypeOptionsGroup,
  getBillingRequestStatusBtnGroup,
  getBillingProcessResultBtnGroup,
  getBillingPaymentMethodBtnGroup
} from '@/entities/transaction/model/contant';
import {
  BillingFilterProps,
  BillingPaymentMethod,
  BillingProcessResult,
  BillingRequestStatus,
  BillingSearchType
} 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 BillingFilter = ({
  filterOn,
  setFilterOn,
  mid,
  searchType,
  searchKeyword,
  startDate,
  endDate,
  requestStatus,
  processResult,
  paymentMethod,
  minAmount,
  maxAmount,
  setMid,
  setSearchType,
  setSearchKeyword,
  setStartDate,
  setEndDate,
  setRequestStatus,
  setProcessResult,
  setPaymentMethod,
  setMinAmount,
  setMaxAmount
}: BillingFilterProps) => {
  const { t } = useTranslation();

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterSearchType, setFilterSearchType] = useState<BillingSearchType>(searchType);
  const [filterSearchKeyword, setFilterSearchKeyword] = useState<string>(searchKeyword);
  const [filterStartDate, setFilterStartDate] = useState<string>(startDate);
  const [filterEndDate, setFilterEndDate] = useState<string>(endDate);
  const [filterRequestStatus, setFilterRequestStatus] = useState<BillingRequestStatus>(requestStatus);
  const [filterProcessResult, setFilterProcessResult] = useState<BillingProcessResult>(processResult);
  const [filterPaymentMethod, setFilterPaymentMethod] = useState<BillingPaymentMethod>(paymentMethod);
  const [filterMinAmount, setFilterMinAmount] = useState<number | undefined>(minAmount);
  const [filterMaxAmount, setFilterMaxAmount] = useState<number | undefined>(maxAmount);

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

  const searchTypeOptionsGroup = getBillingSearchTypeOptionsGroup(t);
  const requestStatusBtnGroup = getBillingRequestStatusBtnGroup(t);
  const processResultBtnGroup = getBillingProcessResultBtnGroup(t);
  const paymentMethodBtnGroup = getBillingPaymentMethodBtnGroup(t);

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

	const onClickToSetFilter = () => {
    setMid(filterMid);
    setSearchType(filterSearchType);
    setSearchKeyword(filterSearchKeyword);
    setStartDate(filterStartDate);
    setEndDate(filterEndDate);
    setRequestStatus(filterRequestStatus);
    setProcessResult(filterProcessResult);
    setPaymentMethod(filterPaymentMethod);
    setMinAmount(filterMinAmount);
    setMaxAmount(filterMaxAmount);
    onClickToClose();
	};
  
  useEffect(() => {
    setFilterRequestStatus(requestStatus);
  }, [requestStatus]);

  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={ setFilterSearchType }
              selectOptions={ searchTypeOptionsGroup }
              inputValue={ searchKeyword }
              inputSetter={ setFilterSearchKeyword }
            ></FilterSelectInput>
            <FilterCalendar
              title={t('filter.period')}
              startDate={ filterStartDate }
              endDate={ filterEndDate }
              setStartDate={ setFilterStartDate }
              setEndDate={ setFilterEndDate }
            ></FilterCalendar>

            <FilterButtonGroups
              title={t('filter.requestStatus')}
              activeValue={ filterRequestStatus }
              btnGroups={ requestStatusBtnGroup }
              setter={ setFilterRequestStatus }
            ></FilterButtonGroups>
            <FilterButtonGroups
              title={t('filter.processingResult')}
              activeValue={ filterProcessResult }
              btnGroups={ processResultBtnGroup }
              setter={ setFilterProcessResult }
            ></FilterButtonGroups>
            <FilterButtonGroups
              title={t('filter.paymentMethod')}
              activeValue={ filterPaymentMethod }
              btnGroups={ paymentMethodBtnGroup }
              setter={ setFilterPaymentMethod }
            ></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>
    </>
  );
};