import { GameListRep, GameRequest, getGameDetailApi } from "@/api/home";
import { useEventPoint } from "@/hooks/useEventPoint";
import { defaultLocale, usePathname, useRouter } from "@/i18n/routing";
import { getCookies } from "@/utils/Cookies";
import { Toast } from "antd-mobile";
import { useTranslations } from "next-intl";
// 地址白名单
const whiteUrls = ["192.168.0.84", "localhost", "192.168.0.46"];
const useGame = () => {
    const router = useRouter();
    const t = useTranslations();
    const { eventStartTrial } = useEventPoint();
    const pathname = usePathname();

    const getCoinType = (game: GameListRep, mode: number) => {
        const type = game.coin_types!;
        // 本金加彩金
        if (mode) return mode;
        if (type & 1 || type & 2) {
            return 1;
        }
        // 免费币
        if (type & 4) {
            return 2;
        }
        // 重币
        if (type & 8) {
            return 3;
        }
        return;
    };
    const getGameUrl = (game: GameListRep, params: GameRequest) => {
        Toast.show({
            icon: "loading",
            duration: 0,
            maskStyle: { zIndex: 99999, background: "rgba(0,0,0,0.5)" },
        });
        // const brand = brandList.find((item) => item.gid === game.game_id)?.brand ?? "";
        const lang = getCookies("language") || defaultLocale;
        getGameDetailApi({ ...params, language: lang === "br" ? "pt" : lang })
            .then((res) => {
                Toast.clear();
                if (res.data && res.data.game_url) {
                    eventStartTrial();
                    const game_url = res.data.game_url;

                    // 获取游戏的品牌
                    const gameId = game?.game_id || "";

                    // 然后是文档对象
                    if (game_url.indexOf("!doctype") !== -1) {
                        sessionStorage.setItem("game_url", res.data.game_url);
                        const url = `${encodeURI(window.location.href)}&return_url=${encodeURI(window.location.href.replace(pathname, ""))}&category_name=${game.provider}`;
                        router.push(`/game?${url}`);
                        return;
                    }
                    const url = `${encodeURI(res.data.game_url)}&return_url=${encodeURI(window.location.href.replace(pathname, ""))}&category_name=${game.provider}`;

                    // 如果是pp游戏,则当前窗口打开新的地址，不需要加return_url
                    // gid 如果开头是1的6位数的数字，则认为是pp游戏
                    let isPP = /^1\d{5}$/.test(gameId);
                    // 如果是play游戏,则当前窗口打开新的地址
                    const isPlay = /^5\d{5}$/.test(gameId);
                    if (isPP || isPlay) {
                        // const ppUrl = `${encodeURI(res.data.game_url)}&category_name=${game.provider}`;
                        window.open(url, "_self");
                        return;
                    }

                    if (new RegExp("https").test(game_url)) {
                        // 如果是https
                        router.push(`/game?${url}`);
                        return;
                    } else {
                        window.open(url);
                    }
                } else {
                    Toast.show(t("code.500"));
                }
            })
            .catch((error) => {
                Toast.clear();
                Toast.show(t(`code.${error.data?.code || 500}`));
            });
    };
    return {
        getGameUrl: getGameUrl,
        getCoinType: getCoinType,
    };
};

export default useGame;
