package org.dainst.chronontology.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;

/**
 * @author Daniel M. de Oliveira
 */
public class ControllerConfig extends Config {

    static final String MSG_MUST_TYPE_ES= "datastores.0 must be of type \""+
        ConfigConstants.DATASTORE_TYPE_ES+"\".";
    static final String MSG_ES_CLASH= "When both datastores use the same elasticsearch url, the index names must be different.";

    private DatastoreConfig[] datastoreConfigs = new DatastoreConfig[2];
    private boolean useConnect = true;

    private Properties props= null;

    @Override
    public boolean validate(Properties props) {
        this.props= props;
        return (
            _validate(props,"useConnect","true")
        );
    }

    @Override
    public ArrayList<String> getConstraintViolations() {
        ArrayList<String> allViolations= new ArrayList<String>();
        allViolations.addAll(constraintViolations);
        for (DatastoreConfig config: Arrays.asList(datastoreConfigs)) {
            if (config!=null)
                allViolations.addAll(config.getConstraintViolations());
        }
        return allViolations;
    }

    private boolean esConfigsClash() {
        // TODO implement equals
        return ((datastoreConfigs[0].getType().equals(datastoreConfigs[1].getType()))
                && (datastoreConfigs[0].getUrl().equals(datastoreConfigs[1].getUrl()))
                && (datastoreConfigs[0].getIndexName().equals(datastoreConfigs[1].getIndexName())));
    }

    private void validateFirstDatastore() {

        datastoreConfigs[0]= new DatastoreConfig("0");
        if (!datastoreConfigs[0].validate(props)) throw new ConfigValidationException();

        if (!datastoreConfigs[0].getType().equals(ConfigConstants.DATASTORE_TYPE_ES)) {
            throw new ConfigValidationException(MSG_MUST_TYPE_ES);
        }
    }

    private boolean validateSecondDatastore() {
        datastoreConfigs[1]= new DatastoreConfig("1");
        return (datastoreConfigs[1].validate(props));
    }


    void setUseConnect(String useIt) {

        validateFirstDatastore();

        if (useIt.equals("false")) useConnect = false;
        if (useConnect) {
            if (!validateSecondDatastore()) throw new ConfigValidationException();
            if (esConfigsClash()) {
                throw new ConfigValidationException(MSG_ES_CLASH);
            }
        }
    }

    public boolean isUseConnect() {
        return useConnect;
    }

    public DatastoreConfig[] getDatastoreConfigs() {
        return datastoreConfigs;
    }
}

@@ -1566,37 +1566,32 @@
 te void validate
-First
 Datastore() %7B%0A%0A 
@@ -1584,21 +1584,30 @@
 tastore(
+Integer nr
 ) %7B%0A
-%0A
         
@@ -1623,17 +1623,18 @@
 Configs%5B
-0
+nr
 %5D= new D
@@ -1652,11 +1652,21 @@
 fig(
-%220%22
+nr.toString()
 );%0A 
@@ -1694,17 +1694,18 @@
 Configs%5B
-0
+nr
 %5D.valida
@@ -1750,24 +1750,101 @@
 xception();%0A
+    %7D%0A%0A%0A    void setUseConnect(String useIt) %7B%0A%0A        validateDatastore(0);
 %0A        if 
@@ -1917,18 +1917,16 @@
 YPE_ES))
- %7B
 %0A       
@@ -1989,342 +1989,43 @@
 S);%0A
-        %7D
 %0A    
-%7D%0A%0A
     
-private boolean validateSecondDatastore() %7B%0A        datastoreConfigs%5B1%5D= new DatastoreConfig(%221%22);%0A        return (datastoreConfigs%5B1%5D.validate(props));%0A    %7D%0A%0A%0A    void setUseConnect(String useIt) %7B%0A%0A        validateFirstDatastore();%0A%0A        if (useIt.equals(%22false%22)) useConnect = false;%0A        if (useConnect
+if (!useIt.equals(%22false%22)
 ) %7B%0A
@@ -2040,21 +2040,16 @@
     
-if (!
 validate
 Seco
@@ -2048,14 +2048,8 @@
 date
-Second
 Data
@@ -2058,47 +2058,9 @@
 ore(
-)) throw new ConfigValidationException(
+1
 );%0A 
@@ -2095,26 +2095,8 @@
 h())
- %7B%0A               
  thr
@@ -2151,30 +2151,35 @@
         
-    %7D%0A
+%7D else%0A    
         
 %7D%0A    %7D%0A
@@ -2166,25 +2166,42 @@
             
-%7D
+useConnect= false;
 %0A    %7D%0A%0A    

Improve readability.