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.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.permeate.server.mapping.LazyNettyClientPermeateServerMapping;
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.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.LazyNettyClientPermeateServerMappingRepository;
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
    LazyNettyClientPermeateServerMappingRepository lazyNettyClientPermeateServerMappingRepository;

    @Resource
    ServerNodeProperties serverNodeProperties;

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

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

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

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


        createClientPermeateServerSocketMessage(lazyNettyClientPermeateServerMapping);
        return lazyNettyClientPermeateServerMappingRepository.story(lazyNettyClientPermeateServerMapping);
    }

    /**
     * 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) {
        LazyNettyClientPermeateServerMapping lazyNettyClientPermeateServerMapping = LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateServerMapping(lazyInternalNetworkClientPermeateServerMappingQueryOneCommand);
        return lazyNettyClientPermeateServerMappingRepository.findOne(lazyNettyClientPermeateServerMapping).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) {
        LazyNettyClientPermeateServerMapping lazyNettyClientPermeateServerMapping = LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateServerMapping(lazyInternalNetworkClientPermeateServerMappingQueryListCommand);
        return lazyNettyClientPermeateServerMappingRepository.findList(lazyNettyClientPermeateServerMapping)        .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) {
        LazyNettyClientPermeateServerMapping lazyNettyClientPermeateServerMapping = LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateServerMapping(lazyInternalNetworkClientPermeateServerMappingQueryListCommand);
        return lazyNettyClientPermeateServerMappingRepository.findPage(size,current, lazyNettyClientPermeateServerMapping)        .convert(page -> page.convert(LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE::fromLazyInternalNetworkClientPermeateServerMapping))            ;
    }

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

    @Override
    public Result<LazyNettyClientPermeateServerMapping> remove(LazyInternalNetworkClientPermeateServerMappingRemoveCommand lazyInternalNetworkClientPermeateServerMappingRemoveCommand) {
     LazyNettyClientPermeateServerMapping lazyNettyClientPermeateServerMapping = LazyInternalNetworkClientPermeateServerMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateServerMapping(lazyInternalNetworkClientPermeateServerMappingRemoveCommand);
     return lazyNettyClientPermeateServerMappingRepository.remove(lazyNettyClientPermeateServerMapping);
    }

    /**
     * 关闭 客户端渗透服务端socket 消息
     * @param lazyNettyClientPermeateServerMapping 客户端渗透服务端映射
     */
    public  void  closeClientPermeateServerSocketMessage(LazyNettyClientPermeateServerMapping lazyNettyClientPermeateServerMapping){
        // 发送客户端初始化渗透
        String clientId = lazyNettyClientPermeateServerMapping.getClientId();
        Channel clientChannel = ChannelContext.getLoadBalance(clientId);
        if(clientChannel!=null  &&clientChannel.isActive()){
            String permeateTargetIp = lazyNettyClientPermeateServerMapping.getPermeateTargetIp();
            Integer permeateTargetPort = lazyNettyClientPermeateServerMapping.getPermeateTargetPort();
            Integer visitorPort = lazyNettyClientPermeateServerMapping.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 lazyNettyClientPermeateServerMapping 客户端渗透服务端映射
     */
    public  void  createClientPermeateServerSocketMessage(LazyNettyClientPermeateServerMapping lazyNettyClientPermeateServerMapping){
        // 发送客户端初始化渗透
        String clientId = lazyNettyClientPermeateServerMapping.getClientId();
        Channel clientChannel = ChannelContext.getLoadBalance(clientId);
        if(clientChannel!=null  &&clientChannel.isActive()){
            String permeateTargetIp = lazyNettyClientPermeateServerMapping.getPermeateTargetIp();
            Integer permeateTargetPort = lazyNettyClientPermeateServerMapping.getPermeateTargetPort();
            Integer visitorPort = lazyNettyClientPermeateServerMapping.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);
        }
    }
}