package com.yanzhenjie.andserver.sample.websocket;

import com.alibaba.fastjson.JSONObject;
import com.yanzhenjie.andserver.http.session.Session;


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

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;

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

public class SocketServer extends WebSocketServer {

    private static  ConcurrentHashMap<String,WebSocket> webSocketMap = new ConcurrentHashMap<>();
//    private  static SocketServer instance = null;
    public  SocketServer(InetSocketAddress address) {
        super(address);
    }

//    public static  SocketServer getInstance (String address,int port) {
//        synchronized (SocketServer.class) {
//            if(instance == null) {
//                instance = new SocketServer(new InetSocketAddress(address, port));
//            }
//
//            return instance;
//        }
//    }

    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        conn.send("Welcome to the server!"); //This method sends a message to the new client
        webSocketMap.put(handshake.getResourceDescriptor(), conn);
        System.out.println("open----socketMap.size="+webSocketMap.size());
        broadcast( "new connection: " + handshake.getResourceDescriptor() ); //This method sends a message to all clients connected
        System.out.println("new connection to " + conn.getRemoteSocketAddress());
    }

    @Override
    public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        System.out.println("closed " + conn.getRemoteSocketAddress() + " with exit code " + code + " additional info: " + reason);
    }

    @Override
    public void onMessage(WebSocket conn, String message) {
        System.out.println("received message from "	+ conn.getRemoteSocketAddress() + ": " + message);
    }

    @Override
    public void onMessage( WebSocket conn, ByteBuffer message ) {
        System.out.println("received ByteBuffer from "	+ conn.getRemoteSocketAddress());
    }

    @Override
    public void onError(WebSocket conn, Exception ex) {
        System.err.println("an error occurred on connection " + conn.getRemoteSocketAddress()  + ":" + ex);
    }

    @Override
    public void onStart() {
        System.out.println("server started successfully");
    }

    public  void sendMessage(){
        System.out.println("send Message invoked : socketMap.size="+ webSocketMap.size());
    }



/*    public static void main(String[] args) {

        String host = "localhost";
        int port = 8887;
        WebSocketServer server = getInstance(host, port);
        TimerTask timerTask = new TimerTask() {

            @Override
            public void run() {
                System.out.println("--------------------"+webSocketMap.size());
            }
        };

        Timer timer = new Timer();
        timer.schedule(timerTask, 1000, 1000);
        server.run();
        System.out.println("------------------------------");
    }*/
}