package com.brilliance.isc.doc.convert;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;

import java.io.IOException;
import java.util.Optional;
import java.util.Properties;

public class CompositePropertySourceFactory extends DefaultPropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        String sourceName = Optional.ofNullable(name).orElse(resource.getResource().getFilename());
        if(!resource.getResource().exists()){
            return new PropertiesPropertySource(sourceName, new Properties());
        }
        else if(sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")){
            Properties propertiesFromYaml = loadYaml(resource);
            return new PropertiesPropertySource(sourceName,propertiesFromYaml);
        }else{
            return super.createPropertySource(name,resource);
        }
    }

    private Properties loadYaml(EncodedResource resource){
        YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
        factoryBean.setResources(resource.getResource());
        factoryBean.afterPropertiesSet();
        return factoryBean.getObject();
    }
}
