package com.example.demo.module.user;

import com.example.demo.common.GlobalRoot;
import com.example.demo.module.friend.FriendService;
import com.example.demo.module.login.GameLoginService;
import com.example.demo.table.UserDayActive;
import pers.jc.engine.JCEngine;
import pers.jc.engine.JCEntity;
import pers.jc.network.SocketFunction;
import pers.jc.network.SocketMethod;
import pers.jc.sql.SQL;
import pers.jc.util.JCLogger;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

/**
 * Socket实体-玩家
 */
public class Player extends JCEntity {
    public static Map<Integer, Player> playerMap = new HashMap<>();
    public int userID;

    @Override
    public void onLoad() {
        JCLogger.info("player onLoad", id);
    }

    @Override
    public void onDestroy() {
        if (userID != 0) {
            playerMap.remove(userID, this);
            JCEngine.executorService.execute(() -> {
                GameLoginService.ins.setUserOfflineTime(userID);
            });
            JCLogger.info("认证玩家", userID, "退出，目前在线玩家数量：", playerMap.size());
        }
        JCLogger.info("player onDestroy", id);
    }

    //旧版认证接口
    @SocketFunction(auth = false)
    public void authToken(String idAndToken) {
        authToken2(idAndToken, "Home");
    }

    //被客户端调用的方法
    @SocketFunction(auth = false)
    public void authToken2(String idAndToken, String sceneName) {
        String[] strArr = idAndToken.split("&");
        int uid = Integer.parseInt(strArr[0]);
        String token = strArr[1];
        JCLogger.info("uid", uid, "token", token);
        JCEngine.executorService.execute(() -> {
            boolean authRes = GameLoginService.ins.authToken(uid, token, sceneName);
            director.callbackHandler.offerCallback(() -> {
                if (authRes) {
                    Player player = playerMap.get(uid);
                    if (player != null) player.channel.close();
                }
                if (authRes) {
                    authed = true;
                    userID = uid;
                    if (!isValid) return;
                    playerMap.put(userID, this);
                    JCLogger.info("认证玩家", userID, "登录，目前在线玩家数量：", playerMap.size());
                }
                onAuthRes(authRes);
                if (authRes) {
                    checkHasFriendTip();
                }
            });
            if (authRes) {
                //记录日活登录
                UserDayActive userDayActive = new UserDayActive();
                userDayActive.setId(uid);
                GlobalRoot.db.insertIgnore(userDayActive);
            }
        });
    }

    //向客户端发送的请求
    public void onAuthRes(boolean res) {
        call("onAuthRes", res);
    }
    public void onHasFriendTip() {
        call("onHasFriendTip");
    }

    //其它方法
    void checkHasFriendTip() {
        JCEngine.executorService.execute(() -> {
            boolean hasFriend = FriendService.ins.checkHasFriendRequest(userID);
            if (hasFriend) {
                onHasFriendTip();
            }
        });
    }

    @SocketFunction
    public void sbLog(int uid, long time, String tag, String info) {
        JCEngine.executorService.execute(() -> {
            GlobalRoot.db.insert(new SQL(){{
                INSERT_INTO("sb_log");
                VALUES("uid", PARAM(uid));
                VALUES("time", PARAM(new Timestamp(time)));
                VALUES("tag", PARAM(tag));
                VALUES("info", PARAM(info));
            }});
        });
    }
}
