package com.example.demo.module.user;

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

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;
    public LoginToken m_loginToken;

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

    @Override
    public void onDestroy() {
        if (userID != 0) {
            playerMap.remove(userID, this);
            JCEngine.asyncService.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 clientToken, String sceneName) {
        JCLogger.info("clientToken", clientToken);
        boolean authRes = false;
        LoginToken loginToken = LoginToken.parseClientToken(clientToken);
        if (loginToken != null && loginToken.isMD5SignRight()
            && loginToken.serverIndex == GlobalConfig.serverConfig.getServerIndex()
        ) {
            //如果该账号是在线的，根据失效时间看要强退谁
            Player samePlayer = playerMap.get(loginToken.uid);
            if (samePlayer == null) {
                authRes = true;
            } else if (samePlayer != null && loginToken.expireTime >= samePlayer.m_loginToken.expireTime) {
                samePlayer.channel.close();
                authRes = true;
            }
        }
        if (authRes) {
            authed = true;
            userID = loginToken.uid;
            m_loginToken = loginToken;
            playerMap.put(userID, this);
            JCLogger.info("认证玩家", userID, "登录，目前在线玩家数量：", playerMap.size());
        }
        onAuthRes(authRes);
        if (authRes) {
            checkHasFriendTip();
            recordUserDayActive();
        }
    }

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

    //其它方法
    void checkHasFriendTip() {
        JCEngine.asyncService.execute(() -> {
            boolean hasFriend = FriendService.ins.checkHasFriendRequest(userID);
            if (hasFriend) {
                onHasFriendTip();
            }
        });
    }
    void recordUserDayActive() {
        JCEngine.asyncService.execute(() -> {
            UserDayActive userDayActive = new UserDayActive();
            userDayActive.setId(userID);
            GlobalRoot.db.insertIgnore(userDayActive);
        });
    }
}
