package com.friday.executor.config;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.alibaba.dubbo.config.spring.ServiceBean;
import com.friday.blank.api.service.external.IExternalNotifyService;
import com.friday.mystery.apiauthentication.service.IAccountService;
import com.friday.mystery.toolkitapi.service.ITradingDayService;
import com.friday.operation.api.distributor.service.IDistributorInternalService;
import com.friday.operation.api.executor.service.IDynamicModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

/**
 * @Author YaoYao
 * @Date Created In 12:10 2022/12/14
 * @Description
 * @Modified By
 */
@Configuration
public class ExecutorDubboConfig {
    @Value("${dubbo.application.name}")
    private String applicationName;
    @Value("${dubbo.application.owner}")
    private String applicationOwner;
    @Value("${dubbo.application.logger}")
    private String applicationLogger;
    @Value("${dubbo.protocol.name}")
    private String protocolName;
    @Value("${dubbo.protocol.port}")
    private int protocolPort;
    @Value("${dubbo.registry.executor.default.address}")
    private String executorRegistryAddress;
    @Value("${inter.provider.dubbo.address}")
    private String interProviderRegistryAddress;
    @Value("${dubbo.registry.executor.default.timeout}")
    private int executorRegistryTimeout;
    @Value("${dubbo.registry.executor.default.version}")
    private String executorRegistryVersion;

    @Autowired
    @Lazy
    private IDynamicModuleService dynamicModuleService;

    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(applicationName);
        applicationConfig.setOwner(applicationOwner);
        applicationConfig.setLogger(applicationLogger);
        return applicationConfig;
    }

    @Bean
    public ProtocolConfig protocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName(protocolName);
        protocolConfig.setPort(protocolPort);
        return protocolConfig;
    }

    private RegistryConfig defaultRegistryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(executorRegistryAddress);
        registryConfig.setTimeout(executorRegistryTimeout);
        registryConfig.setDefault(true);
        return registryConfig;
    }

    @Bean
    public ServiceBean serviceBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(IDynamicModuleService.class);
        serviceBean.setRef(this.dynamicModuleService);
        serviceBean.setRegistry(this.defaultRegistryConfig());
        serviceBean.setVersion(executorRegistryVersion);
        serviceBean.setTimeout(30000);
        return serviceBean;
    }

    @Bean
    public ReferenceBean referenceDistributorInternalServiceBean() {
        ReferenceBean referenceBean = new ReferenceBean();
        referenceBean.setInterface(IDistributorInternalService.class);
        referenceBean.setId("distributorInternalService");
        referenceBean.setRegistry(this.defaultRegistryConfig());
        referenceBean.setRetries(0);
        referenceBean.setTimeout(30000);
        referenceBean.setCheck(false);
        referenceBean.setVersion(executorRegistryVersion);
        return referenceBean;
    }

    private RegistryConfig interProviderRegistry() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(interProviderRegistryAddress);
        registryConfig.setTimeout(executorRegistryTimeout);
        return registryConfig;
    }

    @Bean
    public ReferenceBean<IAccountService> accountServiceReferenceBean(){
        ReferenceBean<IAccountService> referenceBean = new ReferenceBean<>();
        referenceBean.setInterface(IAccountService.class);
        referenceBean.setId("accountService");
        referenceBean.setTimeout(300000);
        referenceBean.setCheck(false);
        referenceBean.setVersion(executorRegistryVersion);
        referenceBean.setRegistry(this.defaultRegistryConfig());
        return referenceBean;
    }

    @Bean
    public ReferenceBean<IExternalNotifyService> externalNotifyServiceReferenceBean(){
        ReferenceBean<IExternalNotifyService> referenceBean = new ReferenceBean<>();
        referenceBean.setInterface(IExternalNotifyService.class);
        referenceBean.setId("externalNotifyService");
        referenceBean.setCheck(false);
        referenceBean.setVersion(executorRegistryVersion);
        referenceBean.setRegistry(this.defaultRegistryConfig());
        return referenceBean;
    }

    @Bean
    public ReferenceBean<ITradingDayService> tradingDayServiceReferenceBean(){
        ReferenceBean<ITradingDayService> referenceBean = new ReferenceBean<>();
        referenceBean.setInterface(ITradingDayService.class);
        referenceBean.setId("tradingDayService");
        referenceBean.setCheck(false);
        referenceBean.setVersion(executorRegistryVersion);
        referenceBean.setRegistry(this.defaultRegistryConfig());
        return referenceBean;
    }
}
