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 {
  getCashReceiptPurposeTypeBtnGroup,
  getCashReceiptTransactionTypeBtnGroup,
  getCashReceiptProcessResultBtnGroup
} from '@/entities/transaction/model/contant';
import {
  CashReceiptFilterProps,
  CashReceiptPurposeType,
  CashReceiptTransactionType,
  CashReceiptProcessResult,
  CashReceiptSearchNumberType
} 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 CashReceiptFilter = ({
  filterOn,
  setFilterOn,
  mid,
  startDate,
  endDate,
  purposeType,
  transactionType,
  processResult,
  searchNumberType,
  setMid,
  setStartDate,
  setEndDate,
  setPurposeType,
  setTransactionType,
  setProcessResult,
  setSearchNumberType
}: CashReceiptFilterProps) => {
  const { t } = useTranslation();

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterStartDate, setFilterStartDate] = useState<string>(startDate);
  const [filterEndDate, setFilterEndDate] = useState<string>(endDate);
  const [filterPurposeType, setFilterPurposeType] = useState<CashReceiptPurposeType>(purposeType);
  const [filterTransactionType, setFilterTransactionType] = useState<CashReceiptTransactionType>(transactionType);
  const [filterProcessResult, setFilterProcessResult] = useState<CashReceiptProcessResult>(processResult);
  const [filterSearchNumberType, setFilterSearchNumberType] = useState<CashReceiptSearchNumberType>(searchNumberType);

  const [filterIssueType, setFilterIssueType] = useState<string>('');
  const [filterIssueNumber, setFilterIssueNumber] = useState<string>('');
  const issueTypeOptionsGroup = [
    {name: t('filter.searchOptions.approvalNumber'), value: 'issue'},
    {name: t('filter.searchOptions.issueNumber'), value: 'issue2'},
  ];

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

  const purposeTypeBtnGroup = getCashReceiptPurposeTypeBtnGroup(t);
  const transactionTypeBtnGroup = getCashReceiptTransactionTypeBtnGroup(t);
  const processResultBtnGroup = getCashReceiptProcessResultBtnGroup(t);

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

  const onClickToSetFilter = () => {
    setMid(filterMid);
    setStartDate(filterStartDate);
    setEndDate(filterEndDate);
    setPurposeType(filterPurposeType);
    setTransactionType(filterTransactionType);
    setProcessResult(filterProcessResult);
    onClickToClose();
  };
    
  useEffect(() => {
    setFilterTransactionType(transactionType);
  }, [transactionType]);

  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>
            <FilterCalendar
              title={t('filter.issuanceDate')}
              startDate={ filterStartDate }
              endDate={ filterEndDate }
              setStartDate={ setFilterStartDate }
              setEndDate={ setFilterEndDate }
            ></FilterCalendar>

            <FilterButtonGroups
              title={t('filter.purpose')}
              activeValue={ filterPurposeType }
              btnGroups={ purposeTypeBtnGroup }
              setter={ setFilterPurposeType }
            ></FilterButtonGroups>
            <FilterButtonGroups
              title={t('filter.transactionCategory')}
              activeValue={ filterTransactionType }
              btnGroups={ transactionTypeBtnGroup }
              setter={ setFilterTransactionType }
            ></FilterButtonGroups>
            <FilterButtonGroups
              title={t('filter.progressStatus')}
              activeValue={ filterProcessResult }
              btnGroups={ processResultBtnGroup }
              setter={ setFilterProcessResult }
            ></FilterButtonGroups>
            <FilterSelectInput
              title={t('filter.approvalIssuanceNumber')}
              selectValue={ filterSearchNumberType }
              selectSetter={ setFilterSearchNumberType }
              selectOptions={ issueTypeOptionsGroup }
              inputValue={ filterIssueNumber }
              inputSetter={ setFilterIssueNumber }
            ></FilterSelectInput>
          </div>
          <div className="apply-row">
            <button
              className="btn-50 btn-blue flex-1"
              onClick={ () => onClickToSetFilter() }
            >{t('filter.apply')}</button>
          </div>
        </div>
      </motion.div>
    </>
  );
};