package com.sy.coinage.websocket;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by liuhong on 2021/7/22.
 *
 */
@Slf4j
@ServerEndpoint(value = "/ware/{pageId}")
@Component
public class WareWebSocketServer {

    //todo 后续添加造币一部、造币三部


    //  pageId 使用仓库编号

    /**
     *  造币二部
     */
    public static final String  SHOP_TWO_PAGE_ID  = "102";

    /**
     *  总库一
     */
    public static final String  MAIN_ONE_PAGE_ID = "201";

    /**
     * 总库二
     */
    public  static  final String MAIN_TWO_PAGE_ID =  "202";



    /**
     * 保存 sessionId,pageId的绑定关系
     */
    private static Map<String,String>  clients = new ConcurrentHashMap<>();

    /**
     * 造币车间二，页面session
     */
    private static Map<String,Session>  clientShopTwo = new ConcurrentHashMap<>();

    /**
     * 总库一 页面session
     */
    private static Map<String,Session>  clientMainOne = new ConcurrentHashMap<>();

    /**
     * 总库二页面session
     */
    private static Map<String,Session>  clientMainTwo = new ConcurrentHashMap<>();



    private Session session;

    @OnOpen
    public void onOpen(Session session, @PathParam("pageId")String  pageId) {

        this.session = session;

        log.info(" client is open , pageId is :{}",pageId);

        switch (pageId){

            case SHOP_TWO_PAGE_ID:
                clientShopTwo.put(session.getId(), session);
                clients.put(session.getId(),pageId);
                break;
            case MAIN_ONE_PAGE_ID:
                clientMainOne.put(session.getId(), session);
                clients.put(session.getId(),pageId);
                break;
            case MAIN_TWO_PAGE_ID:
                clientMainTwo.put(session.getId(), session);
                clients.put(session.getId(),pageId);

        }
    }


    @OnClose
    public void onClose() {
         String sessionId = this.session.getId();
        String pageId = clients.get(sessionId);

        switch (pageId){

            case SHOP_TWO_PAGE_ID:
                clientShopTwo.remove(sessionId);
                break;
            case MAIN_ONE_PAGE_ID:
                clientMainOne.remove(sessionId);
                break;
            case MAIN_TWO_PAGE_ID:
                clientMainTwo.remove(sessionId);


        }
        clients.remove(sessionId);
    }


    /**
     *  发送事件消息到指定页面
     * @param message  事件消息实体
     * @param pageId : 页面ID
     */
    public static void sendWsMessage(Object message ,String pageId) {

        Map<String,Session>  temp = queryClient(pageId);

        for (String sessionId : temp.keySet())
            ((Session)temp.get(sessionId)).getAsyncRemote().sendObject(JSONObject.toJSONString(message));
    }


    private static Map<String,Session>  queryClient(String pageId){

        switch (pageId){

            case SHOP_TWO_PAGE_ID:
                return clientShopTwo;

            case MAIN_ONE_PAGE_ID:
                return clientMainOne;
            case MAIN_TWO_PAGE_ID:
                return clientMainTwo;

        }
        return null;
    }

}
