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

import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.infrastructure.entity.LazyNettyServerPropertiesDO;
import org.framework.lazy.cloud.network.heartbeat.client.application.LazyNettyServerPropertiesApplication;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;

import java.util.Objects;

/**
 * 初始化配置
 */
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Configuration
public class InitConfig implements CommandLineRunner, DisposableBean {

    private final NettyClientProperties nettyClientProperties;
    private final LazyLambdaStream lazyLambdaStream;

    private final LazyNettyServerPropertiesApplication lazyNettyServerPropertiesApplication;

    public InitConfig(NettyClientProperties nettyClientProperties, LazyLambdaStream lazyLambdaStream, LazyNettyServerPropertiesApplication lazyNettyServerPropertiesApplication) {
        this.nettyClientProperties = nettyClientProperties;
        this.lazyLambdaStream = lazyLambdaStream;
        this.lazyNettyServerPropertiesApplication = lazyNettyServerPropertiesApplication;
    }


    @Override
    public void run(String... args) throws Exception {
        try {
            // 存储配置到db
            initDb2Config();
            // 启动客户端连接
            lazyNettyServerPropertiesApplication.starterAllClientSocket();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 存储配置到db
     */
    public void initDb2Config() {

        String clientId = nettyClientProperties.getClientId();
        String inetHost = nettyClientProperties.getInetHost();
        int inetPort = nettyClientProperties.getInetPort();
        String appKey = nettyClientProperties.getAppKey();
        String appSecret = nettyClientProperties.getAppSecret();
        if (Objects.isNull(clientId) ||
                Objects.isNull(inetHost)) {
            log.warn("配置信息为空，请通过页面添加配置信息:{}", nettyClientProperties);
            return;
        }
        LazyNettyServerPropertiesDO lazyNettyServerPropertiesDO = new LazyNettyServerPropertiesDO();
        lazyNettyServerPropertiesDO.setClientId(clientId);
        lazyNettyServerPropertiesDO.setInetHost(inetHost);
        lazyNettyServerPropertiesDO.setInetPort(inetPort);
        lazyNettyServerPropertiesDO.setType(PropertiesType.CONFIG);
        lazyNettyServerPropertiesDO.setIsDeleted(false);
        lazyNettyServerPropertiesDO.setAppKey(appKey);
        lazyNettyServerPropertiesDO.setAppSecret(appSecret);

        // 根据服务端端口、port 唯一性验证
        boolean exists = lazyLambdaStream.exists(LazyWrappers.<LazyNettyServerPropertiesDO>lambdaWrapper()
                .eq(LazyNettyServerPropertiesDO::getInetHost, inetHost)
                .eq(LazyNettyServerPropertiesDO::getInetPort, inetPort)
                .eq(LazyNettyServerPropertiesDO::getClientId, clientId)
        );
        if (!exists) {
            lazyLambdaStream.insert(lazyNettyServerPropertiesDO);
        }
    }

    /**
     * 程序关闭后执行
     */
    @Override
    public void destroy() {
        lazyNettyServerPropertiesApplication.destroyClientSocket();
    }
}
