package jwebform;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import jwebform.element.structure.Element;
import jwebform.element.structure.ElementContainer;
import jwebform.env.Env;
import jwebform.processors.Processor;
import jwebform.validation.FormValidator;

// Represents a form
public final class Form {

  private final List<ElementContainer> elements;
  private final String id;
  private final List<FormValidator> formValidators;
  private final Processor processor;

  // Constructors

  // full
  public Form(String id, List<ElementContainer> elements, List<FormValidator> formValidators) {
    this.elements = elements;
    this.id = id;
    this.formValidators = formValidators;
    this.processor = new Processor();
  }

  public Form(String id, List<ElementContainer> elements) {
    this(id, elements, new ArrayList<>());
  }


  public Form(String id, ElementContainer... elements) {
    this(id, new ArrayList<>(), elements);
  }

  public Form(String id, Element... elements) {
    this(id, packElementsInContainer(elements), new ArrayList<>());
  }


  public Form(String id, List<FormValidator> formValidators, ElementContainer... elements) {
    this(id, Arrays.asList(elements), formValidators);
  }

  public Form(String id, List<FormValidator> formValidators, Element... elements) {
    this(id, packElementsInContainer(elements), formValidators);
  }
  // End constructors

  // process each element, run validations
  public final FormResult run(Env env) {
    return processor.run(env, id, elements, formValidators);
  }



  private static List<ElementContainer> packElementsInContainer(Element... elements) {
    List<ElementContainer> ec = new ArrayList<>();
    for (int i = 0; i < elements.length; i++) {
      ec.add(new ElementContainer(elements[i]));
    }
    return ec;
  }


  public final List<ElementContainer> getElements() {
    return elements;
  }

  public final String getId() {
    return id;
  }

 

}

@@ -625,20 +625,47 @@
 lidators
+,%0A      Processor processor
 ) %7B%0A
-
     this
@@ -766,16 +766,167 @@
 cessor =
+ processor;%0A  %7D%0A%0A  public Form(String id, List%3CElementContainer%3E elements, List%3CFormValidator%3E formValidators) %7B%0A    this(id, elements, formValidators,
  new Pro
@@ -933,16 +933,17 @@
 cessor()
+)
 ;%0A  %7D%0A%0A 

allow Processor to override