package org.framework.lazy.cloud.network.heartbeat.client.netty.advanced;

import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.socket.NettyServerPermeateClientRealSocket;
import org.framework.lazy.cloud.network.heartbeat.common.InternalNetworkPenetrationRealClient;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeServicePermeateClientRealConnectTypeAdvanced;

import java.util.List;

/**
 * 客户端创建真实代理同奥
 */
@Slf4j
public class ClientHandleDistributeServicePermeateClientRealConnectTypeAdvanced extends AbstractHandleDistributeServicePermeateClientRealConnectTypeAdvanced<NettyProxyMsg> {

    private final NettyClientProperties nettyClientProperties;// 服务端地址信息
    private final List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList;

    public ClientHandleDistributeServicePermeateClientRealConnectTypeAdvanced(NettyClientProperties nettyClientProperties, List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
        this.nettyClientProperties = nettyClientProperties;
        this.handleChannelTypeAdvancedList = handleChannelTypeAdvancedList;
    }

    /**
     * 处理当前数据
     *
     * @param channel 当前通道
     * @param msg     通道数据
     */
    @Override
    protected void doHandler(Channel channel, NettyProxyMsg msg) {
        // 创建真实端口监听
        byte[] clientIdBytes = msg.getClientId();
        byte[] visitorPort = msg.getVisitorPort();
        byte[] clientTargetIp = msg.getClientTargetIp();
        byte[] clientTargetPort = msg.getClientTargetPort();
        byte[] visitorIdBytes = msg.getVisitorId();
        InternalNetworkPenetrationRealClient internalNetworkPenetrationRealClient =
                InternalNetworkPenetrationRealClient
                        .builder()
                        .clientId(new String(clientIdBytes))
                        .visitorPort(Integer.valueOf(new String(visitorPort)))
                        .clientTargetIp(new String(clientTargetIp))
                        .clientTargetPort(Integer.valueOf(new String(clientTargetPort)))
                        .visitorId(new String(visitorIdBytes))
                        .build();

        // 绑定真实服务端口
        NettyServerPermeateClientRealSocket.buildRealServer(internalNetworkPenetrationRealClient, nettyClientProperties, handleChannelTypeAdvancedList);

    }
}
