package com.friday.custody.config;

import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.alibaba.dubbo.config.spring.ServiceBean;
import com.friday.custody.service.dubboService.CustodyDataServiceImpl;
import com.friday.mystery.toolkitapi.service.ITradingDayService;
//import com.htsc.pipe.api.service.ChanpinquanjingService;
import com.friday.operation.api.custody.service.ICustodyDataService;
import com.friday.operation.api.custody.service.ITrustAuthorizationService;
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;

@Configuration
public class DubboConfig {

//    @Value("${custody.dubbo.consumer.filter}")
//    private String consumerFilter;
    @Value("${custody.dubbo.application.name}")
    private String applicationName;
//    @Value("taie-outward")
//    private String applicationOwner;
    @Value("${custody.dubbo.registry.CDSAddress}")
    private String registryCDSAddress;
    @Value("${custody.dubbo.registry.CDSTimeout}")
    private Integer registryCDSTimeout;
    @Value("${custody.dubbo.registry.ibpmsNotification.address}")
    private String registryIbpmsNotificationAddress;
    @Value("${custody.dubbo.registry.ibpmsNotification.registryTimeout}")
    private Integer registryTimeout;

    @Bean
    public ProviderConfig providerConfig() {
        ProviderConfig providerConfig = new ProviderConfig();
//        providerConfig.setFilter(consumerFilter);
        return providerConfig;
    }
    @Bean
    public ConsumerConfig consumerConfig() {
        ConsumerConfig consumerConfig = new ConsumerConfig();
//        consumerConfig.setFilter(consumerFilter);
        return consumerConfig;
    }
    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(applicationName);
        return applicationConfig;
    }


    /**
     * 资讯中心接口服务API
     */
    @Bean
    public ReferenceBean referenceCDSServiceBean() {
        ReferenceBean referenceBean = new ReferenceBean();
        referenceBean.setInterface("com.friday.mystery.forwardapi.service.IHttpRequestService");
        referenceBean.setId("httpRequestService");
        referenceBean.setVersion("1.0.0");
        referenceBean.setCluster("failfast");
        referenceBean.setTimeout(registryCDSTimeout);
        referenceBean.setCheck(false);
        referenceBean.setRegistry(this.registryCDSConfig());
        return referenceBean;
    }

    @Bean
    public RegistryConfig registryCDSConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(registryCDSAddress);
        registryConfig.setTimeout(registryTimeout);
        registryConfig.setCheck(false);
        return registryConfig;
    }

//    @Bean
//    public ReferenceBean referenceIbpmsNotificationNotifyServiceBean() {
//        ReferenceBean referenceBean = new ReferenceBean();
//        referenceBean.setRegistry(this.registryIbpmsNotificationConfig());
//        referenceBean.setInterface("com.friday.mystery.notificationapi.service.INotifyService");
//        referenceBean.setId("notifyService");
//        referenceBean.setVersion("1.0.0");
//        referenceBean.setTimeout(registryTimeout);
//        referenceBean.setCheck(false);
//        return referenceBean;
//    }

    @Bean
    public ReferenceBean referenceIbpmsTradingDayServiceBean() {
        ReferenceBean referenceBean = new ReferenceBean();
        referenceBean.setRegistry(this.registryIbpmsNotificationConfig());
        referenceBean.setInterface(ITradingDayService.class);
        referenceBean.setId("tradingDayService");
        referenceBean.setVersion("1.0.0");
        referenceBean.setTimeout(registryTimeout);
        referenceBean.setCheck(false);
        return referenceBean;
    }
//    @Bean
//    public ReferenceBean chanpingquanjingServiceBean(){
//        ReferenceBean referenceBean = new ReferenceBean();
//        referenceBean.setRegistry(this.registryIbpmsNotificationConfig());
//        referenceBean.setInterface(ChanpinquanjingService.class);
//        referenceBean.setId("chanpinquanjingFromPrivatePlantFormService");
//        referenceBean.setVersion("1.0.0");
//        referenceBean.setTimeout(registryTimeout);
//        referenceBean.setCheck(false);
//        return referenceBean;
//    }

    @Bean
    public RegistryConfig registryIbpmsNotificationConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(registryIbpmsNotificationAddress);
        registryConfig.setTimeout(registryTimeout);
        registryConfig.setCheck(false);
        return registryConfig;
    }

    @Autowired
    @Lazy
    private CustodyDataServiceImpl custodyDataService;
    @Bean
    public ServiceBean custodyDataServiceBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(ICustodyDataService.class);
        serviceBean.setRef(this.custodyDataService);
        serviceBean.setVersion("1.0.0");
        serviceBean.setRegistry(this.registryIbpmsNotificationConfig());
        return serviceBean;
    }

    @Autowired
    @Lazy
    private ITrustAuthorizationService trustAuthorizationService;
    @Bean
    public ServiceBean trustAuthorizationServiceBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(ITrustAuthorizationService.class);
        serviceBean.setRef(this.trustAuthorizationService);
        serviceBean.setVersion("1.0.0");
        serviceBean.setRegistry(this.registryIbpmsNotificationConfig());
        return serviceBean;
    }


}
