import { ECurrencyType, EExploreFilter, ENetworkType, ESaleType } from '@src/constants/Enums';
import { ICollection, INFT } from 'metarare';
import { useEffect, useState } from 'react';

interface IOwnProps {
  list: INFT[];
}

const useExploreFilter = ({ list }: IOwnProps) => {
  // 블록 체인
  const [networkType, setNetworkType] = useState<ENetworkType | ''>(ENetworkType.ETH);

  // 카테고리
  // const [category, setCategory] = useState<ECategory>(ECategory.ALL);

  // 컬렉션
  const [collection, setCollection] = useState<string>('');

  // 판매 타입
  const [saleType, setSaleType] = useState<ESaleType | ''>('');

  // 가격 범위
  const [selectedCurrency, setSelectedCurrency] = useState<ECurrencyType>(ECurrencyType.ETH);
  const [minPrice, setMinPrice] = useState<string>('');
  const [maxPrice, setMaxPrice] = useState<string>('');

  // 정렬
  const [selectedFilterType, setSelectedFilterType] = useState<EExploreFilter | ''>('');
  const [exploreList, setExploreList] = useState<INFT[]>(list);

  useEffect(() => {
    if (selectedFilterType === EExploreFilter.NEW) {
      // 최신순
      setExploreList(list.slice().sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()));
    } else if (selectedFilterType === EExploreFilter.ROW_PRICE) {
      // 가격 낮은순
      setExploreList(list.slice().sort((a, b) => Number(a.current_price || 0) - Number(b.current_price || 0)));
    } else if (selectedFilterType === EExploreFilter.HIGH_PRICE) {
      // 가격 높으순
      setExploreList(list.slice().sort((a, b) => Number(b.current_price || 0) - Number(a.current_price || 0)));
    } else {
      setExploreList(list);
    }
  }, [selectedFilterType, list]);

  return {
    networkType,
    setNetworkType,
    collection,
    setCollection,
    saleType,
    setSaleType,
    selectedCurrency,
    setSelectedCurrency,
    minPrice,
    setMinPrice,
    maxPrice,
    setMaxPrice,
    selectedFilterType,
    setSelectedFilterType,
    exploreList
  };
};

export default useExploreFilter;
