"use client";
import Box from "@/components/Box";
import { FC, PropsWithChildren, ReactNode, useRef } from "react";

import { Swiper, SwiperClass, SwiperSlide } from "swiper/react";

import { Category, GameListRep } from "@/api/home";
import { CardProps } from "@/components/Card/Card";
import GroupCard, { GroupProps } from "@/components/Card/GroupCard";
import clsx from "clsx";
import { useTranslations } from "next-intl";
import styles from "./style.module.scss";

type TodoHandler = (item: Category) => void;

interface SwiperGroupProps extends GroupProps, CardProps {
    group: Category;
    page?: number;
    // style
    bg?: boolean;
    visibleTitle?: boolean;
    // todos
    visibleTodos?: boolean;
    todoHandler?: TodoHandler;
    // icon
    offsetX?: number;
    offsetY?: number;

    // SwiperSlide
    slideRender?: (data: GameListRep[]) => ReactNode;
}

const HomeSwiper: FC<PropsWithChildren<SwiperGroupProps>> = (props) => {
    const {
        group,
        page = 3,
        todoHandler,
        bg = true,
        visibleTitle = true,
        visibleTodos = true,
        slideRender,
        ...other
    } = props;

    const t = useTranslations("HomePage");
    const swiperRef = useRef<SwiperClass | null>(null);

    const bodyClass = clsx(
        {
            ["bg-[#1a1a1a]"]: bg,
        },
        "mb-[0.11rem]"
    );
    if (!group.game_list) return;
    const lineNum = page * group.line_num;

    const gameList = group.game_list.slice(0, group.line_config_amount);
    const swiperData = Array(gameList.length ? Math.ceil(gameList.length / lineNum) : 0)
        .fill(0)
        .map((_, index) => {
            return {
                key: index,
                data: gameList
                    .slice(index * lineNum, index * lineNum + lineNum)
                    .map((subItem) => ({ ...subItem, category_name: group.category_name })),
            };
        });
    const prev = () => {
        swiperRef.current!.slidePrev();
    };
    const next = () => {
        swiperRef.current!.slideNext();
    };
    const iconClass = clsx(styles.mainLeftIcon, "pro-iconfont", `pro-${group.icon}`);
    return (
        <div>
            {visibleTitle && (
                <Box none>
                    <div className={styles.mainTitle}>
                        <div className={styles.mainLeft}>
                            <div className={iconClass}></div>
                            <div className={styles.marginLeftTitle}>{group.category_name}</div>
                            <div className={styles.marginLeftTag}> {t("gameTag")}</div>
                        </div>
                        <img
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GtG87Nw2dh2OK78qsmOcidnJRTQvO7d6fsNjeEmNR0Ymkd1exGxl51bPThzBN9iD85tdTkRsJnEbz2U4ihM4jKubXExEbCxxm8yF2Ks8G/cpHsW1TS4oIs6Wa26z9Svewof4CJ/jdJ2JueYWMVuJ23ydxvf4evg+wC84idXh6xT+HgwGq00tMqKN/gHg555imkrLwAAAAABJRU5ErkJggg=="
                            alt=""
                        />
                        <div className={styles.mainRight}>
                            {visibleTodos && (
                                <div onClick={() => todoHandler && todoHandler(group)}>
                                    Todos
                                    <span className={styles.marginRightCount}>{group.to_tal}</span>
                                </div>
                            )}

                            <div onClick={prev}>
                                <span className="iconfont icon-xiangzuo1"></span>
                            </div>
                            <div onClick={next}>
                                <span className="iconfont icon-xiangyou1"></span>
                            </div>
                        </div>
                    </div>
                </Box>
            )}

            <Box none className={bodyClass}>
                <Box>
                    <Swiper
                        spaceBetween={10}
                        slidesPerView={1}
                        onSwiper={(swiper) => {
                            swiperRef.current = swiper;
                        }}
                        loop
                    >
                        {swiperData?.map((data, index) => (
                            <SwiperSlide key={data.key}>
                                {slideRender ? (
                                    slideRender(data.data)
                                ) : (
                                    <GroupCard
                                        data={data.data}
                                        {...other}
                                        groupType={1}
                                    ></GroupCard>
                                )}
                            </SwiperSlide>
                        ))}
                    </Swiper>
                </Box>
            </Box>
        </div>
    );
};

export default HomeSwiper;
