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.LazyNettyClientPermeateClientMapping;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.netty.client.permeate.client.mapping.LazyNettyClientPermeateClientMappingRepository;
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
    LazyNettyClientPermeateClientMappingRepository lazyNettyClientPermeateClientMappingRepository;
    /**
     * describe 新增客户端渗透客户端映射
     *
     * @param lazyInternalNetworkClientPermeateClientMappingStoryCommand 新增客户端渗透客户端映射     
     * @return {@link Result< LazyNettyClientPermeateClientMapping >} 客户端渗透客户端映射新增后领域对象
     
     * @author Jia wei Wu
     * @date 2024/09/17 09:55 晚上
     **/

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

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

    @Override
    public Result<LazyNettyClientPermeateClientMapping> updateOne(LazyInternalNetworkClientPermeateClientMappingUpdateCommand lazyInternalNetworkClientPermeateClientMappingUpdateCommand) {
        LazyNettyClientPermeateClientMapping lazyNettyClientPermeateClientMapping = LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateClientMapping(lazyInternalNetworkClientPermeateClientMappingUpdateCommand);
        createClientPermeateClientSocketMessage(lazyNettyClientPermeateClientMapping);
        createClientPermeateClientSocketMessage(lazyNettyClientPermeateClientMapping);
        return lazyNettyClientPermeateClientMappingRepository.story(lazyNettyClientPermeateClientMapping);
    }

    /**
     * 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) {
        LazyNettyClientPermeateClientMapping lazyNettyClientPermeateClientMapping = LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateClientMapping(lazyInternalNetworkClientPermeateClientMappingQueryOneCommand);
        return lazyNettyClientPermeateClientMappingRepository.findOne(lazyNettyClientPermeateClientMapping).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) {
        LazyNettyClientPermeateClientMapping lazyNettyClientPermeateClientMapping = LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateClientMapping(lazyInternalNetworkClientPermeateClientMappingQueryListCommand);
        return lazyNettyClientPermeateClientMappingRepository.findList(lazyNettyClientPermeateClientMapping)        .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) {
        LazyNettyClientPermeateClientMapping lazyNettyClientPermeateClientMapping = LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateClientMapping(lazyInternalNetworkClientPermeateClientMappingQueryListCommand);
        return lazyNettyClientPermeateClientMappingRepository.findPage(size,current, lazyNettyClientPermeateClientMapping)        .convert(page -> page.convert(LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE::fromLazyInternalNetworkClientPermeateClientMapping))            ;
    }

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

    @Override
    public Result<LazyNettyClientPermeateClientMapping> remove(LazyInternalNetworkClientPermeateClientMappingRemoveCommand lazyInternalNetworkClientPermeateClientMappingRemoveCommand) {
     LazyNettyClientPermeateClientMapping lazyNettyClientPermeateClientMapping = LazyInternalNetworkClientPermeateClientMappingDTOAssembler.INSTANCE.toLazyInternalNetworkClientPermeateClientMapping(lazyInternalNetworkClientPermeateClientMappingRemoveCommand);
     return lazyNettyClientPermeateClientMappingRepository.remove(lazyNettyClientPermeateClientMapping);
    }

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

            String permeateTargetIp = lazyNettyClientPermeateClientMapping.getPermeateTargetIp();
            Integer permeateTargetPort = lazyNettyClientPermeateClientMapping.getPermeateTargetPort();
            Integer visitorPort = lazyNettyClientPermeateClientMapping.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 lazyNettyClientPermeateClientMapping 客户端渗透客户端映射
     */
    public  void  createClientPermeateClientSocketMessage(LazyNettyClientPermeateClientMapping lazyNettyClientPermeateClientMapping){
        // 发送客户端初始化渗透
        String fromClientId = lazyNettyClientPermeateClientMapping.getFromClientId();
        String toClientId = lazyNettyClientPermeateClientMapping.getToClientId();
        Channel clientChannel = ChannelContext.getLoadBalance(fromClientId);
        if(clientChannel!=null  &&clientChannel.isActive()){
            String permeateTargetIp = lazyNettyClientPermeateClientMapping.getPermeateTargetIp();
            Integer permeateTargetPort = lazyNettyClientPermeateClientMapping.getPermeateTargetPort();
            Integer visitorPort = lazyNettyClientPermeateClientMapping.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);
        }
    }
}