package com.bonus.common.houqin.oss;

import com.bonus.common.houqin.oss.http.OssEndpoint;
import com.bonus.common.houqin.oss.service.OssTemplate;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@EnableConfigurationProperties({OssProperties.class})
//@AutoConfiguration
@Configuration
public class OssAutoConfiguration {
   private final OssProperties properties;

   @Bean
   @ConditionalOnMissingBean({OssTemplate.class})
   @ConditionalOnProperty(
      name = {"oss.enable"},
      havingValue = "true",
      matchIfMissing = true
   )
   public OssTemplate ossTemplate() {
      return new OssTemplate(this.properties);
   }

   @Bean
   @ConditionalOnProperty(
      name = {"oss.info"},
      havingValue = "true"
   )
   public OssEndpoint ossEndpoint(OssTemplate template) {
      return new OssEndpoint(template);
   }

   public OssAutoConfiguration(final OssProperties properties) {
      this.properties = properties;
   }
}
