package org.framework.lazy.cloud.network.heartbeat.server.standalone.application.impl;

import io.netty.channel.Channel;
import jakarta.annotation.Resource;
import org.framework.lazy.cloud.network.heartbeat.common.ChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.MessageType;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.LazyInternalNetworkClientPermeateClientMappingApplication;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.assembler.LazyInternalNetworkClientPermeateClientMappingDTOAssembler;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.permeate.client.mapping.*;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyInternalNetworkClientPermeateClientMappingDTO;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.permeate.client.mapping.LazyInternalNetworkClientPermeateClientMapping;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.permeate.client.mapping.LazyInternalNetworkClientPermeateClientMappingRepository;
import org.wu.framework.database.lazy.web.plus.stereotype.LazyApplication;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
import org.wu.framework.web.response.Result;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;
/**
 * describe 客户端渗透客户端映射 
 *
 * @author Jia wei Wu
 * @date 2024/09/17 09:55 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl 
 **/
@LazyApplication
public class LazyInternalNetworkClientPermeateClientMappingApplicationImpl implements LazyInternalNetworkClientPermeateClientMappingApplication {

    @Resource
    LazyInternalNetworkClientPermeateClientMappingRepository lazyInternalNetworkClientPermeateClientMappingRepository;
    /**
     * describe 新增客户端渗透客户端映射
     *
     * @param lazyInternalNetworkClientPermeateClientMappingStoryCommand 新增客户端渗透客户端映射     
     * @return {@link Result<LazyInternalNetworkClientPermeateClientMapping>} 客户端渗透客户端映射新增后领域对象     
     
     * @author Jia wei Wu
     * @date 2024/09/17 09:55 晚上
     **/

    @Override
    public Result<LazyInternalNetworkClientPermeateClientMapping> story(LazyInternalNetworkClientPermeateClientMappingStoryCommand lazyInternalNetworkClientPermeateClientMappingStoryCommand) {
        LazyInternalNetworkClientPermeateClientMapping lazyInternalNetworkClientPermeateClientMapping = LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateClientMapping(lazyInternalNetworkClientPermeateClientMappingStoryCommand);
       // 下发客户端渗透客户端请求
        createClientPermeateClientSocketMessage(lazyInternalNetworkClientPermeateClientMapping);
        
        return lazyInternalNetworkClientPermeateClientMappingRepository.story(lazyInternalNetworkClientPermeateClientMapping);
    }
    /**
     * describe 批量新增客户端渗透客户端映射
     *
     * @param lazyInternalNetworkClientPermeateClientMappingStoryCommandList 批量新增客户端渗透客户端映射     
     * @return {@link Result<List<LazyInternalNetworkClientPermeateClientMapping>>} 客户端渗透客户端映射新增后领域对象集合     
     
     * @author Jia wei Wu
     * @date 2024/09/17 09:55 晚上
     **/

    @Override
    public Result<List<LazyInternalNetworkClientPermeateClientMapping>> batchStory(List<LazyInternalNetworkClientPermeateClientMappingStoryCommand> lazyInternalNetworkClientPermeateClientMappingStoryCommandList) {
        List<LazyInternalNetworkClientPermeateClientMapping> lazyInternalNetworkClientPermeateClientMappingList = lazyInternalNetworkClientPermeateClientMappingStoryCommandList.stream().map( LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE::toLazyInternalNetworkClientPermeateClientMapping).collect(Collectors.toList());
        for (LazyInternalNetworkClientPermeateClientMapping lazyInternalNetworkClientPermeateClientMapping : lazyInternalNetworkClientPermeateClientMappingList) {
            createClientPermeateClientSocketMessage(lazyInternalNetworkClientPermeateClientMapping);
        }
        return lazyInternalNetworkClientPermeateClientMappingRepository.batchStory(lazyInternalNetworkClientPermeateClientMappingList);
    }
    /**
     * describe 更新客户端渗透客户端映射
     *
     * @param lazyInternalNetworkClientPermeateClientMappingUpdateCommand 更新客户端渗透客户端映射     
     * @return {@link Result<LazyInternalNetworkClientPermeateClientMapping>} 客户端渗透客户端映射领域对象     
     
     * @author Jia wei Wu
     * @date 2024/09/17 09:55 晚上
     **/

    @Override
    public Result<LazyInternalNetworkClientPermeateClientMapping> updateOne(LazyInternalNetworkClientPermeateClientMappingUpdateCommand lazyInternalNetworkClientPermeateClientMappingUpdateCommand) {
        LazyInternalNetworkClientPermeateClientMapping lazyInternalNetworkClientPermeateClientMapping = LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateClientMapping(lazyInternalNetworkClientPermeateClientMappingUpdateCommand);
        createClientPermeateClientSocketMessage(lazyInternalNetworkClientPermeateClientMapping);
        createClientPermeateClientSocketMessage(lazyInternalNetworkClientPermeateClientMapping);
        return lazyInternalNetworkClientPermeateClientMappingRepository.story(lazyInternalNetworkClientPermeateClientMapping);
    }

    /**
     * describe 查询单个客户端渗透客户端映射
     *
     * @param lazyInternalNetworkClientPermeateClientMappingQueryOneCommand 查询单个客户端渗透客户端映射     
     * @return {@link Result<LazyInternalNetworkClientPermeateClientMappingDTO>} 客户端渗透客户端映射DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/09/17 09:55 晚上
     **/

    @Override
    public Result<LazyInternalNetworkClientPermeateClientMappingDTO> findOne(LazyInternalNetworkClientPermeateClientMappingQueryOneCommand lazyInternalNetworkClientPermeateClientMappingQueryOneCommand) {
        LazyInternalNetworkClientPermeateClientMapping lazyInternalNetworkClientPermeateClientMapping = LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateClientMapping(lazyInternalNetworkClientPermeateClientMappingQueryOneCommand);
        return lazyInternalNetworkClientPermeateClientMappingRepository.findOne(lazyInternalNetworkClientPermeateClientMapping).convert(LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE::fromLazyInternalNetworkClientPermeateClientMapping);
    }

    /**
     * describe 查询多个客户端渗透客户端映射
     *
     * @param lazyInternalNetworkClientPermeateClientMappingQueryListCommand 查询多个客户端渗透客户端映射     
     * @return {@link Result<List<LazyInternalNetworkClientPermeateClientMappingDTO>>} 客户端渗透客户端映射DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/09/17 09:55 晚上
     **/

    @Override
    public Result<List<LazyInternalNetworkClientPermeateClientMappingDTO>> findList(LazyInternalNetworkClientPermeateClientMappingQueryListCommand lazyInternalNetworkClientPermeateClientMappingQueryListCommand) {
        LazyInternalNetworkClientPermeateClientMapping lazyInternalNetworkClientPermeateClientMapping = LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateClientMapping(lazyInternalNetworkClientPermeateClientMappingQueryListCommand);
        return lazyInternalNetworkClientPermeateClientMappingRepository.findList(lazyInternalNetworkClientPermeateClientMapping)        .convert(lazyInternalNetworkClientPermeateClientMappings -> lazyInternalNetworkClientPermeateClientMappings.stream().map(LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE::fromLazyInternalNetworkClientPermeateClientMapping).collect(Collectors.toList())) ;
    }

    /**
     * describe 分页查询多个客户端渗透客户端映射
     *
     * @param lazyInternalNetworkClientPermeateClientMappingQueryListCommand 分页查询多个客户端渗透客户端映射     
     * @return {@link Result<LazyPage<LazyInternalNetworkClientPermeateClientMappingDTO>>} 分页客户端渗透客户端映射DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/09/17 09:55 晚上
     **/

    @Override
    public Result<LazyPage<LazyInternalNetworkClientPermeateClientMappingDTO>> findPage(int size,int current,LazyInternalNetworkClientPermeateClientMappingQueryListCommand lazyInternalNetworkClientPermeateClientMappingQueryListCommand) {
        LazyInternalNetworkClientPermeateClientMapping lazyInternalNetworkClientPermeateClientMapping = LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateClientMapping(lazyInternalNetworkClientPermeateClientMappingQueryListCommand);
        return lazyInternalNetworkClientPermeateClientMappingRepository.findPage(size,current,lazyInternalNetworkClientPermeateClientMapping)        .convert(page -> page.convert(LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE::fromLazyInternalNetworkClientPermeateClientMapping))            ;
    }

    /**
     * describe 删除客户端渗透客户端映射
     *
     * @param lazyInternalNetworkClientPermeateClientMappingRemoveCommand 删除客户端渗透客户端映射     
     * @return {@link Result<LazyInternalNetworkClientPermeateClientMapping>} 客户端渗透客户端映射     
     
     * @author Jia wei Wu
     * @date 2024/09/17 09:55 晚上
     **/

    @Override
    public Result<LazyInternalNetworkClientPermeateClientMapping> remove(LazyInternalNetworkClientPermeateClientMappingRemoveCommand lazyInternalNetworkClientPermeateClientMappingRemoveCommand) {
     LazyInternalNetworkClientPermeateClientMapping lazyInternalNetworkClientPermeateClientMapping = LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateClientMapping(lazyInternalNetworkClientPermeateClientMappingRemoveCommand);
     return lazyInternalNetworkClientPermeateClientMappingRepository.remove(lazyInternalNetworkClientPermeateClientMapping);
    }

    /**
     * 关闭 客户端渗透客户端socket 消息
     *
     * @param lazyInternalNetworkClientPermeateClientMapping 客户端渗透客户端映射
     */
    public  void  closeClientPermeateClientSocketMessage(LazyInternalNetworkClientPermeateClientMapping lazyInternalNetworkClientPermeateClientMapping){
        // 发送客户端初始化渗透
        String clientId = lazyInternalNetworkClientPermeateClientMapping.getFromClientId();
        Channel clientChannel = ChannelContext.getLoadBalance(clientId);
        if (clientChannel != null && clientChannel.isActive()) {

            String permeateTargetIp = lazyInternalNetworkClientPermeateClientMapping.getPermeateTargetIp();
            Integer permeateTargetPort = lazyInternalNetworkClientPermeateClientMapping.getPermeateTargetPort();
            Integer visitorPort = lazyInternalNetworkClientPermeateClientMapping.getVisitorPort();
            NettyProxyMsg nettyMsg = new NettyProxyMsg();
            nettyMsg.setType(MessageType.DISTRIBUTE_CLIENT_PERMEATE_CLIENT_CLOSE);
            nettyMsg.setClientTargetIp(permeateTargetIp);
            nettyMsg.setClientTargetPort(permeateTargetPort);
            nettyMsg.setVisitorPort(visitorPort);
            clientChannel.writeAndFlush(nettyMsg);
        }
    }

    /**
     * 创建 客户端渗透客户端socket 消息
     *
     * @param lazyInternalNetworkClientPermeateClientMapping 客户端渗透客户端映射
     */
    public  void  createClientPermeateClientSocketMessage(LazyInternalNetworkClientPermeateClientMapping lazyInternalNetworkClientPermeateClientMapping){
        // 发送客户端初始化渗透
        String fromClientId = lazyInternalNetworkClientPermeateClientMapping.getFromClientId();
        String toClientId = lazyInternalNetworkClientPermeateClientMapping.getToClientId();
        Channel clientChannel = ChannelContext.getLoadBalance(fromClientId);
        if(clientChannel!=null  &&clientChannel.isActive()){
            String permeateTargetIp = lazyInternalNetworkClientPermeateClientMapping.getPermeateTargetIp();
            Integer permeateTargetPort = lazyInternalNetworkClientPermeateClientMapping.getPermeateTargetPort();
            Integer visitorPort = lazyInternalNetworkClientPermeateClientMapping.getVisitorPort();
            NettyProxyMsg nettyMsg = new NettyProxyMsg();
            nettyMsg.setType(MessageType.DISTRIBUTE_CLIENT_PERMEATE_CLIENT_INIT);
            nettyMsg.setClientTargetIp(permeateTargetIp);
            nettyMsg.setClientTargetPort(permeateTargetPort);
            nettyMsg.setVisitorPort(visitorPort);
            nettyMsg.setClientId(fromClientId);
            nettyMsg.setData(toClientId.getBytes(StandardCharsets.UTF_8));
            clientChannel.writeAndFlush(nettyMsg);
        }
    }
}