/**
 *
 */
package fr.emse.gitlab.saref.entities.tests;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

/**
 * @author Omar Qawasmeh
 *
 *
 */
@XmlRootElement(name = "testsuites")
@XmlType(propOrder = { "name", "tests", "failures", "errors", "testSuitesList" })
public class TestSuites implements Serializable {

	@XmlAttribute
	String name = "";

	@XmlAttribute
	int tests = 0;

	@XmlAttribute
	int failures = 0;
	
	@XmlAttribute
	int errors = 0;
	
	@XmlElement(name="testsuite")
	private List<TestSuite> testSuitesList = new ArrayList<>();

	private transient Map<String, TestSuite> testSuitesMap;
	
	public TestSuites() {}
	
	public TestSuites(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}

	public int getFailures() {
		return failures;
	}

	public int getErrors() {
		return errors;
	}

	public TestSuite get(String name) {
		if(testSuitesMap == null) {
			testSuitesMap = new HashMap<>();
			for(TestSuite testSuite : testSuitesList) {
				testSuitesMap.put(testSuite.getName(), testSuite);
			}
		}
		TestSuite testSuite = testSuitesMap.get(name);
		if(testSuite == null) {
			testSuite = new TestSuite(this, name);
			testSuitesMap.put(name, testSuite);
			testSuitesList.add(testSuite);
		}
		return testSuite;
	}

	public void prune() {
		testSuitesList.removeIf((ts) -> {
			return ts.getErrors() + ts.getFailures() == 0;
		});		
		testSuitesMap.values().removeIf((ts) -> {
			return ts.getErrors() + ts.getFailures() == 0;
		});		
	}

}