/*
 * The MIT License
 *
 * Copyright 2017 Fernando van Loenhout.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package me.ferrybig.javacoding.teamspeakconnector;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.Promise;
import java.net.SocketAddress;
import me.ferrybig.javacoding.teamspeakconnector.internal.TeamspeakConnectionInitizer;
import me.ferrybig.javacoding.teamspeakconnector.util.FutureUtil;

public class TeamspeakApi {

	private final EventLoopGroup group;

	public TeamspeakApi(EventLoopGroup group) {
		this.group = group;
	}

	public Future<TeamspeakConnection> connect(SocketAddress addr) {
		Promise<TeamspeakConnection> prom = this.group.next().newPromise();
		ChannelFuture channel = openChannel(addr, new TeamspeakConnectionInitizer(prom, 20000), 20000);
		channel.addListener(future -> {
			if (!channel.isSuccess()) {
				prom.setFailure(new TeamspeakException("Connection failed", channel.cause()));
			}
		});
		return prom;
	}

	public Future<TeamspeakConnection> connect(SocketAddress addr, String username, String password) {
		final Future<TeamspeakConnection> connectFuture = connect(addr);
		final Future<TeamspeakConnection> result = FutureUtil.chainFutureFlat(this.group.next().newPromise(), connectFuture, con -> con.login(username, password));
		result.addListener(f -> {
			if (!f.isSuccess()) {

			}
		});
		return result;
	}

	private ChannelFuture openChannel(SocketAddress addr, ChannelInitializer<? extends SocketChannel> ch, int timneout) {
		Bootstrap bootstrap = new Bootstrap();
		bootstrap.channel(NioSocketChannel.class);
		bootstrap.remoteAddress(addr);
		bootstrap.handler(ch);
		bootstrap.group(group);
		bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
		bootstrap.option(ChannelOption.TCP_NODELAY, true); // We send relatively small packets, we benefit more from delayed ack
		return bootstrap.connect();
	}

}

@@ -1758,145 +1758,407 @@
 l;%0A%0A
-public class TeamspeakApi %7B%0A%0A%09private final EventLoopGroup group;%0A%0A%09public TeamspeakApi(EventLoopGroup group) %7B%0A%09%09this.group = group;%0A%09%7D%0A
+/**%0A *%0A * @deprecated Use TeamspeakBootstrap instead%0A */%0A@Deprecated%0Apublic class TeamspeakApi %7B%0A%0A%09private final EventLoopGroup group;%0A%0A%09/**%0A%09 *%0A%09 * @param group%0A%09 * @deprecated Use TeamspeakBootstrap instead%0A%09 */%0A%09@Deprecated%0A%09public TeamspeakApi(EventLoopGroup group) %7B%0A%09%09this.group = group;%0A%09%7D%0A%0A%09/**%0A%09 *%0A%09 * @param addr%0A%09 * @return%0A%09 * @deprecated Use TeamspeakBootstrap instead%0A%09 */%0A%09@Deprecated
 %0A%09pu
@@ -2566,16 +2566,158 @@
 om;%0A%09%7D%0A%0A
+%09/**%0A%09 *%0A%09 * @param addr%0A%09 * @param username%0A%09 * @param password%0A%09 * @return%0A%09 * @deprecated Use TeamspeakBootstrap instead%0A%09 */%0A%09@Deprecated%0A
 %09public 
@@ -3119,16 +3119,152 @@
 lt;%0A%09%7D%0A%0A
+%09/**%0A%09 *%0A%09 * @param addr%0A%09 * @param ch%0A%09 * @param timneout%0A%09 * @return%0A%09 * @deprecated Use TeamspeakBootstrap instead%0A%09 */%0A%09@Deprecated%0A
 %09private

Use deprecation