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


import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.handler.NettyClientPermeateServerVisitorHandler;
import org.framework.lazy.cloud.network.heartbeat.client.netty.socket.NettyClientPermeateClientRealSocket;
import org.framework.lazy.cloud.network.heartbeat.client.netty.socket.NettyClientTransferServerSocket;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.NettyRealIdContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.wu.framework.spring.utils.SpringContextHolder;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 客户端渗透客户端数据传输通道连接成功
 *
 *
 * @see MessageTypeEnums#DISTRIBUTE_CLIENT_TRANSFER_CLIENT_PERMEATE_CHANNEL_CONNECTION_SUCCESSFUL
 */
@Slf4j
public class ClientHandleDistributeClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced extends AbstractHandleDistributeClientTransferClientPermeateChannelConnectionSuccessfulTypeAdvanced<NettyProxyMsg> {

    /**
     * 处理当前数据
     *
     * @param channel       当前通道
     * @param nettyProxyMsg 通道数据
     */
    @Override
    public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
        // 创建connect 然后发送创建成功
        byte[] msgClientId = nettyProxyMsg.getClientId();
        byte[] msgClientTargetIp = nettyProxyMsg.getClientTargetIp();
        byte[] msgClientTargetPort = nettyProxyMsg.getClientTargetPort();
        byte[] msgVisitorId = nettyProxyMsg.getVisitorId();
        byte[] msgVisitorPort = nettyProxyMsg.getVisitorPort();

        String clientId=new String(msgClientId);
        String clientTargetIp=new String(msgClientTargetIp);
        Integer clientTargetPort=Integer.parseInt(new String(msgClientTargetPort));
        String visitorId=new String(msgVisitorId);
        Integer visitorPort=Integer.parseInt(new String(msgVisitorPort));
        NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);
        List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values().stream().collect(Collectors.toList());
        NettyClientPermeateClientRealSocket.buildRealServer(
                clientId,
                clientTargetIp,
                clientTargetPort,
                visitorPort,
                visitorId,
                nettyClientProperties,
                handleChannelTypeAdvancedList
        );

    }

}
