package org.framework.lazy.cloud.network.heartbeat.client.config;


import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.netty.socket.NettyClientSocket;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.framework.lazy.cloud.network.heartbeat.client.netty.event.ClientChangeEvent;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.wu.framework.core.NormalUsedString;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * description 自动配置
 *
 * @author 吴佳伟
 * @date 2023/09/12 18:22
 * @see InitConfig
 */
@Deprecated
@Slf4j
public class ClientAutoConfiguration implements CommandLineRunner {

    private final NettyClientProperties nettyClientProperties;
    private final ClientChangeEvent clientChangeEvent;

    private final List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList; // 处理服务端发送过来的数据类型


    public static final ThreadPoolExecutor NETTY_CLIENT_EXECUTOR = new ThreadPoolExecutor(1, 1, 200, TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<>(1));

    public ClientAutoConfiguration(NettyClientProperties nettyClientProperties,
                                   ClientChangeEvent clientChangeEvent,
                                   List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
        this.nettyClientProperties = nettyClientProperties;
        this.clientChangeEvent = clientChangeEvent;
        this.handleChannelTypeAdvancedList = handleChannelTypeAdvancedList;
    }


    @Bean(destroyMethod = "shutdown")
    public NettyClientSocket nettyClientSocket() {
        String inetHost = nettyClientProperties.getInetHost();
        int inetPort = nettyClientProperties.getInetPort();
        String clientId = nettyClientProperties.getClientId();
        String appKey = nettyClientProperties.getAppKey();
        String appSecret = nettyClientProperties.getAppSecret();
        return new NettyClientSocket(inetHost, inetPort, clientId, NormalUsedString.DEFAULT, appKey,appSecret,clientChangeEvent, handleChannelTypeAdvancedList);
    }

    /**
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {


        String inetHost = nettyClientProperties.getInetHost();
        int inetPort = nettyClientProperties.getInetPort();
        String clientId = nettyClientProperties.getClientId();
        String appKey = nettyClientProperties.getAppKey();
        String appSecret = nettyClientProperties.getAppSecret();
        NettyClientSocket nettyClientSocket = new NettyClientSocket(
                inetHost, inetPort,
                clientId, NormalUsedString.DEFAULT,appKey,appSecret,
                clientChangeEvent, handleChannelTypeAdvancedList);
        Thread thread = new Thread(() -> {
            try {
                nettyClientSocket.newConnect2Server();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        });
        log.info("当前服务连接Netty客户端:{},Netty端口:{}", inetHost, inetPort);
        NETTY_CLIENT_EXECUTOR.execute(thread);


    }
}
