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

import io.netty.channel.Channel;
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.properties.ServerNodeProperties;
import org.wu.framework.database.lazy.web.plus.stereotype.LazyApplication;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.LazyInternalNetworkClientPermeateServerMappingApplication;
import org.wu.framework.web.response.Result;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.permeate.server.mapping.LazyInternalNetworkClientPermeateServerMapping;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.permeate.server.mapping.LazyInternalNetworkClientPermeateServerMappingRemoveCommand;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.permeate.server.mapping.LazyInternalNetworkClientPermeateServerMappingStoryCommand;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.permeate.server.mapping.LazyInternalNetworkClientPermeateServerMappingUpdateCommand;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.permeate.server.mapping.LazyInternalNetworkClientPermeateServerMappingQueryListCommand;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.permeate.server.mapping.LazyInternalNetworkClientPermeateServerMappingQueryOneCommand;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.assembler.LazyInternalNetworkClientPermeateServerMappingDTOAssembler;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyInternalNetworkClientPermeateServerMappingDTO;
import java.util.stream.Collectors;
import jakarta.annotation.Resource;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.permeate.server.mapping.LazyInternalNetworkClientPermeateServerMappingRepository;
import java.util.List;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
/**
 * describe 客户端渗透服务端映射 
 *
 * @author Jia wei Wu
 * @date 2024/09/17 09:26 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl 
 **/
@LazyApplication
public class LazyInternalNetworkClientPermeateServerMappingApplicationImpl implements LazyInternalNetworkClientPermeateServerMappingApplication {

    @Resource
    LazyInternalNetworkClientPermeateServerMappingRepository lazyInternalNetworkClientPermeateServerMappingRepository;

    @Resource
    ServerNodeProperties serverNodeProperties;

    /**
     * describe 新增客户端渗透服务端映射
     *
     * @param lazyInternalNetworkClientPermeateServerMappingStoryCommand 新增客户端渗透服务端映射     
     * @return {@link Result<LazyInternalNetworkClientPermeateServerMapping>} 客户端渗透服务端映射新增后领域对象     
     
     * @author Jia wei Wu
     * @date 2024/09/17 09:26 晚上
     **/

    @Override
    public Result<LazyInternalNetworkClientPermeateServerMapping> story(LazyInternalNetworkClientPermeateServerMappingStoryCommand lazyInternalNetworkClientPermeateServerMappingStoryCommand) {
        LazyInternalNetworkClientPermeateServerMapping lazyInternalNetworkClientPermeateServerMapping = LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateServerMapping(lazyInternalNetworkClientPermeateServerMappingStoryCommand);
        String serverId = serverNodeProperties.getNodeId();
        lazyInternalNetworkClientPermeateServerMapping.setServerId(serverId);
        // 发送客户端初始化渗透
        createClientPermeateServerSocketMessage(lazyInternalNetworkClientPermeateServerMapping);
        return lazyInternalNetworkClientPermeateServerMappingRepository.story(lazyInternalNetworkClientPermeateServerMapping);
    }
    /**
     * describe 批量新增客户端渗透服务端映射
     *
     * @param lazyInternalNetworkClientPermeateServerMappingStoryCommandList 批量新增客户端渗透服务端映射     
     * @return {@link Result<List<LazyInternalNetworkClientPermeateServerMapping>>} 客户端渗透服务端映射新增后领域对象集合     
     
     * @author Jia wei Wu
     * @date 2024/09/17 09:26 晚上
     **/

    @Override
    public Result<List<LazyInternalNetworkClientPermeateServerMapping>> batchStory(List<LazyInternalNetworkClientPermeateServerMappingStoryCommand> lazyInternalNetworkClientPermeateServerMappingStoryCommandList) {
        List<LazyInternalNetworkClientPermeateServerMapping> lazyInternalNetworkClientPermeateServerMappingList = lazyInternalNetworkClientPermeateServerMappingStoryCommandList.stream().map( LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE::toLazyInternalNetworkClientPermeateServerMapping).collect(Collectors.toList());
        for (LazyInternalNetworkClientPermeateServerMapping lazyInternalNetworkClientPermeateServerMapping : lazyInternalNetworkClientPermeateServerMappingList) {
            String serverId = serverNodeProperties.getNodeId();
            lazyInternalNetworkClientPermeateServerMapping.setServerId(serverId);
            createClientPermeateServerSocketMessage(lazyInternalNetworkClientPermeateServerMapping);
        }
        return lazyInternalNetworkClientPermeateServerMappingRepository.batchStory(lazyInternalNetworkClientPermeateServerMappingList);
    }
    /**
     * describe 更新客户端渗透服务端映射
     *
     * @param lazyInternalNetworkClientPermeateServerMappingUpdateCommand 更新客户端渗透服务端映射     
     * @return {@link Result<LazyInternalNetworkClientPermeateServerMapping>} 客户端渗透服务端映射领域对象     
     
     * @author Jia wei Wu
     * @date 2024/09/17 09:26 晚上
     **/

    @Override
    public Result<LazyInternalNetworkClientPermeateServerMapping> updateOne(LazyInternalNetworkClientPermeateServerMappingUpdateCommand lazyInternalNetworkClientPermeateServerMappingUpdateCommand) {
        LazyInternalNetworkClientPermeateServerMapping lazyInternalNetworkClientPermeateServerMapping = LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateServerMapping(lazyInternalNetworkClientPermeateServerMappingUpdateCommand);
        String serverId = serverNodeProperties.getNodeId();
        lazyInternalNetworkClientPermeateServerMapping.setServerId(serverId);
        // 关闭
        LazyInternalNetworkClientPermeateServerMapping mapping = new LazyInternalNetworkClientPermeateServerMapping();
        mapping.setId(lazyInternalNetworkClientPermeateServerMapping.getId());
        lazyInternalNetworkClientPermeateServerMappingRepository.findOne(mapping).accept(this::closeClientPermeateServerSocketMessage);


        createClientPermeateServerSocketMessage(lazyInternalNetworkClientPermeateServerMapping);
        return lazyInternalNetworkClientPermeateServerMappingRepository.story(lazyInternalNetworkClientPermeateServerMapping);
    }

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

    @Override
    public Result<LazyInternalNetworkClientPermeateServerMappingDTO> findOne(LazyInternalNetworkClientPermeateServerMappingQueryOneCommand lazyInternalNetworkClientPermeateServerMappingQueryOneCommand) {
        LazyInternalNetworkClientPermeateServerMapping lazyInternalNetworkClientPermeateServerMapping = LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateServerMapping(lazyInternalNetworkClientPermeateServerMappingQueryOneCommand);
        return lazyInternalNetworkClientPermeateServerMappingRepository.findOne(lazyInternalNetworkClientPermeateServerMapping).convert(LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE::fromLazyInternalNetworkClientPermeateServerMapping);
    }

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

    @Override
    public Result<List<LazyInternalNetworkClientPermeateServerMappingDTO>> findList(LazyInternalNetworkClientPermeateServerMappingQueryListCommand lazyInternalNetworkClientPermeateServerMappingQueryListCommand) {
        LazyInternalNetworkClientPermeateServerMapping lazyInternalNetworkClientPermeateServerMapping = LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateServerMapping(lazyInternalNetworkClientPermeateServerMappingQueryListCommand);
        return lazyInternalNetworkClientPermeateServerMappingRepository.findList(lazyInternalNetworkClientPermeateServerMapping)        .convert(lazyInternalNetworkClientPermeateServerMappings -> lazyInternalNetworkClientPermeateServerMappings.stream().map(LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE::fromLazyInternalNetworkClientPermeateServerMapping).collect(Collectors.toList())) ;
    }

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

    @Override
    public Result<LazyPage<LazyInternalNetworkClientPermeateServerMappingDTO>> findPage(int size,int current,LazyInternalNetworkClientPermeateServerMappingQueryListCommand lazyInternalNetworkClientPermeateServerMappingQueryListCommand) {
        LazyInternalNetworkClientPermeateServerMapping lazyInternalNetworkClientPermeateServerMapping = LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateServerMapping(lazyInternalNetworkClientPermeateServerMappingQueryListCommand);
        return lazyInternalNetworkClientPermeateServerMappingRepository.findPage(size,current,lazyInternalNetworkClientPermeateServerMapping)        .convert(page -> page.convert(LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE::fromLazyInternalNetworkClientPermeateServerMapping))            ;
    }

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

    @Override
    public Result<LazyInternalNetworkClientPermeateServerMapping> remove(LazyInternalNetworkClientPermeateServerMappingRemoveCommand lazyInternalNetworkClientPermeateServerMappingRemoveCommand) {
     LazyInternalNetworkClientPermeateServerMapping lazyInternalNetworkClientPermeateServerMapping = LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateServerMapping(lazyInternalNetworkClientPermeateServerMappingRemoveCommand);
     return lazyInternalNetworkClientPermeateServerMappingRepository.remove(lazyInternalNetworkClientPermeateServerMapping);
    }

    /**
     * 关闭 客户端渗透服务端socket 消息
     * @param lazyInternalNetworkClientPermeateServerMapping 客户端渗透服务端映射
     */
    public  void  closeClientPermeateServerSocketMessage(LazyInternalNetworkClientPermeateServerMapping lazyInternalNetworkClientPermeateServerMapping){
        // 发送客户端初始化渗透
        String clientId = lazyInternalNetworkClientPermeateServerMapping.getClientId();
        Channel clientChannel = ChannelContext.getLoadBalance(clientId);
        if(clientChannel!=null  &&clientChannel.isActive()){
            String permeateTargetIp = lazyInternalNetworkClientPermeateServerMapping.getPermeateTargetIp();
            Integer permeateTargetPort = lazyInternalNetworkClientPermeateServerMapping.getPermeateTargetPort();
            Integer visitorPort = lazyInternalNetworkClientPermeateServerMapping.getVisitorPort();
            NettyProxyMsg nettyMsg = new NettyProxyMsg();
            nettyMsg.setType(MessageType.DISTRIBUTE_CLIENT_PERMEATE_SERVER_CLOSE);
            nettyMsg.setClientTargetIp(permeateTargetIp);
            nettyMsg.setClientTargetPort(permeateTargetPort);
            nettyMsg.setVisitorPort(visitorPort);
            clientChannel.writeAndFlush(nettyMsg);
        }
    }

    /**
     * 创建 客户端渗透服务端socket 消息
     * @param lazyInternalNetworkClientPermeateServerMapping 客户端渗透服务端映射
     */
    public  void  createClientPermeateServerSocketMessage(LazyInternalNetworkClientPermeateServerMapping lazyInternalNetworkClientPermeateServerMapping){
        // 发送客户端初始化渗透
        String clientId = lazyInternalNetworkClientPermeateServerMapping.getClientId();
        Channel clientChannel = ChannelContext.getLoadBalance(clientId);
        if(clientChannel!=null  &&clientChannel.isActive()){
            String permeateTargetIp = lazyInternalNetworkClientPermeateServerMapping.getPermeateTargetIp();
            Integer permeateTargetPort = lazyInternalNetworkClientPermeateServerMapping.getPermeateTargetPort();
            Integer visitorPort = lazyInternalNetworkClientPermeateServerMapping.getVisitorPort();
            NettyProxyMsg nettyMsg = new NettyProxyMsg();
            nettyMsg.setType(MessageType.DISTRIBUTE_CLIENT_PERMEATE_SERVER_INIT);
            nettyMsg.setClientTargetIp(permeateTargetIp);
            nettyMsg.setClientTargetPort(permeateTargetPort);
            nettyMsg.setVisitorPort(visitorPort);
            clientChannel.writeAndFlush(nettyMsg);
        }
    }
}