import { useEffect, useState } from 'react';
import { useNavigate } from '@/shared/lib/hooks/use-navigate';
import { Swiper, SwiperSlide } from 'swiper/react';
import 'swiper/css';
import { IMAGE_ROOT } from '@/shared/constants/common';
import { UserFavorite } from '@/entities/user/model/types';
import { useStore } from '@/shared/model/store';
import { useTranslation } from 'react-i18next';

/*
const items: Array<UserFavorite> = [
  {img: IMAGE_ROOT + '/ico_menu_01.svg', title: '지급대행'},
  {img: IMAGE_ROOT + '/ico_menu_02.svg', title: '거래내역조회'},
  {img: IMAGE_ROOT + '/ico_menu_03.svg', title: '정산달력'},
  {img: IMAGE_ROOT + '/ico_menu_02.svg', title: '거래내역조회'},
  {img: IMAGE_ROOT + '/ico_menu_03.svg', title: '정산달력'}
];
*/
export interface FavoriteWrapperProps {
  usingType: 'home' | 'menu',
  editMode?: boolean,
  setEditMode?: (editMode: boolean) => void;
  changeMenuId?: string;
  setMenuOn?: (menuOn: boolean) => void;
};

export const FavoriteWrapper = ({
  usingType,
  editMode,
  setEditMode,
  changeMenuId,
  setMenuOn
}: FavoriteWrapperProps) => {
  const { navigate } = useNavigate();
  const { i18n, t } = useTranslation();

  const [favoriteItems, setFavoriteItems] = useState<Array<UserFavorite>>([]);
  
  const itemAdd: UserFavorite = {
    menuId: 0,
    menuName: t('favorite.edit'),
    menuNameEng: t('favorite.edit'),
    iconFilePath: IMAGE_ROOT + '/ico_menu_plus.svg',
    programPath: '',
  };
  
  const onClickToFavoriteEdit = () => {
    if(setEditMode){
      setEditMode(true);
    }
  };
  
  const onClickToNavigate = (path?: string) => {
    if(!!path){
      navigate(path);
      if(setMenuOn){
        setMenuOn(false);
      }
    }
  };

  const getFavoriteList = () => {
    let userFavorite = useStore.getState().UserStore.userFavorite;
    setFavoriteItems(userFavorite);
  };

  const makeFavoriteItems = () => {
    let rs = [];
    for(let i=0;i<favoriteItems.length;i++){
      const displayName = i18n.language === 'en' && favoriteItems[i]?.menuNameEng
        ? favoriteItems[i]?.menuNameEng
        : favoriteItems[i]?.menuName;

      rs.push(
        <SwiperSlide key={ `favorite-slide-key-${i}` }>
          <div
            className="swiper-item"
            onClick={ () => !editMode && onClickToNavigate(favoriteItems[i]?.programPath) }
          >
            <div className="swiper-icon coin-icon">
              <img
                src={ favoriteItems[i]?.iconFilePath || '' }
                alt={ displayName }
              />
            </div>
            <span className="swiper-text">{ displayName }</span>
          </div>
        </SwiperSlide>
      );
    }
    return rs;
  };
  const makeAddFavoriteItem = () => {
    let rs = [];
    if(!editMode && usingType === 'menu'){
      rs.push(
        <SwiperSlide key={ `favorite-item-add-slide-key` }>
          <div 
            className="swiper-item"
            onClick={ onClickToFavoriteEdit }
          >
            <div className="swiper-icon coin-icon">
              <img 
                src={ itemAdd.iconFilePath || '' } 
                alt={ itemAdd.menuName } 
              />
            </div>
            <span className="swiper-text">{ itemAdd.menuName }</span>
          </div>
        </SwiperSlide>
      );
    }
    
    return rs;
  };

  useEffect(() => {
    getFavoriteList();
  }, [changeMenuId]);

  return (
    <>
      <Swiper
        spaceBetween={ 9 }
        slidesPerView={ 4 }
        style={{height: ((usingType === 'home') && (favoriteItems.length < 1))? '0px': '75px'}}
      >
      { (favoriteItems.length > 0) && makeFavoriteItems() }
      { makeAddFavoriteItem() }
      </Swiper>
    </>
  );
};
