import { useEffect, 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 { FilterCalendar } from '@/shared/ui/filter/calendar';
import { FilterButtonGroups } from '@/shared/ui/filter/button-groups';
import { FilterRangeAmount } from '@/shared/ui/filter/range-amount';
import {
  FilterMotionDuration,
  FilterMotionStyle,
  FilterMotionVariants
} from '@/entities/common/model/constant';
import moment from 'moment';
import { FilterInput } from '@/shared/ui/filter/input';
import { OrderStatus, PaymentStatus } from '../../model/ars/types';
import { getArsOrderStatusBtnGroup, getArsPaymentStatusBtnGroup } from '../../model/ars/constant';
import { useStore } from '@/shared/model/store';
import { FilterSelectMid } from '@/shared/ui/filter/select-mid';

export interface ArsFilterProps {
  filterOn: boolean;
  setFilterOn: (filterOn: boolean) => void;
  mid: string;
  moid: string;
  fromDate: string;
  toDate: string;
  paymentStatus: PaymentStatus;
  orderStatus: OrderStatus;
  minAmount?: number;
  maxAmount?: number;
  setMid: (mid: string) => void;
  setMoid: (moid: string) => void;
  setFromDate: (fromDate: string) => void;
  setToDate: (toDate: string) => void;
  setPaymentStatus: (paymentStatus: PaymentStatus) => void;
  setOrderStatus: (orderStatus: OrderStatus) => void;
  setMinAmount: (minAmount?: number) => void;
  setMaxAmount: (maxAmount?: number) => void;
};

export const ArsFilter = ({
  filterOn,
  setFilterOn,
  mid,
  moid,
  fromDate,
  toDate,
  paymentStatus,
  orderStatus,
  minAmount,
  maxAmount,
  setMid,
  setMoid,
  setFromDate,
  setToDate,
  setPaymentStatus,
  setOrderStatus,
  setMinAmount,
  setMaxAmount
}: ArsFilterProps) => {

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterMoid, setFilterMoid] = useState<string>(moid);
  const [filterFromDate, setFilterFromDate] = useState<string>(moment(fromDate).format('YYYYMMDD'));
  const [filterToDate, setFilterToDate] = useState<string>(moment(toDate).format('YYYYMMDD'));
  const [filterPaymentStatus, setFilterPaymentStatus] = useState<PaymentStatus>(paymentStatus);
  const [filterOrderStatus, setFilterOrderStatus] = useState<OrderStatus>(orderStatus);
  const [filterMinAmount, setFilterMinAmount] = useState<number | undefined>(minAmount);
  const [filterMaxAmount, setFilterMaxAmount] = useState<number | undefined>(maxAmount);

  const { t } = useTranslation();

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

  const onClickToSetFilter = () => {
    setMid(filterMid);
    setMoid(filterMoid);
    setFromDate(filterFromDate);
    setToDate(filterToDate);
    setPaymentStatus(filterPaymentStatus);
    setOrderStatus(filterOrderStatus);
    setMinAmount(filterMinAmount);
    setMaxAmount(filterMaxAmount);
    onClickToClose();
  };
  
  useEffect(() => {
    setFilterPaymentStatus(paymentStatus);
  }, [paymentStatus]);

  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>
            <FilterInput
              title={t('transaction.constants.orderNumber')}
              inputValue={ filterMoid }
              inputSetter={ setFilterMoid }
            ></FilterInput>
            <FilterCalendar
              title={t('filter.period')}
              startDate={ filterFromDate }
              endDate={ filterToDate }
              setStartDate={ setFilterFromDate }
              setEndDate={ setFilterToDate }
            ></FilterCalendar>
            <FilterButtonGroups
              title={t('transaction.filter.paymentStatus')}
              activeValue={ filterPaymentStatus }
              btnGroups={ getArsPaymentStatusBtnGroup(t) }
              setter={ setFilterPaymentStatus }
            ></FilterButtonGroups>
            <FilterButtonGroups
              title={t('transaction.filter.orderStatus')}
              activeValue={ filterOrderStatus }
              btnGroups={ getArsOrderStatusBtnGroup(t) }
              setter={ setFilterOrderStatus }
            ></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>
    </>
  );
};