package de.retest.recheck;

import java.util.List;

import org.aeonbits.owner.Config.LoadPolicy;
import org.aeonbits.owner.Config.LoadType;
import org.aeonbits.owner.Config.Sources;
import org.aeonbits.owner.ConfigCache;
import org.aeonbits.owner.ConfigFactory;
import org.aeonbits.owner.Reloadable;

import de.retest.recheck.configuration.ProjectRootFinderUtil;
import de.retest.recheck.persistence.FileOutputFormat;

@LoadPolicy( LoadType.MERGE )
@Sources( { "system:properties", "file:${projectroot}/.retest/retest.properties" } )
public interface RecheckProperties extends Reloadable {

	/*
	 * Basic usage.
	 */

	static void init() {
		ProjectRootFinderUtil.getProjectRoot().ifPresent(
				projectRoot -> ConfigFactory.setProperty( "projectroot", projectRoot.toAbsolutePath().toString() ) );
	}

	static RecheckProperties getInstance() {
		final RecheckProperties instance = ConfigCache.getOrCreate( RecheckProperties.class );
		instance.reload();
		return instance;
	}

	/*
	 * Various constants.
	 */

	static final String PROPERTY_VALUE_SEPARATOR = ";";
	static final String ZIP_FOLDER_SEPARATOR = "/";

	static final String SCREENSHOT_FOLDER_NAME = "screenshot";
	static final String RECHECK_FOLDER_NAME = "recheck";
	static final String DEFAULT_XML_FILE_NAME = "retest.xml";

	static final String RETEST_FOLDER_NAME = ".retest";
	static final String RETEST_PROPERTIES_FILE_NAME = "retest.properties";

	static final String GOLDEN_MASTER_FILE_EXTENSION = ".recheck";
	static final String TEST_REPORT_FILE_EXTENSION = ".report";
	static final String AGGREGATED_TEST_REPORT_FILE_NAME = "tests" + TEST_REPORT_FILE_EXTENSION;

	/*
	 * Properties, their key constants and related functionality.
	 */

	static final String IGNORE_ATTRIBUTES_PROPERTY_KEY = "de.retest.recheck.ignore.attributes";

	@Key( IGNORE_ATTRIBUTES_PROPERTY_KEY )
	@DefaultValue( "" )
	@Separator( PROPERTY_VALUE_SEPARATOR )
	List<String> ignoreAttributes();

	static final String ELEMENT_MATCH_THRESHOLD_PROPERTY_KEY = "de.retest.recheck.elementMatchThreshold";

	@Key( ELEMENT_MATCH_THRESHOLD_PROPERTY_KEY )
	@DefaultValue( "0.3" )
	double elementMatchThreshold();

	static final String ROOT_ELEMENT_MATCH_THRESHOLD_PROPERTY_KEY = "de.retest.recheck.rootElementMatchThreshold";

	@Key( ROOT_ELEMENT_MATCH_THRESHOLD_PROPERTY_KEY )
	@DefaultValue( "0.8" )
	double rootElementMatchThreshold();

	static final String ROOT_ELEMENT_CONTAINED_CHILDREN_MATCH_THRESHOLD_PROPERTY_KEY =
			"de.retest.recheck.rootElementContainedChildrenMatchThreshold";

	@Key( ROOT_ELEMENT_CONTAINED_CHILDREN_MATCH_THRESHOLD_PROPERTY_KEY )
	@DefaultValue( "0.5" )
	double rootElementContainedChildrenMatchThreshold();

	static final String REHUB_REPORT_UPLOAD_ENABLED_PROPERTY_KEY = "de.retest.recheck.rehub.reportUploadEnabled";

	@Key( REHUB_REPORT_UPLOAD_ENABLED_PROPERTY_KEY )
	@DefaultValue( "false" )
	boolean rehubReportUploadEnabled();

	static final String FILE_OUTPUT_FORMAT_PROPERTY_KEY = "de.retest.output.Format";

	@Key( FILE_OUTPUT_FORMAT_PROPERTY_KEY )
	FileOutputFormat fileOutputFormat();

	default FileOutputFormat getReportOutputFormat() {
		if ( rehubReportUploadEnabled() ) {
			return FileOutputFormat.CLOUD;
		}
		final FileOutputFormat format = fileOutputFormat();
		if ( format == null ) {
			return FileOutputFormat.KRYO;
		}
		return format;
	}

	default FileOutputFormat getStateOutputFormat() {
		final FileOutputFormat format = fileOutputFormat();
		return format == FileOutputFormat.ZIP ? format : FileOutputFormat.PLAIN;
	}

}

@@ -1000,37 +1000,24 @@
 nts.%0A%09 */%0A%0A%09
-static final 
 String PROPE
@@ -1040,37 +1040,24 @@
 TOR = %22;%22;%0A%09
-static final 
 String ZIP_F
@@ -1077,37 +1077,24 @@
 OR = %22/%22;%0A%0A%09
-static final 
 String SCREE
@@ -1124,37 +1124,24 @@
 reenshot%22;%0A%09
-static final 
 String RECHE
@@ -1165,37 +1165,24 @@
 %22recheck%22;%0A%09
-static final 
 String DEFAU
@@ -1212,37 +1212,24 @@
 est.xml%22;%0A%0A%09
-static final 
 String RETES
@@ -1252,37 +1252,24 @@
 %22.retest%22;%0A%09
-static final 
 String RETES
@@ -1312,37 +1312,24 @@
 perties%22;%0A%0A%09
-static final 
 String GOLDE
@@ -1363,37 +1363,24 @@
 .recheck%22;%0A%09
-static final 
 String TEST_
@@ -1415,29 +1415,16 @@
 port%22;%0A%09
-static final 
 String A
@@ -1570,29 +1570,16 @@
 %0A%09 */%0A%0A%09
-static final 
 String I
@@ -1783,37 +1783,24 @@
 ibutes();%0A%0A%09
-static final 
 String ELEME
@@ -1978,37 +1978,24 @@
 eshold();%0A%0A%09
-static final 
 String ROOT_
@@ -2191,37 +2191,24 @@
 eshold();%0A%0A%09
-static final 
 String ROOT_
@@ -2479,37 +2479,24 @@
 eshold();%0A%0A%09
-static final 
 String REHUB
@@ -2700,21 +2700,8 @@
 ;%0A%0A%09
-static final 
 Stri

Fix unecessary static final modifier in interfaces