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.NettyClientPermeateClientVisitorSocket;
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.AbstractHandleDistributeClientPermeateClientInitTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.wu.framework.spring.utils.SpringContextHolder;

import java.util.ArrayList;
import java.util.List;


/**
 * 客户端渗透客户端init信息
 *
 * @see MessageTypeEnums#DISTRIBUTE_CLIENT_PERMEATE_CLIENT_INIT
 */
@Slf4j
public class ClientHandleDistributeClientPermeateClientInitTypeAdvanced extends AbstractHandleDistributeClientPermeateClientInitTypeAdvanced<NettyProxyMsg> {

    /**
     * 处理当前数据
     *
     * @param channel       当前通道
     * @param nettyProxyMsg 通道数据
     */
    @Override
    public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
        // 初始化 客户端渗透服务端socket
        byte[] fromClientIdBytes = nettyProxyMsg.getClientId();
        byte[] visitorPortBytes = nettyProxyMsg.getVisitorPort();
        byte[] clientTargetIpBytes = nettyProxyMsg.getClientTargetIp();
        byte[] clientTargetPortBytes = nettyProxyMsg.getClientTargetPort();
        byte[] toClientIdBytes = nettyProxyMsg.getData();

        String fromClientId = new String(fromClientIdBytes);
        String toClientId = new String(toClientIdBytes);
        Integer visitorPort = Integer.parseInt(new String(visitorPortBytes));
        String clientTargetIp = new String(clientTargetIpBytes);
        Integer clientTargetPort = Integer.parseInt(new String(clientTargetPortBytes));

        List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
//        ChannelFlowAdapter channelFlowAdapter = SpringContextHolder.getBean(ChannelFlowAdapter.class);
        NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);
        log.info("client permeate client from client_id:【{}】 to_client_id【{}】with visitor_port【{}】, clientTargetIp:【{}】clientTargetPort:【{}】",
                fromClientId, toClientId, visitorPort, clientTargetIp, clientTargetPort);
        NettyClientPermeateClientVisitorSocket nettyClientPermeateClientVisitorSocket =
                NettyClientPermeateClientVisitorSocket.NettyClientPermeateClientVisitorSocketBuilder.builder()
                        .builderClientId(fromClientId)
                        .builderClientTargetIp(clientTargetIp)
                        .builderClientTargetPort(clientTargetPort)
                        .builderVisitorPort(visitorPort)
                .builderNettyClientProperties(nettyClientProperties)
//                .builderChannelFlowAdapter(channelFlowAdapter)
                        .builderToClientId(toClientId)
                .builderHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList)

                .build();
        try {
            nettyClientPermeateClientVisitorSocket.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
