package com.inspect.base.websocket.service;

import com.alibaba.fastjson.JSONObject;
import com.inspect.base.websocket.configure.Datawebsocket;
import com.inspect.base.websocket.configure.WsPool;
import com.inspect.base.websocket.domain.WebSocketMessage;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

public class WsServer extends WebSocketServer {
   public WsServer(int port) {
      super(new InetSocketAddress(port));
   }

   public WsServer(InetSocketAddress address) {
      super(address);
   }

   public void onOpen(WebSocket conn, ClientHandshake handshake) {
      System.out.println("--------------------WebSocket已建立连接-------------------------");
   }

   public void onClose(WebSocket conn, int code, String reason, boolean remote) {
      this.userLeave(conn);
      System.out.println(reason);
   }

   public void onMessage(WebSocket conn, String message) {
      System.out.println("----------已接收到客户端发来的消息----------" + message);
      WebSocketMessage webSocketMessage = (WebSocketMessage)JSONObject.parseObject(message, WebSocketMessage.class);
      String username = UUID.randomUUID().toString();
      if ("login".equals(webSocketMessage.getEvent())) {
         System.out.println("----------已接收到客户端发来的注册----------" + message);
         this.userJoin(conn, username);
         WsPool.sendMessageToAll(WebSocketMessage.getInstance("reply", "websocket服务器接收到心跳..."));
      } else if ("heartbeat".equals(webSocketMessage.getEvent())) {
         System.out.println("----------已接收到客户端发来的心跳----------" + message);
         WsPool.sendMessageToAll(WebSocketMessage.getInstance("reply", "websocket服务器接登录成功！"));
      }

   }

   public void onError(WebSocket conn, Exception ex) {
      System.out.println("on error");
      ex.printStackTrace();
   }

   public void onStart() {
   }

   private void userLeave(WebSocket conn) {
      WsPool.removeUser(conn);
   }

   private void userJoin(WebSocket conn, String userName) {
      WsPool.addUser(userName, conn);
   }

   public String jsonJX(String message) {
      Map<String, Object> webSocketMap = new HashMap();
      String userName = "";
      String token = "";
      String event = "";
      JSONObject parseObject = JSONObject.parseObject(message);
      if (parseObject != null) {
         Object json = parseObject.get("event");
         event = json + "";
         Object jsonArray = parseObject.get("data");
         if (jsonArray != null) {
            JSONObject parse = JSONObject.parseObject(jsonArray + "");
            userName = parse.get("userName") + "";
            token = parse.get("token") + "";
         }
      }

      Datawebsocket data = new Datawebsocket();
      data.setUserName(userName);
      data.setToken(token);
      if (event.equals("login")) {
         webSocketMap.put("event", "login");
      } else if (event.equals("heartbeat")) {
         webSocketMap.put("event", "heartbeat");
      }

      webSocketMap.put("data", data);
      JSONObject jsonObject = new JSONObject(webSocketMap);
      String str = jsonObject.toString();
      return str;
   }

   public String jsonUserName(String message) {
      String userName = "";
      JSONObject parseObject = JSONObject.parseObject(message);
      if (parseObject != null) {
         Object json = parseObject.get("event");
         Object jsonArray = parseObject.get("data");
         if (jsonArray != null) {
            JSONObject parse = JSONObject.parseObject(jsonArray + "");
            userName = parse.get("userName") + "";
         }
      }

      return userName;
   }
}
