
package com.tmerclub.cloud.im.socket.platform;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.api.auth.bo.UidInfoBO;

import com.tmerclub.cloud.api.auth.feign.TokenFeignClient;
import com.tmerclub.cloud.common.cache.util.RedisUtil;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.common.util.MapUtil;
import com.tmerclub.cloud.im.constants.Constant;
import com.tmerclub.cloud.im.constants.MsgCode;
import com.tmerclub.cloud.im.constants.SendType;
import com.tmerclub.cloud.im.socket.app.UserWebSocketServer;
import com.tmerclub.cloud.im.vo.MessageInfoVO;
import com.tmerclub.cloud.im.vo.MsgItemVO;
import jakarta.websocket.*;
import jakarta.websocket.server.PathParam;
import jakarta.websocket.server.ServerEndpoint;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Lzk
 */
@ServerEndpoint("/p/ua/im/websocket/online/{token}/{userId}")
@Component
public class SysOnlineWebSocketServer {

    /**
     * userId为key
     */
    public static final ConcurrentHashMap<String, Session> SYS_ONLINE_MAP = new ConcurrentHashMap<>(256);

    private static TokenFeignClient tokenFeignClient;

    public static void setApplicationContext(ApplicationContext applicationContext) {
        tokenFeignClient = applicationContext.getBean(TokenFeignClient.class);
    }

    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session, @PathParam("token") String token) throws IOException {
        token = Base64.decodeStr(token);
        UidInfoBO uidInfoBO = tokenFeignClient.checkToken(token);
        if (uidInfoBO == null) {
            if (session.isOpen()) {
                session.getBasicRemote().sendText(Json.toJsonString(ServerResponseEntity.fail(MsgCode.CAN_NOT_FIND_USER_INFO, "无法获取用户信息")));
            }
            return;
        }

        // 如果原来就有token
        Session oldSession = SYS_ONLINE_MAP.get(Constant.ONLINE_SYS_PREFIX + uidInfoBO.getUserId() + Constant.UNDERLINE + token);
        if (oldSession != null) {
            // 客户端收到11，关闭ws链接，弹出提示
            if (oldSession.isOpen()) {
                oldSession.getBasicRemote().sendText(Json.toJsonString(ServerResponseEntity.fail(MsgCode.LOGIN_IN_OTHER_PLACE, "账户已在别处登录，请刷新")));
                oldSession.close();
            }
        }

        Boolean isExist = MapUtil.isExistKeyPrefix(SYS_ONLINE_MAP, Constant.ONLINE_SYS_PREFIX);
        if (!isExist) {
            Set<String> set2 = UserWebSocketServer.USER_MAP.keySet();
            if (CollUtil.isNotEmpty(set2)) {
                MsgItemVO msgItem = new MsgItemVO();
                msgItem.setChatType(SendType.PLATFORM);
                msgItem.setShopId(uidInfoBO.getUserId());
                msgItem.setOnlineOrOffline(true);
                for (String key : set2) {
                    Session userSession = UserWebSocketServer.USER_MAP.get(key);
                    if (Objects.nonNull(userSession)) {
                        if (userSession.isOpen()) {
                            userSession.getBasicRemote().sendText(Json.toJsonString(ServerResponseEntity.success(MsgCode.ONLINE_OR_OFFLINE, msgItem)));
                        }
                    }
                }
            }
        }

        SYS_ONLINE_MAP.put(Constant.ONLINE_SYS_PREFIX + uidInfoBO.getUserId() + Constant.UNDERLINE + token, session);
        if (session.isOpen()) {
            session.getBasicRemote().sendText(Json.toJsonString(ServerResponseEntity.success()));
        }
        Integer unRead = RedisUtil.get(Constant.UNREAD_SYS_PREFIX + uidInfoBO.getUserId() + Constant.UNDERLINE);
        if (Objects.nonNull(unRead) && 1 == unRead) {
            MsgItemVO msgItem = new MsgItemVO();
            msgItem.setNewMessage(true);
            session.getBasicRemote().sendText(Json.toJsonString(ServerResponseEntity.success(msgItem)));
            RedisUtil.del(Constant.UNREAD_SYS_PREFIX + uidInfoBO.getUserId() + Constant.UNDERLINE);
        }
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose(Session session, @PathParam("token") String token, @PathParam("userId") Long userId) throws IOException {
        SYS_ONLINE_MAP.remove(Constant.ONLINE_SYS_PREFIX + userId + Constant.UNDERLINE + token);

        Boolean isExist = MapUtil.isExistKeyPrefix(SYS_ONLINE_MAP, Constant.ONLINE_SYS_PREFIX);
        if (!isExist) {
            Set<String> set2 = UserWebSocketServer.USER_MAP.keySet();
            if (CollUtil.isNotEmpty(set2)) {
                MsgItemVO msgItem = new MsgItemVO();
                msgItem.setShopId(userId);
                msgItem.setChatType(SendType.PLATFORM);
                msgItem.setOnlineOrOffline(false);
                sendOfflineMsg(set2, msgItem);
            }
        }
    }

    private static void sendOfflineMsg(Set<String> set2, MsgItemVO msgItem) throws IOException {
        for (String key : set2) {
            Session userSession = UserWebSocketServer.USER_MAP.get(key);
            if (userSession != null) {
                if (userSession.isOpen()) {
                    userSession.getBasicRemote().sendText(Json.toJsonString(ServerResponseEntity.success(MsgCode.ONLINE_OR_OFFLINE, msgItem)));
                }
            }
        }
    }

    /**
     * 连接错误调用的方法
     */
    @OnError
    public void onError(Session session, @PathParam("token") String token, Throwable error, @PathParam("userId") Long userId) throws IOException {
        SYS_ONLINE_MAP.remove(Constant.ONLINE_SYS_PREFIX + userId + Constant.UNDERLINE + token);

        Boolean isExist = MapUtil.isExistKeyPrefix(SYS_ONLINE_MAP, Constant.ONLINE_SYS_PREFIX);
        if (!isExist) {
            Set<String> set2 = UserWebSocketServer.USER_MAP.keySet();
            if (CollUtil.isNotEmpty(set2)) {
                MsgItemVO msgItem = new MsgItemVO();
                msgItem.setShopId(userId);
                msgItem.setOnlineOrOffline(false);
                sendOfflineMsg(set2, msgItem);
            }
        }
    }

    /**
     * 收到客户端消息后调用的方法，该消息由用户发过来
     *
     * @param sourceMessage 客户端发送过来的消息
     */
    @OnMessage
    public void onMessage(String sourceMessage, @PathParam("token") String token, Session session) throws IOException {
        token = Base64.decodeStr(token);
        UidInfoBO uidInfoBO = tokenFeignClient.checkToken(token);
        if (Objects.isNull(uidInfoBO)) {
            if (session.isOpen()) {
                session.getBasicRemote().sendText(Json.toJsonString(ServerResponseEntity.fail(MsgCode.CAN_NOT_FIND_USER_INFO, "无法获取平台用户信息")));
            }
            return;
        }

        if (StrUtil.isBlank(sourceMessage)) {
            if (session.isOpen()) {
                session.getBasicRemote().sendText(Json.toJsonString(ServerResponseEntity.fail(MsgCode.CONTENT_FORMAT_ERROR, "平台消息内容格式有误")));
            }
            return;
        }

        MessageInfoVO messageInfo = Json.parseObject(sourceMessage, MessageInfoVO.class);

        if (Objects.isNull(messageInfo)) {
            if (session.isOpen()) {
                session.getBasicRemote().sendText(Json.toJsonString(ServerResponseEntity.fail(MsgCode.CONTENT_FORMAT_ERROR, "平台消息内容格式有误")));
            }
            return;
        }

        // 保持心跳
        if (StrUtil.equals(messageInfo.getContent(), Constant.HEART_BEAT)) {
            if (session.isOpen()) {
                session.getBasicRemote().sendText(Json.toJsonString(ServerResponseEntity.success(MsgCode.HEART_BEAT, "保持心跳")));
            }
        }

    }

}
