/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you 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 io.github.satoshinm.WebSandboxMC.ws;

import java.util.List;
import java.util.Locale;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;

/**
 * Echoes uppercase content of text frames.
 */
public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {

    private final WebSocketServerThread webSocketServerThread;

    public WebSocketFrameHandler(WebSocketServerThread webSocketServerThread) {
        this.webSocketServerThread = webSocketServerThread;
    }

    @Override
    @SuppressWarnings("deprecation") // TODO: why is HANDSHAKE_COMPLETE deprecated and what is the replacement?
    public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception {
        System.out.println("userEventTriggered: "+evt);
        if (evt == WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) {
            // "The Handshake was complete successful and so the channel was upgraded to websockets"


            // Since we're in a callback we cannot call any Bukkit API safely here, see:
            // http://bukkit.gamepedia.com/Scheduler_Programming#Tips_for_thread_safety
            // " Warning:	Asynchronous tasks should never access any API in Bukkit"
            webSocketServerThread.scheduleSyncTask(new Runnable() {
                @Override
                public void run() {
                    webSocketServerThread.handleNewClient(ctx);
                }
            });
        }
    }

    @Override
    public void channelRead0(final ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
        //System.out.println("channel read, obj="+obj);

        if (frame instanceof TextWebSocketFrame) {
            // TODO: remove text frames, not used
            // Send the uppercase string back.
            String request = ((TextWebSocketFrame) frame).text();
            System.out.println("channel " + ctx.channel() + " received text request " + request);
            ctx.channel().writeAndFlush(new TextWebSocketFrame(request.toUpperCase(Locale.US)));
        } else if (frame instanceof BinaryWebSocketFrame) {
            ByteBuf content = ((BinaryWebSocketFrame) frame).content();

            byte[] bytes = new byte[content.capacity()];
            content.getBytes(0, bytes);

            final String string = new String(bytes);
            //System.out.println("received "+content.capacity()+" bytes: "+string);

            this.webSocketServerThread.scheduleSyncTask(new Runnable() {
                @Override
                public void run() {
                    webSocketServerThread.handle(string, ctx);
                }
            });
        } else {
            String message = "unsupported frame type: " + frame.getClass().getName();
            throw new UnsupportedOperationException(message);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}

@@ -685,113 +685,31 @@
 ort 
-java.util.List;%0Aimport java.util.Locale;%0A%0Aimport io.netty.buffer.ByteBuf;%0Aimport io.netty.buffer.Unpooled
+io.netty.buffer.ByteBuf
 ;%0Aim
@@ -805,74 +805,8 @@
 me;%0A
-import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;%0A
 impo
@@ -945,170 +945,8 @@
 er;%0A
-import org.bukkit.Bukkit;%0Aimport org.bukkit.Material;%0Aimport org.bukkit.World;%0Aimport org.bukkit.block.Block;%0A%0A/**%0A * Echoes uppercase content of text frames.%0A */
 %0Apub
@@ -2438,424 +2438,8 @@
 eof 
-TextWebSocketFrame) %7B%0A            // TODO: remove text frames, not used%0A            // Send the uppercase string back.%0A            String request = ((TextWebSocketFrame) frame).text();%0A            System.out.println(%22channel %22 + ctx.channel() + %22 received text request %22 + request);%0A            ctx.channel().writeAndFlush(new TextWebSocketFrame(request.toUpperCase(Locale.US)));%0A        %7D else if (frame instanceof 
 Bina
@@ -2491,39 +2491,14 @@
 nt =
- ((BinaryWebSocketFrame)
  frame
-)
 .con

Remove unused TextWebSocketFrame, we're all binary