package org.xcolab.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.ResourcePropertySource;

import org.xcolab.util.exceptions.InternalException;

import java.io.File;
import java.io.IOException;

public class HomeDirPropertyResolver extends StandardEnvironment {

    private static final Logger _log = LoggerFactory.getLogger(HomeDirPropertyResolver.class);

    private static final String HOME_DIR = System.getProperty("user.home");
    private static final String FILE_BASE_NAME = ".xcolab.application";
    private static final String FILE_BASE_PATH = HOME_DIR + File.separator + FILE_BASE_NAME;

    //.properties file
    private static final String PROPERTIES_FILE_PATH = FILE_BASE_PATH + ".properties";
    private static final File PROPERTIES_FILE = new File(PROPERTIES_FILE_PATH);

    //.yml file
    private static final String YAML_FILE_PATH = FILE_BASE_PATH + ".yml";
    private static final File YAML_FILE = new File(YAML_FILE_PATH);

    public HomeDirPropertyResolver(boolean required) {
        if (!(PROPERTIES_FILE.exists() || YAML_FILE.exists()) && required) {
            throw new InternalException("No property files found: " + PROPERTIES_FILE_PATH);
        }
    }

    @Override
    protected void customizePropertySources(MutablePropertySources propertySources) {
        super.customizePropertySources(propertySources);
        try {
            propertySources.addLast(
                    new ResourcePropertySource(new FileSystemResource(PROPERTIES_FILE)));
            propertySources.addLast(
                    new ResourcePropertySource(new FileSystemResource(YAML_FILE)));

        } catch (IOException e) {
            _log.warn("No properties file with allowed extension found in {}", PROPERTIES_FILE_PATH);
        }
    }
}

@@ -1605,80 +1605,96 @@
     
-propertySources.addLast(%0A                    new ResourcePropertyS
+if (PROPERTIES_FILE.exists()) %7B%0A                FileSystemResource fileSystemRes
 ource
-(
+ = 
 new 
@@ -1728,18 +1728,16 @@
 ES_FILE)
-))
 ;%0A      
@@ -1742,16 +1742,20 @@
         
+ 
+   
 property
@@ -1770,16 +1770,64 @@
 addLast(
+new ResourcePropertySource(fileSystemResource));
 %0A       
@@ -1822,32 +1822,35 @@
 e));%0A           
+ %7D%0A
          new Res
@@ -1846,35 +1846,93 @@
     
-new ResourcePropertyS
+   if (YAML_FILE.exists()) %7B%0A                FileSystemResource fileSystemRes
 ource
-(
+ = 
 new 
@@ -1960,20 +1960,120 @@
 ML_FILE)
-))
 ;%0A
+                propertySources.addLast(new ResourcePropertySource(fileSystemResource));%0A            %7D
 %0A       
@@ -2122,18 +2122,29 @@
 g.warn(%22
-No
+Couldn't load
  propert
@@ -2155,66 +2155,49 @@
 file
- with allowed extension found in %7B%7D%22, PROPERTIES_FIL
+(s) %7B%7D.yml/properties%22, FILE_BAS
 E_PATH
+, e
 );%0A 

Improve error handling in `HomeDirPropertyResolver`