package com.mooctest.config;

import com.mongodb.MongoClientOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;


@Configuration
@EnableMongoRepositories(
        basePackages = "com.mooctest.dao2",
        mongoTemplateRef = "mongoTemplate2")
public class MongoTwoConfig {

    @Autowired
    private Environment environment;


    @Bean(name = "mongoTemplate2")
    public MongoTemplate mongoTemplate(@Qualifier("mongoFactory2") MongoDbFactory mongoFactory) throws Exception {
        return new MongoTemplate(mongoFactory);
    }

    @Bean("mongoFactory2")
    public MongoDbFactory mongoFactory(
            @Qualifier("mongoProperties2") MongoProperties mongoProperties) throws Exception {
        return new SimpleMongoDbFactory(
                mongoProperties.createMongoClient(MongoClientOptions.builder().build(), environment),
                mongoProperties.getDatabase());
    }

    @Bean(name = "mongoProperties2")
    @ConfigurationProperties(prefix = "mongodb2")
    public MongoProperties properties() {
        return new MongoProperties();
    }

}

