import { useEffect, useState } from 'react';
import { motion } from 'framer-motion';
import { IMAGE_ROOT } from '@/shared/constants/common';
import { FilterSelect } from '@/shared/ui/filter/select';
import { FilterCalendar } from '@/shared/ui/filter/calendar';
import { FilterButtonGroups } from '@/shared/ui/filter/button-groups';
import { FilterRangeAmount } from '@/shared/ui/filter/range-amount';
import {
  PayoutDisbursementStatus,
  PayoutSearchDateType
} from '../../model/payout/types';
import {
  getPayoutSearchClBtnGroup,
  getPayoutDisbursementStatusBtnGroup
} from '../../model/payout/constant';
import {
  FilterMotionDuration,
  FilterMotionStyle,
  FilterMotionVariants
} from '@/entities/common/model/constant';
import moment from 'moment';
import { useStore } from '@/shared/model/store';
import { FilterSelectMid } from '@/shared/ui/filter/select-mid';
import { useTranslation } from 'react-i18next';

export interface PayoutFilterProps {
  filterOn: boolean;
  setFilterOn: (filterOn: boolean) => void;
  mid: string;
  searchDateType: PayoutSearchDateType;
  fromDate: string;
  toDate: string;
  status: PayoutDisbursementStatus;
  minAmount?: number;
  maxAmount?: number;
  setMid: (mid: string) => void;
  setSearchDateType: (searchDateType: PayoutSearchDateType) => void;
  setFromDate: (fromDate: string) => void;
  setToDate: (toDate: string) => void;
  setStatus: (status: PayoutDisbursementStatus) => void;
  setMinAmount: (minAmount?: number) => void;
  setMaxAmount: (maxAmount?: number) => void;
};

export const PayoutFilter = ({
  filterOn,
  setFilterOn,
  mid,
  searchDateType,
  fromDate,
  toDate,
  status,
  minAmount,
  maxAmount,
  setMid,
  setSearchDateType,
  setFromDate,
  setToDate,
  setStatus,
  setMinAmount,
  setMaxAmount
}: PayoutFilterProps) => {
  const { t } = useTranslation();

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterSearchDateType, setFilterSearchDateType] = useState<PayoutSearchDateType>(searchDateType);
  const [filterFromDate, setFilterFromDate] = useState<string>(moment(fromDate).format('YYYY.MM.DD'));
  const [filterToDate, setFilterToDate] = useState<string>(moment(toDate).format('YYYY.MM.DD'));
  const [filterStatus, setFilterStatus] = useState<PayoutDisbursementStatus>(status);
  const [filterMinAmount, setFilterMinAmount] = useState<number | undefined>(minAmount);
  const [filterMaxAmount, setFilterMaxAmount] = useState<number | undefined>(maxAmount);

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

  const onClickToSetFilter = () => {
    setMid(filterMid);
    setSearchDateType(filterSearchDateType);
    setFromDate(filterFromDate);
    setToDate(filterToDate);
    setStatus(filterStatus);
    setMinAmount(filterMinAmount);
    setMaxAmount(filterMaxAmount);
    onClickToClose();
  };

  useEffect(() => {
    setFilterStatus(status);
  }, [status]);

  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={setFilterMid}
              showType={'GID'}
            ></FilterSelectMid>
            <FilterButtonGroups
              title={t('additionalService.fundAccount.searchCriteria')}
              activeValue={filterSearchDateType}
              btnGroups={getPayoutSearchClBtnGroup(t)}
              setter={setFilterSearchDateType}
            ></FilterButtonGroups>
            <FilterCalendar
              title={t('filter.period')}
              startDate={filterFromDate}
              endDate={filterToDate}
              setStartDate={setFilterFromDate}
              setEndDate={setFilterToDate}
            ></FilterCalendar>
            <FilterButtonGroups
              title={t('additionalService.payout.paymentStatus')}
              activeValue={filterStatus}
              btnGroups={getPayoutDisbursementStatusBtnGroup(t)}
              setter={setFilterStatus}
            ></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>
    </>
  );
};