/*
 * Copyright 2015-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.intellij.ideabuck.ws;

import com.facebook.buck.intellij.ideabuck.ws.buckevents.BuckEventsHandlerInterface;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;

import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

// Set the maximum message text size to 24 MB
@WebSocket(maxTextMessageSize = 24 * 1024 * 1024)
public class BuckSocket {
  private AtomicBoolean connected;
  private AtomicLong lastActionTime;
  private BuckEventsHandlerInterface eventsHandler;

  @SuppressWarnings("unused")
  private Session session;

  public BuckSocket(BuckEventsHandlerInterface eventsHandler) {
    this.connected = new AtomicBoolean(false);
    this.lastActionTime = new AtomicLong(0);
    this.eventsHandler = eventsHandler;
  }

  public boolean isConnected() {
    return connected.get();
  }

  public long getTimeSinceLastAction() {
    return new Date().getTime() - lastActionTime.get();
  }

  @OnWebSocketClose
  public void onClose(int statusCode, String reason) {
    connected.set(false);
    this.session = null;
    eventsHandler.onDisconnect();
  }

  @OnWebSocketConnect
  public void onConnect(Session session) {
    connected.set(true);
    this.session = session;
    eventsHandler.onConnect();
  }

  @OnWebSocketMessage
  public void onMessage(String msg) {
    lastActionTime.set(new Date().getTime());
    eventsHandler.onMessage(msg);
  }

  public void sendMessage(String msg)
      throws InterruptedException, ExecutionException, TimeoutException {
    lastActionTime.set(new Date().getTime());
    Future<Void> fut;
    fut = session.getRemote().sendStringByFuture(msg);
    fut.get(1, TimeUnit.SECONDS);
  }
}

@@ -1077,277 +1077,39 @@
 til.
-Date;%0Aimport java.util.concurrent.ExecutionException;%0Aimport java.util.concurrent.Future;%0Aimport java.util.concurrent.TimeUnit;%0Aimport java.util.concurrent.TimeoutException;%0Aimport java.util.concurrent.atomic.AtomicBoolean;%0Aimport java.util.concurrent.atomic.AtomicLong
+concurrent.atomic.AtomicBoolean
 ;%0A%0A/
@@ -1268,45 +1268,8 @@
 ed;%0A
-  private AtomicLong lastActionTime;%0A
   pr
@@ -1490,53 +1490,8 @@
 e);%0A
-    this.lastActionTime = new AtomicLong(0);%0A
     
@@ -1601,110 +1601,8 @@
  %7D%0A%0A
-  public long getTimeSinceLastAction() %7B%0A    return new Date().getTime() - lastActionTime.get();%0A  %7D%0A%0A
   @O
@@ -1984,355 +1984,35 @@
     
-lastActionTime.set(new Date().getTime());%0A    eventsHandler.onMessage(msg);%0A  %7D%0A%0A  public void sendMessage(String msg)%0A      throws InterruptedException, ExecutionException, TimeoutException %7B%0A    lastActionTime.set(new Date().getTime());%0A    Future%3CVoid%3E fut;%0A    fut = session.getRemote().sendStringByFuture(msg);%0A    fut.get(1, TimeUnit.SECONDS
+eventsHandler.onMessage(msg
 );%0A 

Remove dead code from BuckSocket