import { APP_SERVER } from "@constants/app";
import { connectServer, disconnectServer, onDisconnect, sendToServer } from "@features/socketSlice";
import { reqBankruptcyGift, reqFirstPayGift, reqFreeWithdraw, reqFreeWithdrawMission, reqGameInfo, reqHolidaysActInfo, reqHolidaysGiftInfo, reqMailCheck } from "@features/api";
import { updatePayFinish } from "@features/paySlice";
import { trackEvent } from "../../tracking/TrackAll";
import { toast } from "react-toastify";
import { useLocation } from "react-router-dom";
import { updateServerKick } from "@features/mainSlice";

const roomName = APP_SERVER.ROOM_HALL;
const serverUrl = APP_SERVER.SERVER_URL;
const i18n = global.i18n;
//信息处理
const __playground_message_types = (message, dispatch) => {

}
const handlePayFinish = (message, dispatch) => {
    setTimeout(() => {
        dispatch(updatePayFinish(message));
        dispatch(reqFirstPayGift());
        dispatch(reqFreeWithdraw());
        dispatch(reqFreeWithdrawMission());
        dispatch(reqBankruptcyGift());
        dispatch(reqHolidaysGiftInfo());
        dispatch(reqHolidaysActInfo());
    }, 0);
    let { data } = message;
    if (data && data.PayNum >= 200) global.showDropCoins();
    setTimeout(() => {
        dispatch(reqGameInfo());
    }, 1000);
}
const handleKick = (message, dispatch) => {
    let onSure = () => {
        dispatch(updateServerKick(0));
        dispatch(connectHall());
    }
    let data = {
        content: i18n.t("hall.offline"),
        sureCB: onSure,
        onlySure: true,
        isLockClose: true,
        sureLabel: i18n.t("hall.reconnect"),
    }
    global.msgBoxShow(data)
    dispatch(updateServerKick(1));
}
const handleHallTimeout = (message, dispatch) => {
    let onSure = () => {
        dispatch(connectHall());
    }
    let data = {
        content: i18n.t("hall.disconnected"),
        sureCB: onSure,
        onlySure: true,
        isLockClose: true,
        sureLabel: i18n.t("hall.reconnect"),
    }
    global.msgBoxShow(data)
}
const roger = (message, dispatch) => {
    // console.log(message);
    if (message) {
        let { cmd } = message;
        switch (cmd) {
            case "pay_finish": {
                //充值完成 message={cmd:...,data:{Golds:12.xx,PayNum:10}}
                handlePayFinish(message, dispatch)
                //顺带处理打点
                let { data } = message;
                if (!!data && !!data.event) {
                    trackEvent("pay", data.params);
                }
                break;
            }
            case "call_client": {
                //调用客户端接口，比如将进入游戏的客户端踢出返回，强制登出等 message={cmd:"",data:{}}
                if (!!message.data && !!message.data.type) {
                    let { data } = message;
                    switch (message.data.type) {
                        case "track": {
                            trackEvent(data.event, data.params)
                            break;
                        }
                        case "refresh_mail": {
                            //有新的邮件刷新邮件
                            //播放动画newmail.json
                            global.showMailNew();
                            //刷新mail 红点
                            dispatch(reqMailCheck());
                            break;
                        }
                        case "start_change": {
                            global.updateGameScore(data.Golds);
                        }
                    }


                }
                break;
            }
        }
    }
}
const chat = (message, dispatch) => {

}
const operate = (message, dispatch) => {
    let { cmd } = message;
    switch (cmd) {
        case "id": {
            dispatch(hallSendState());
            break;
        }
        case "state": {
            break;
        }
        //踢
        case "kick": {
            // send("operate",{cmd:"kick",data:{reason:"hall.other_online"}});
            handleKick(message, dispatch);
            break;
        }
    }
}
//信息处理
const messageHandler = {
    __playground_message_types,
    roger,
    chat,
    operate
}
const onHallConnect = () => {
    return (dispatch, getState) => {
        dispatch(hallSendReg());
        // dispatch(hallSendState(0));
        // dispatch(hallSendChat("123456"));
        // setInterval(()=>{
        //     dispatch(hallSendChat("test"));
        // },1000);
    }
}
const onHallTimeoutClose = () => {
    return (dispatch, getState) => {
        handleHallTimeout("", dispatch);
    }
}
const send = (type, message, dispatch) => {
    dispatch(sendToServer({
        roomName,
        type,
        message
    }));
}
export const onHallMessage = (type, message) => {
    return (dispatch, getState) => {
        let handler = messageHandler[type];
        if (handler != null) {
            handler(message, dispatch);
        }
    }
}
export const connectHall = (hallServerUrl = '') => {
    if (hallServerUrl === '') hallServerUrl = serverUrl;
    return (dispatch, getState) => {
        let state = getState();
        let url = state.main.hallServerUrl;
        if (url === '') url = serverUrl;
        dispatch(connectServer({
            roomName: roomName,
            serverUrl: hallServerUrl,
            messageCB: onHallMessage,
            onRoomConnect: onHallConnect,
            onRoomTimeoutClose: onHallTimeoutClose,
        }));
    }
}
export const disconnectHall = () => {
    return disconnectServer(roomName);
}
export const hallSendReg = () => {
    return (dispatch, getState) => {
        let main = getState().main;
        let userInfo = main.userInfo;
        let { UserID, GlobalUID, FaceID, NickName } = userInfo;
        send("id", { UserID, GlobalUID, FaceID, NickName }, dispatch);
    }
}
export const hallSendChat = (message) => {
    return (dispatch, getState) => {
        send("chat", message, dispatch);
    }
}
export const hallSendState = () => {
    return (dispatch, getState) => {
        let main = getState().main;
        let gameID = main.gameID;
        let message = { in_game: gameID };
        send("state", message, dispatch);
    }
}
