import { useEffect } from 'react';
import { useState } from 'react';
import { motion } from 'framer-motion';
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 { 
  AllTransactionMoidTidOptionsGroup,
  AllTransactionStateCodeBtnGroup,
  AllTransactionServiceCodeOptionsGroup,
  AllTransactionCardBankCodeOptionsGroup,
} from '@/entities/transaction/model/contant';
import { 
  AllTransactionFilterProps,
  AllTransactionSearchCl,
  AllTransactionStateCode,
  AllTransactionServiceCode,
  AllTransactionMoidTid
} from '../../model/types';
import { 
  FilterMotionDuration,
  FilterMotionStyle,
  FilterMotionVariants
} from '@/entities/common/model/constant';

export const AllTransactionFilter = ({
  filterOn,
  setFilterOn,
  mid,
  fromDate,
  toDate,
  stateCode,
  serviceCode,
  minAmount,
  maxAmount,
  cardCode,
  bankCode,
  searchCl,
  searchValue,
  setMid,
  setMoid,
  setTid,
  setFromDate,
  setToDate,
  setStateCode,
  setServiceCode,
  setMinAmount,
  setMaxAmount,
  setCardCode,
  setBankCode,
  setSearchCl,
  setSearchValue,
}: AllTransactionFilterProps) => {

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterMoidTidType, setFilterMoidTidType] = useState<AllTransactionMoidTid>(AllTransactionMoidTid.MOID);
  const [filterMoidTidValue, setFilterMoidTidValue] = useState<string>('');
  const [filterFromDate, setFilterFromDate] = useState<string>(fromDate);
  const [filterToDate, setFilterToDate] = useState<string>(toDate);
  const [filterStateCode, setFilterStateCode] = useState<AllTransactionStateCode>(stateCode);
  const [filterServiceCode, setFilterServiceCode] = useState<AllTransactionServiceCode>(serviceCode);
  const [filterMinAmount, setFilterMinAmount] = useState<number | undefined>(minAmount);
  const [filterMaxAmount, setFilterMaxAmount] = useState<number | undefined>(maxAmount);
  
  const [filterCardCode, setFilterCardCode] = useState<string | undefined>(cardCode);
  const [filterBankCode, setFilterBankCode] = useState<string | undefined>(bankCode);

  const [filterCardBankCode, setFilterCardBankCode] = useState<string>('');

  const [filterSearchCl, setFilterSearchCl] = useState<AllTransactionSearchCl | undefined>(searchCl);
  const [filterSearchValue, setFilterSearchValue] = useState<string | undefined>(searchValue);

  const [searchClOptionsGroup, setSearchClOptionsGroup] = useState<Array<Record<string, string>>>([]);
  const onClickToClose = () => {
    setFilterOn(false);
  };
  
  const setNewDate = (newDate: any) => {
    console.log(newDate)
  };

  const onClickToSetFilter = () => {
    setMid(filterMid);
    if(filterMoidTidType === AllTransactionMoidTid.MOID){
      setMoid(filterMoidTidValue);
      setTid('');
    }
    else if(filterMoidTidType === AllTransactionMoidTid.TID){
      setMoid('');
      setTid(filterMoidTidValue);
    }
    setFromDate(filterFromDate);
    setToDate(filterToDate);
    setStateCode(filterStateCode);

    setServiceCode(filterServiceCode);
    setMinAmount(filterMinAmount);
    setMaxAmount(filterMaxAmount);
    setBankCode(filterBankCode);
    setCardCode(filterCardCode);
    setSearchCl(filterSearchCl);
    setSearchValue(filterSearchValue);
    onClickToClose();
  };
  let MidOptions = [
    {name: 'nictest001m', value: 'nictest001m'}
  ];

  let setSearchClOptions = (value: AllTransactionServiceCode) => {
    let options = [];
    setFilterServiceCode(value);
    if(value === AllTransactionServiceCode.ALL){

    }
    else if(value === AllTransactionServiceCode.CREDIT_CARD){
      options.push({name: '카드번호', value: AllTransactionSearchCl.CARD_NO});
      options.push({name: '승인번호', value: AllTransactionSearchCl.CARD_APPROVAL_NO});
    }
    else if(value === AllTransactionServiceCode.VIRTUAL_ACCOUNT){
      options.push({name: '가상계좌번호', value: AllTransactionSearchCl.VACCT_NO});
      options.push({name: '입금자명', value: AllTransactionSearchCl.VACCT_DEPOSIT_NM});
    }
    else if(value === AllTransactionServiceCode.ACCOUNT_TRANSFER){
      options.push({name: '구매자명', value: AllTransactionSearchCl.BANK_BUYER_NM});
    }
    else if(value === AllTransactionServiceCode.ACCOUNT_SIMPLE_TRANSFER){
      
    }
    else if(value === AllTransactionServiceCode.MOBILE_PAYMENT){
      options.push({name: '휴대폰번호', value: AllTransactionSearchCl.TEL_NO});
    }
    else if(value === AllTransactionServiceCode.SSGMONEY){
      options.push({name: '상품권번호', value: AllTransactionSearchCl.SSGMONEY_GIFT_NO}); 
    }
    else if(value === AllTransactionServiceCode.SSGBANK){
      options.push({name: '승인번호', value: AllTransactionSearchCl.SSGBANK_APPROVAL_NO}); 
    }
    else if(value === AllTransactionServiceCode.CULT){
      options.push({name: '컬처랜드ID', value: AllTransactionSearchCl.SSGBANK_APPROVAL_NO}); 
    }
    else if(value === AllTransactionServiceCode.TMONEY){
      options.push({name: '티머니번호', value: AllTransactionSearchCl.TMONEY_CARD_NO}); 
    }
    setSearchClOptionsGroup(options);
  };

  /*
  useEffect(() => {
    setFilterDeliveryStatus(deliveryStatus);
  }, [deliveryStatus]);
  */

  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">필터</div>
            <div className="full-menu-actions">
              <button 
                id="closeFullMenu"
                className="full-menu-close"
              >
                <img 
                  src={ IMAGE_ROOT + '/ico_close.svg' }
                  alt="닫기"
                  onClick={ () => onClickToClose() }
                />
              </button>
            </div>
          </div>
      
          <div className="option-list pt-16">
            <FilterSelect
              title='가맹점'
              selectValue={ filterMid }
              selectSetter={ setMid }
              selectOptions={ MidOptions }
            ></FilterSelect>
            <FilterSelectInput
              title='주문번호/TID'
              selectValue={ filterMoidTidType }
              selectSetter={ setFilterMoidTidType }
              selectOptions={ AllTransactionMoidTidOptionsGroup }
              inputValue={ filterMoidTidValue }
              inputSetter={ setFilterMoidTidValue }
            ></FilterSelectInput>
            <FilterCalendar
              title='조회기간'
              startDate={ filterFromDate }
              endDate={ filterToDate }
              setStartDate={ setFilterFromDate }
              setEndDate={ setFilterToDate }
            ></FilterCalendar>
            
            <FilterButtonGroups
              title='거래상태'
              activeValue={ filterStateCode }
              btnGroups={ AllTransactionStateCodeBtnGroup }
              setter={ setFilterStateCode }
            ></FilterButtonGroups>
            <FilterSelect
              title='결제수단'
              selectValue={ filterServiceCode }
              selectSetter={ setSearchClOptions }
              selectOptions={ AllTransactionServiceCodeOptionsGroup }
            ></FilterSelect>
            
            <FilterRangeAmount
              title='거래금액'
              minAmount={ filterMinAmount }
              maxAmount={ filterMaxAmount }
              setMinAmount={ setFilterMinAmount }
              setMaxAmount={ setFilterMaxAmount }
            ></FilterRangeAmount>
            {/*
            <FilterSelect
              title='카드사/은행'
              selectValue={}
              selectSetter={}
              selectOptions={}
            ></FilterSelect>
            */}
            <FilterSelectInput
              title='상세조회'
              selectValue={ filterSearchCl }
              selectSetter={ setFilterSearchCl }
              selectOptions={ searchClOptionsGroup }
              inputValue={ filterSearchValue }
              inputSetter={ setFilterSearchValue }
            ></FilterSelectInput>
            
          </div>
          <div className="apply-row">
            <button 
              className="btn-50 btn-blue flex-1"
              onClick={ () => onClickToSetFilter() }
            >적용</button>
          </div>
        </div>
      </motion.div>
    </>
  );
};