import { useEffect, useState, useMemo } 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 {
  FilterMotionDuration,
  FilterMotionStyle,
  FilterMotionVariants
} from '@/entities/common/model/constant';
import moment from 'moment';
import { useStore } from '@/shared/model/store';
import { AlimtalkAlimCl, AlimtalkSearchCl, AlimTalkSendCl, AlimtalkSendType, ServiceCode } from '../../model/alimtalk/types';
import { FilterSelectInput } from '@/shared/ui/filter/select-input';
import { getAlimtalkAlimClBtnGroupForGeneral, getAlimtalkAlimClBtnGroupForVBank, getAlimtalkSearchClOptionGroup, getAlimtalkSendClBtnGroup, getAlimtalkSendTypeBtnGroup, getAlimtalkServiceCodeOptionGroup } from '../../model/alimtalk/constant';
import { FilterSelectMid } from '@/shared/ui/filter/select-mid';
import { useTranslation } from 'react-i18next';

export interface AlimtalkFilterProps {
  filterOn: boolean;
  setFilterOn: (filterOn: boolean) => void;
  mid: string;
  searchCl: AlimtalkSearchCl;
  searchValue: string;
  fromDate: string;
  toDate: string;
  serviceCode: ServiceCode;
  alimCl: AlimtalkAlimCl;
  sendType: AlimtalkSendType;
  sendCl: AlimTalkSendCl;
  setMid: (mid: string) => void;
  setSearchCl: (searchCl: AlimtalkSearchCl) => void;
  setSearchValue: (searchValue: string) => void;
  setFromDate: (fromDate: string) => void;
  setToDate: (toDate: string) => void;
  setServiceCode: (serviceCode: ServiceCode) => void;
  setAlimCl: (alimCl: AlimtalkAlimCl) => void;
  setSendType: (sendType: AlimtalkSendType) => void;
  setSendCl: (sendCl: AlimTalkSendCl) => void;
};

export const AlimtalkFilter = ({
  filterOn,
  setFilterOn,
  mid,
  searchCl,
  searchValue,
  fromDate,
  toDate,
  serviceCode,
  alimCl,
  sendType,
  sendCl,
  setMid,
  setSearchCl,
  setSearchValue,
  setFromDate,
  setToDate,
  setServiceCode,
  setAlimCl,
  setSendType,
  setSendCl
}: AlimtalkFilterProps) => {
  const { t } = useTranslation();

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterSearchCl, setFilterSearchCl] = useState<AlimtalkSearchCl>(searchCl);
  const [filterSearchValue, setFilterSearchValue] = useState<string>(searchValue);
  const [filterFromDate, setFilterFromDate] = useState<string>(moment(fromDate).format('YYYYMMDD'));
  const [filterToDate, setFilterToDate] = useState<string>(moment(toDate).format('YYYYMMDD'));
  const [filterServiceCode, setFilterServiceCode] = useState<ServiceCode>(serviceCode);
  const [filterAlimCl, setFilterAlimCl] = useState<AlimtalkAlimCl>(alimCl);
  const [filterSendType, setFilterSendType] = useState<AlimtalkSendType>(sendType);
  const [filterSendCl, setFilterSendCl] = useState<AlimTalkSendCl>(sendCl);

  // 결제수단에 따라 알림구분 버튼 그룹 선택
  const alimClBtnGroup = useMemo(() => {
    if (filterServiceCode === ServiceCode.VBANK) {
      return getAlimtalkAlimClBtnGroupForVBank(t);
    }
    return getAlimtalkAlimClBtnGroupForGeneral(t);
  }, [filterServiceCode, t]);

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

  const onClickToSetFilter = () => {
    setMid(filterMid);
    setSearchCl(filterSearchCl);
    setSearchValue(filterSearchValue);
    setFromDate(filterFromDate);
    setToDate(filterToDate);
    setServiceCode(filterServiceCode);
    setAlimCl(filterAlimCl);
    setSendType(filterSendType);
    setSendCl(filterSendCl);
    onClickToClose();
  };

  useEffect(() => {
    setFilterSendCl(sendCl);
  }, [searchCl]);

  // 결제수단이 변경되면 알림구분을 초기화
  useEffect(() => {
    setFilterAlimCl('' as AlimtalkAlimCl);
  }, [filterServiceCode]);

  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>
            <FilterSelectInput
              title={t('additionalService.alimtalk.orderOrTid')}
              selectValue={filterSearchCl}
              selectSetter={setFilterSearchCl}
              selectOptions={getAlimtalkSearchClOptionGroup(t)}
              inputValue={filterSearchValue}
              inputSetter={setFilterSearchValue}
            ></FilterSelectInput>
            <FilterSelect
              title={t('filter.paymentMethod')}
              selectValue={filterServiceCode}
              selectSetter={setFilterServiceCode}
              selectOptions={getAlimtalkServiceCodeOptionGroup(t)}
            ></FilterSelect>
            <FilterButtonGroups
              title={t('additionalService.alimtalk.notificationCategory')}
              activeValue={filterAlimCl}
              btnGroups={alimClBtnGroup}
              setter={setFilterAlimCl}
            ></FilterButtonGroups>
            <FilterCalendar
              title={t('filter.period')}
              startDate={filterFromDate}
              endDate={filterToDate}
              setStartDate={setFilterFromDate}
              setEndDate={setFilterToDate}
            ></FilterCalendar>
            <FilterButtonGroups
              title={t('additionalService.alimtalk.sendType')}
              activeValue={filterSendType}
              btnGroups={getAlimtalkSendTypeBtnGroup(t)}
              setter={setFilterSendType}
            ></FilterButtonGroups>
            <FilterButtonGroups
              title={t('additionalService.alimtalk.sendCategory')}
              activeValue={filterSendCl}
              btnGroups={getAlimtalkSendClBtnGroup(t)}
              setter={setFilterSendCl}
            ></FilterButtonGroups>
          </div>
          <div className="apply-row">
            <button
              className="btn-50 btn-blue flex-1"
              onClick={() => onClickToSetFilter()}
            >{t('filter.apply')}</button>
          </div>
        </div>
      </motion.div>
    </>
  );
};