package jwebform;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import jwebform.element.structure.Element;
import jwebform.element.structure.ElementResult;
import jwebform.element.structure.RenderInfos;
import jwebform.env.Env;
import jwebform.validation.FormValidationResult;
import jwebform.validation.FormValidator;
import jwebform.validation.ValidationResult;

// Represents a form
public class Form {

	private final List<Element> elements = new ArrayList<>();
	private final String id;
	private final List<FormValidator> formValidators = new ArrayList<>();

	protected String method = "POST";
	
	public Form(String id, List<Element> elements, List<FormValidator> formValidators) {
		this.elements.addAll(elements);
		this.id = id;
		this.formValidators.addAll(formValidators);
	}
	
	
	public FormResult run(Env env) {
		// validate form
		return new FormResult(this, checkIfValid(env), env);
	}



	private FormValidationResult checkIfValid(Env env) {
		// check each element
		boolean completeResult = true;
		int tabIndex = 1;
		Map<Element, ElementResult> elementResults = new LinkedHashMap<>();
		for (Element element : elements) {
			RenderInfos renderInfos = new RenderInfos(id, tabIndex, env, ValidationResult.undefined());
			ElementResult result = element.run(renderInfos);
			elementResults.put(element, result);
			if (result.getValidationResult() != ValidationResult.ok()) {
				completeResult = false;
			}
		}
		/*/
		for (Element element : elements) {
			if (element instanceof Validateable) {
				if (!((Validateable) element).getValidationResult().isValid) {
					completeResult = false;
					break;
				}
			}
		}
		*/
		// run the form-validators
		Map<Element, ValidationResult> overridenValidationResults = new LinkedHashMap<>();

		// TODO: If something breaks here then set the completeResult to false!
		for (FormValidator formValidator : formValidators) {
			overridenValidationResults.putAll(formValidator.validate(elementResults));
		}
		
		FormValidationResult formValidationResult = new FormValidationResult(completeResult, overridenValidationResults);
		return formValidationResult;
	}


	
	List<Element> getElements() {
		return elements;
	}

	
	public String getId() {
		return id;
	}

	public String getMethod() {
		return method;
	}

	public boolean isHtml5Validate() {
		return true; // TODO: Make this configurable
	}


}

@@ -1490,128 +1490,53 @@
 %7D%0A%09%09
-/*/%0A%09%09for (Element element : elements) %7B%0A%09%09%09if (element instanceof Validateable) %7B%0A%09%09%09%09if (!((Validateable) e
+%0A%09%09// run the form-validators%0A%09%09Map%3CE
 lement
-).get
+, 
 Vali
@@ -1551,102 +1551,108 @@
 sult
-().isValid) %7B%0A%09%09%09%09%09complete
+%3E overridenValidation
 Result
+s
  = 
-false;%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09
+new LinkedHashMap%3C%3E();%0A%0A
 %09%09
-%7D
 %0A%09%09
-%7D%0A%09%09*/%0A%09%09// run the
+for (FormValidator formValidator :
  form
--v
+V
 alid
@@ -1660,301 +1660,353 @@
 tors
-%0A%09%09Map%3CElement, ValidationResult%3E overridenValidationResults = new LinkedHashMap%3C%3E();%0A%0A%09%09// TODO: If something breaks here then set the completeResult to false!%0A%09%09for (FormValidator formValidator : formValidators) %7B%0A%09%09%09overridenValidationResults.putAll(formValidator.validate(elementResults));
+) %7B%0A%09%09%09overridenValidationResults.putAll(formValidator.validate(elementResults));%0A%09%09%7D%0A%09%09// If something breaks here then set the completeResult to false!%0A%09%09if (overridenValidationResults.size() %3E 0) %7B%0A%09%09%09completeResult = false;%09// TODO: This must not be true. Eventually some overrides goes from wrong to true. So make this algorith more clever!
 %0A%09%09%7D

remove unused comments