package com.its.pis.xnettcp.vds;

import com.its.app.utils.NettyUtils;
import com.its.pis.xnettcp.vds.process.VdsDataProcess;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
public class VdsTcpClientBootstrapFactory {

    private final int workerThread;
    private final int connectTimeout;
    private final boolean isLogging;
    private final VdsDataProcess vdsDataProcess;
    private EventLoopGroup nioEventLoopGroup = null;

    public Bootstrap createBootstrap() {
        if (this.nioEventLoopGroup == null) {
            this.nioEventLoopGroup = NettyUtils.newEventLoopGroup(this.workerThread, "vdsEventGroup");//new NioEventLoopGroup(this.workerThread);  //EpollEventLoopGroup
        }
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(this.nioEventLoopGroup);

        bootstrap.channel(NioSocketChannel.class);
        bootstrap.option(ChannelOption.AUTO_READ, true);
        bootstrap.option(ChannelOption.TCP_NODELAY, true);
        bootstrap.option(ChannelOption.SO_RCVBUF, 8192);
        bootstrap.option(ChannelOption.SO_SNDBUF, 8192);
        bootstrap.option(ChannelOption.SO_KEEPALIVE, false);
        //bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(2048));
        bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.connectTimeout * 1000);
//        bootstrap.handler(new ChannelInitializer<SocketChannel>() {
//            // VDS 전송 요청을 먼저하고 데이터를 수신한다.
//            // 핸들러가 실행되는 순서는 추가된 순서에 의해 결정된다.(Inbound: head=>tail, Outbound: tail=>head, name2ctx)
//            @Override
//            public void initChannel(SocketChannel ch) {
//                if (isLogging) {
//                    //io.netty.handler.logging.LoggingHandler
//                    //ch.pipeline().addLast(new LoggingHandler(LogLevel.WARN));
//                }
//                ch.pipeline().addLast(new LoggingHandler(LogLevel.WARN));
//                //ch.pipeline().addLast("vdsClientIdleHandler",    new VdsTcpClientIdleHandler(0, 0, 0, TimeUnit.SECONDS));
//                ch.pipeline().addLast("vdsClientDecoder",        new VdsTcpClientDecoder());            // Decoding handler
//                ch.pipeline().addLast("vdsClientInboundHandler", new VdsTcpClientInboundHandler(vdsDataProcess));     // Packet Inbound handler
//                ch.pipeline().addLast("vdsClientEncoder",        new VdsTcpClientEncoder());            // Encoding handler
//            }
//        });

        return bootstrap;
    }

    public EventLoopGroup getEventLoopGroup() {
        return this.nioEventLoopGroup;
    }
    public void addChannelFuture(ChannelFuture future) {
        //this.channelFutures.add(future);
    }
}
