package com.lovecoding.websocket;


import com.alibaba.fastjson2.JSON;
import org.springframework.stereotype.Component;

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

@ServerEndpoint("/game")
@Component
public class GameController {

    @OnOpen
    public void onOpen(Session session){
        System.out.println( "新增用户" );
        GameTools.add(session);
        System.out.println( "当前共有用户： " + GameTools.get().size() );
        HashMap<String, String> message = new HashMap<String, String>();
        message.put("type", "welcome");
        message.put("id", session.getId());
        sendUser( JSON.toJSONString(message), session );
    }

    @OnMessage
    public void onMsg( String msg, Session session ){
        HashMap<String, String> message = (HashMap<String, String>) JSON.parse(msg);

        switch ( message.get("type") ) {
            case "login":
                break;
            case "update":
                HashMap<Object, Object> updateMsg = new HashMap<>();
                updateMsg.put("type", "update");
                updateMsg.put("id", session.getId());
                updateMsg.put("angle", Double.valueOf(message.get("angle")) );
                updateMsg.put("momentum", Double.valueOf(message.get("momentum")) );
                updateMsg.put("x", Double.valueOf(message.get("x")) );
                updateMsg.put("y", Double.valueOf(message.get("y")) );
                updateMsg.put("life", 1 );
                updateMsg.put("name", message.containsValue("name") ? message.get("name") : "Guest" + session.getId() );
                updateMsg.put("authorized", false );
                sendAll(JSON.toJSONString( updateMsg ));
                break;
            case "message":
                HashMap<String, Object> newMessage = new HashMap<>();
                newMessage.put("type", "message");
                newMessage.put("id", session.getId());
                newMessage.put("message", message.get("message"));
                sendAll( JSON.toJSONString( newMessage ) );
                break;
        }
    }

    @OnError
    public void onError( Throwable throwable  ){
        System.out.println( "发生异常" );
        System.out.println( throwable );
    }

    @OnClose
    public void onClose( Session session ){
        System.out.println( "用户掉线了" );
        GameTools.del(session);

        HashMap<Object, Object> msg = new HashMap<>();
        msg.put("type", "closed");
        msg.put("id", session.getId());
        sendAll( JSON.toJSONString(msg) );
    }

    public void sendUser( String msg, Session session ){
        try {
            synchronized (session.getId()) {
                session.getBasicRemote().sendText(msg);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendAll( String msg ){
        ConcurrentHashMap<String, Session> concurrentHashMap = GameTools.get();
        for ( Session session: concurrentHashMap.values() ) {
            try {
                synchronized(session.getId()) {
                    session.getBasicRemote().sendText(msg);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
