package com.friday.tooldriver.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.ServiceBean;
import com.friday.mystery.coresecurity.config.AuthenticationDubboxConfig;
import com.friday.operation.api.keepalive.service.taieoc.IBadgeManage;
import com.friday.operation.api.keepalive.service.taieoc.IGroupManage;
import com.friday.operation.api.keepalive.service.taieoc.IOcManage;
import com.friday.operation.api.keepalive.service.taieoc.ITenantManage;
import com.friday.operation.api.tooldriver.service.*;
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 ToolDriverDubboConfig extends AuthenticationDubboxConfig {
    @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.tooldriver.default.address}")
    private String tooldriverRegistryAddress;
    @Value("${dubbo.registry.tooldriver.default.timeout}")
    private int tooldriverRegistryTimeout;
    @Value("${dubbo.registry.tooldriver.default.version}")
    private String tooldriverRegistryVersion;

    @Autowired
    @Lazy
    private ISmartRPAHeartbeatService heartbeatService;

    @Autowired
    @Lazy
    private IProcess processManager;

    @Autowired
    @Lazy
    private ILicense licenseManager;

    @Autowired
    @Lazy
    private ICapabilityDriver rpaDriver;

    @Autowired
    private ITenantManage tenantManage;

    @Autowired
    private IOcManage ocManage;

    @Autowired
    private IGroupManage groupManage;

    @Autowired
    private IBadgeManage badgeManage;

    @Autowired
    private IEnvironment environmentManage;

    @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(tooldriverRegistryAddress);
        registryConfig.setTimeout(tooldriverRegistryTimeout);
        registryConfig.setDefault(true);
        return registryConfig;
    }

    @Bean
    public ServiceBean serviceBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(ISmartRPAHeartbeatService.class);
        serviceBean.setRef(this.heartbeatService);
        serviceBean.setRegistry(this.defaultRegistryConfig());
        serviceBean.setVersion(tooldriverRegistryVersion);
        return serviceBean;
    }

    @Bean
    public ServiceBean serviceProcessBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(IProcess.class);
        serviceBean.setRef(this.processManager);
        serviceBean.setRegistry(this.defaultRegistryConfig());
        serviceBean.setVersion(tooldriverRegistryVersion);
        return serviceBean;
    }
    @Bean
    public ServiceBean serviceLicenseBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(ILicense.class);
        serviceBean.setRef(this.licenseManager);
        serviceBean.setRegistry(this.defaultRegistryConfig());
        serviceBean.setVersion(tooldriverRegistryVersion);
        return serviceBean;
    }

    @Bean
    public ServiceBean serviceRpaDriverBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(ICapabilityDriver.class);
        serviceBean.setRef(this.rpaDriver);
        serviceBean.setRegistry(this.defaultRegistryConfig());
        serviceBean.setVersion(tooldriverRegistryVersion);
        return serviceBean;
    }

    @Bean
    public ServiceBean serviceTenantBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(ITenantManage.class);
        serviceBean.setRef(this.tenantManage);
        serviceBean.setRegistry(this.defaultRegistryConfig());
        serviceBean.setVersion(tooldriverRegistryVersion);
        return serviceBean;
    }

    @Bean
    public ServiceBean serviceOcBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(IOcManage.class);
        serviceBean.setRef(this.ocManage);
        serviceBean.setRegistry(this.defaultRegistryConfig());
        serviceBean.setVersion(tooldriverRegistryVersion);
        return serviceBean;
    }

    @Bean
    public ServiceBean serviceEnvironmentBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(IEnvironment.class);
        serviceBean.setRef(this.environmentManage);
        serviceBean.setRegistry(this.defaultRegistryConfig());
        serviceBean.setVersion(tooldriverRegistryVersion);
        return serviceBean;
    }

    @Bean
    public ServiceBean serviceGroupBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(IGroupManage.class);
        serviceBean.setRef(this.groupManage);
        serviceBean.setRegistry(this.defaultRegistryConfig());
        serviceBean.setVersion(this.tooldriverRegistryVersion);
        return serviceBean;
    }
    @Bean
    public ServiceBean serviceBadgeBean(){
        ServiceBean serviceBean = new ServiceBean();
        serviceBean.setInterface(IBadgeManage.class);
        serviceBean.setRef(this.badgeManage);
        serviceBean.setRegistry(this.defaultRegistryConfig());
        serviceBean.setVersion(this.tooldriverRegistryVersion);
        return serviceBean;
    }
}
