package com.fs.app.websocket.service;


import com.alibaba.fastjson.JSONObject;
import com.fs.app.websocket.bean.MsgBean;
import com.fs.common.core.domain.R;
import com.fs.common.utils.spring.SpringUtils;
import com.fs.qw.service.IQwUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@ServerEndpoint("/app/webSocketV1")
@Component
public class WebSocketServerAi {

    private static final Logger logger = LoggerFactory.getLogger(WebSocketServerAi.class);
    //concurrent包的线程安全Set，用来存放每个客户端对应的WebSocketServer对象。
    private static ConcurrentHashMap<String, Session> sessionPools = new ConcurrentHashMap<>();

    IQwUserService qwUserService=SpringUtils.getBean(IQwUserService.class);

    //发送消息
    public void sendMessage(Session session, String message) throws IOException {
        if(session != null){
            synchronized (session) {
                session.getBasicRemote().sendText(message);
                System.out.println("发送成功");
            }
        }
    }
    public void sendBaseMessage(Session session, String message) throws IOException {
        if(session != null){
            synchronized (session) {
                session.getBasicRemote().sendText(message);

            }
        }
    }
    public void sendBaseInfo(String id, String message){
        Session session = sessionPools.get(id);
        try {
            if(session != null){
                sendBaseMessage(session, message);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //给指定用户发送信息
    public R sendInfo(String id, String message){

        Session session = sessionPools.get(id);

        try {
            if(session != null){
                sendMessage(session, message);
                System.out.println("发送消息");
                System.out.println(message);
                return R.ok();
            }
            return R.error();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }



    //建立连接成功调用
    @OnOpen
    public void onOpen(Session session) {

        sessionPools.put(session.getId(), session);
        logger.info("logAi链接websocket成功:{}", session.getId());
    }

    //关闭连接时调用
    @OnClose
    public void onClose(Session session){

        sessionPools.remove(session.getId());

    }

    //收到客户端信息
    @OnMessage
    public void onMessage(Session session,String message) throws IOException{
        MsgBean msg=(MsgBean) JSONObject.parseObject(message, MsgBean.class);
        try {
            switch (msg.getCmd()){
                case "heartbeat":
                    break;
                case "sendMsg":
                    break;
                case "login" :
                    break;
                case "logout" :
                    break;
            }
        }
        catch (Exception e){
            System.out.println( "收到消息错误err" + e.getMessage());
        }

    }
    //错误时调用
    @OnError
    public void onError(Session session, Throwable throwable){
        logger.error("发生错误："+session.getId());
    }


    public Set<String> getSessionPools() {
        Set<String> keys = sessionPools.keySet();
        return keys;
    }
}
