package com.njcn.utils;

import com.njcn.pojo.advanced.EncoderMsg;
import com.njcn.pojo.advanced.WebSocketObj;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ServerEndpoint(value = "/websocket/{userFormal}", encoders = { EncoderMsg.class })
public class WebSocketUtil {
	private Session session;

	@OnOpen
	public void onOpen(@PathParam("userFormal") String userName, Session session) {
		this.session = session;

		Map<String, Session> map = new HashMap<String, Session>();
		map.put(userName, this.session);

		for (int size = 0; size < WebSocketObj.getList().size(); size++) {
			for (String strKey : WebSocketObj.getList().get(size).keySet()) {
				if (strKey.equals(userName)) {
					WebSocketObj.getList().remove(size);
				}
			}
		}

		WebSocketObj.getList().add(map);
		WebSocketObj.setList(WebSocketObj.getList());
	}

	@OnClose
	public void onClose() {
		for (int size = 0; size < WebSocketObj.getList().size(); size++) {
			for (String strKey : WebSocketObj.getList().get(size).keySet()) {
				if (this.session == WebSocketObj.getList().get(size).get(strKey)) {
					WebSocketObj.getList().remove(size);
					WebSocketObj.setList(WebSocketObj.getList());
				}
			}
		}
	}

	@OnError
	public void onError(Throwable t) {
		for (int size = 0; size < WebSocketObj.getList().size(); size++) {
			for (String strKey : WebSocketObj.getList().get(size).keySet()) {
				if (this.session == WebSocketObj.getList().get(size).get(strKey)) {
					WebSocketObj.getList().remove(size);
					WebSocketObj.setList(WebSocketObj.getList());
				}
			}
		}
	}

	public void sendMessage(Session se, String message) throws IOException {
		se.getBasicRemote().sendText(message);
	}

	/**
	* @Author chendaofei
	* @Description 指定用户名发送webSocket
	* @Date 10:15 2020/8/19
	**/
	public synchronized  void sendMessageFor(String message, String To) throws IOException {
		for (Map<String, Session> map : WebSocketObj.getList()) {
			for (String strKey : map.keySet()) {
				if (strKey.equals(To) ) {
					map.get(strKey).getBasicRemote().sendText(message);
				}
			}
		}

	}

	/**
	 * 批量指定用户发送
	 * @author cdf
	 * @date 2021/8/9
	 */
	public void sendMessageForBatch(String message, List<String> userList) throws IOException {
		for (Map<String, Session> map : WebSocketObj.getList()) {
			for (String strKey : map.keySet()) {
				for(String userName:userList) {
					if (strKey.equals(userName)) {
						map.get(strKey).getBasicRemote().sendText(message);
					}
				}
			}
		}
	}

	public void sendMsgObj(Session se, Object obj) throws IOException, EncodeException {
		se.getBasicRemote().sendObject(obj);
	}

	public void sendAll(Object obj) throws IOException, EncodeException {
		for (Map<String, Session> map : WebSocketObj.getList()) {
			for (String strKey : map.keySet()) {
				map.get(strKey).getBasicRemote().sendObject(obj);
			}
		}
	}
}
