package com.friday.distributor.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.IExternalCommonService;
import com.friday.operation.api.distributor.service.IDistributorInternalService;
import com.friday.operation.api.distributor.service.IDistributorOperationService;
import com.friday.operation.api.distributor.service.IDistributorService;
import com.friday.operation.api.executor.service.IDynamicModuleService;
import com.friday.operation.api.tooldriver.service.ICapabilityDriver;
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 DistributorDubboConfig {
    @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.distributor.default.address}")
    private String distributorRegistryAddress;
    @Value("${dubbo.registry.distributor.default.timeout}")
    private int distributorRegistryTimeout;
    @Value("${dubbo.registry.distributor.default.version}")
    private String distributorRegistryVersion;

    @Autowired
    @Lazy
    private IDistributorService distributorService;

    @Autowired
    @Lazy
    private IDistributorInternalService distributorInternalService;

    @Autowired
    @Lazy
    private IDistributorOperationService distributorOperationService;

    @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(distributorRegistryAddress);
        registryConfig.setTimeout(distributorRegistryTimeout);
        return registryConfig;
    }

    @Bean
    public ReferenceBean referenceBlankCommonServiceBean() {
        ReferenceBean referenceBean = new ReferenceBean();
        referenceBean.setInterface(IExternalCommonService.class);
        referenceBean.setId("externalCommonService");
        referenceBean.setVersion("1.0.0");
        referenceBean.setRegistry(this.defaultRegistryConfig());
        referenceBean.setCheck(false);
        return referenceBean;
    }

    @Bean
    public ReferenceBean referenceDynamicModuleBean() {
        ReferenceBean referenceBean = new ReferenceBean();
        referenceBean.setInterface(IDynamicModuleService.class);
        referenceBean.setId("dynamicModuleService");
        referenceBean.setRegistry(this.defaultRegistryConfig());
        referenceBean.setCheck(false);
        referenceBean.setVersion(distributorRegistryVersion);
        referenceBean.setTimeout(30000);
        return referenceBean;
    }

    @Bean
    public ReferenceBean referenceRpaDriverBean() {
        ReferenceBean referenceBean = new ReferenceBean();
        referenceBean.setInterface(ICapabilityDriver.class);
        referenceBean.setId("rpaDriver");
        referenceBean.setRegistry(this.defaultRegistryConfig());
        referenceBean.setRetries(0);
        referenceBean.setTimeout(30000);
        referenceBean.setCheck(false);
        referenceBean.setVersion("1.0.0");
        return referenceBean;
    }

    @Bean
    public ServiceBean distributorBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(IDistributorService.class);
        serviceBean.setRef(this.distributorService);
        serviceBean.setRegistry(this.defaultRegistryConfig());
        serviceBean.setVersion(distributorRegistryVersion);
        return serviceBean;
    }

    @Bean
    public ServiceBean distributorInternalBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(IDistributorInternalService.class);
        serviceBean.setRef(this.distributorInternalService);
        serviceBean.setRegistry(this.defaultRegistryConfig());
        serviceBean.setVersion(distributorRegistryVersion);
        return serviceBean;
    }

    @Bean
    public ServiceBean distributorOperationBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(IDistributorOperationService.class);
        serviceBean.setRef(this.distributorOperationService);
        serviceBean.setRegistry(this.defaultRegistryConfig());
        serviceBean.setVersion(distributorRegistryVersion);
        return serviceBean;
    }
}
