export const appJumpTargetOptions = [
    ["wallet", "钱包"],
    ["room", "指定房间"],
    ["room_random", "随机房间"],
    ["room_window", "指定房内拉起窗口"],
    ["room_random_window", "随机房内拉起窗口"],
    ["room_game", "指定房内拉起游戏"],
    ["room_random_game", "随机房内拉起游戏"],
    ["explore_game", "Explore 游戏 H5"],
    ["custom", "自定义 JSON"],
];

export const appJumpWindowOptions = [
    ["game_window", "游戏窗口"],
    ["gift_panel", "礼物面板"],
    ["red_packet_claim", "领红包弹窗"],
    ["wheel", "转盘窗口"],
];

export const appJumpExploreGameCodeOptions = [
    ["game_dice", "Dice Battle"],
    ["game_rock", "Rock Paper Scissors"],
];

const knownTargets = new Set(appJumpTargetOptions.map(([value]) => value).filter((value) => value !== "custom"));
const knownWindows = new Set(appJumpWindowOptions.map(([value]) => value));
const knownExploreGameCodes = new Set(appJumpExploreGameCodeOptions.map(([value]) => value));

export function normalizeJumpConfigType(value) {
    return String(value || "")
        .trim()
        .toLowerCase() === "app"
        ? "app"
        : "h5";
}

export function parseAppJumpParam(rawParam) {
    const raw = String(rawParam || "").trim();
    const data = parseObject(raw);
    if (!data) {
        return defaultParsedJump(raw);
    }

    const type = readString(data.type);
    const target = knownTargets.has(type) ? type : type ? "custom" : "wallet";
    const window = normalizeWindow(data.window);

    return {
        gameCode: normalizeExploreGameCode(data.game_code ?? data.gameCode),
        gameId: readString(data.game_id ?? data.gameId),
        giftId: readString(data.gift_id ?? data.giftId),
        raw,
        redPacketNo: readString(data.red_packet_no ?? data.redPacketNo),
        roomId: readString(data.room_id ?? data.roomId),
        target,
        window,
    };
}

export function buildAppJumpParam(values) {
    const target = knownTargets.has(values?.target) ? values.target : "wallet";
    const payload = { type: target };
    const roomId = readString(values?.roomId);
    const gameId = readString(values?.gameId);
    const gameCode = normalizeExploreGameCode(values?.gameCode);
    const giftId = readString(values?.giftId);
    const redPacketNo = readString(values?.redPacketNo);
    const window = normalizeWindow(values?.window);

    if (target === "room" || target === "room_window" || target === "room_game") {
        payload.room_id = roomId;
    }
    if (target === "room_window" || target === "room_random_window") {
        payload.window = window;
        appendWindowParam(payload, window, { gameId, giftId, redPacketNo });
    }
    if (target === "room_game" || target === "room_random_game") {
        payload.game_id = gameId;
    }
    if (target === "explore_game") {
        payload.game_code = gameCode;
    }

    return JSON.stringify(payload);
}

export function validatePublicAppJumpParam(rawParam) {
    const raw = String(rawParam || "").trim();
    if (!raw) {
        return { message: "请填写 APP 跳转参数", valid: false };
    }
    const data = parseObject(raw);
    if (!data) {
        return { message: "APP 跳转参数必须是 JSON 对象", valid: false };
    }

    const target = readString(data.type);
    if (!knownTargets.has(target)) {
        return { message: "APP 跳转 type 不在公共跳转方案内", valid: false };
    }

    const roomId = readString(data.room_id ?? data.roomId);
    const gameId = readString(data.game_id ?? data.gameId);
    const gameCode = readString(data.game_code ?? data.gameCode);
    const window = readString(data.window);

    if ((target === "room" || target === "room_window" || target === "room_game") && !roomId) {
        return { message: "该 APP 跳转需要填写 room_id", valid: false };
    }
    if ((target === "room_game" || target === "room_random_game") && !gameId) {
        return { message: "该 APP 跳转需要填写 game_id", valid: false };
    }
    if ((target === "room_window" || target === "room_random_window") && !knownWindows.has(window)) {
        return { message: "该 APP 跳转需要填写有效 window", valid: false };
    }
    if (target === "explore_game" && !knownExploreGameCodes.has(gameCode)) {
        return { message: "Explore 游戏 H5 需要选择有效 game_code", valid: false };
    }

    return { message: "", valid: true };
}

function defaultParsedJump(raw = "") {
    return {
        gameCode: "game_dice",
        gameId: "",
        giftId: "",
        raw,
        redPacketNo: "",
        roomId: "",
        target: raw ? "custom" : "wallet",
        window: "gift_panel",
    };
}

function appendWindowParam(payload, window, values) {
    if (window === "game_window" && values.gameId) {
        payload.game_id = values.gameId;
    }
    if (window === "gift_panel" && values.giftId) {
        payload.gift_id = values.giftId;
    }
    if (window === "red_packet_claim" && values.redPacketNo) {
        payload.red_packet_no = values.redPacketNo;
    }
}

function normalizeWindow(value) {
    const window = readString(value);
    return knownWindows.has(window) ? window : "gift_panel";
}

function normalizeExploreGameCode(value) {
    const gameCode = readString(value);
    return knownExploreGameCodes.has(gameCode) ? gameCode : "game_dice";
}

function parseObject(raw) {
    if (!raw) {
        return null;
    }
    try {
        const parsed = JSON.parse(raw);
        if (parsed && !Array.isArray(parsed) && typeof parsed === "object") {
            return parsed;
        }
    } catch {
        return null;
    }
    return null;
}

function readString(value) {
    return String(value ?? "").trim();
}
