package br.com.bloder.blormlib;

import android.view.View;

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

import br.com.bloder.blormlib.validation.Validate;
import br.com.bloder.blormlib.validation.Validation;

/**
 * Created by bloder on 05/06/16.
 */
public class Blorm {

  public static class Builder {

    private List<Validate> validations;
    private View field;

    public Builder() {
      validations = new ArrayList<>();
    }

    public Builder field(View field) {
      this.field = field;
      return this;
    }

    public Builder is(Validate validation) {
      this.validations.add(validation);
      return this;
    }

    public Builder is(Validation validation) {
      validation.field = this.field;
      this.validations.add(validation.validate());
      return this;
    }

    public Builder is(String errorMessgae, Validation validation) {
      validation.field = this.field;
      validation.errorMessage = errorMessgae;
      this.validations.add(validation.validate());
      return this;
    }

    public void onSubmit(View submittedItem) {
      submittedItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          new Blorm(validations).onSubmitted();
        }
      });
    }
  }

  private List<Validate> validations;
  
  public Blorm(List<Validate> validations) {
    this.validations = validations;
  }

  private void onSubmitted() {
    for(Validate validate : validations) {
      if(!validate.validate()) {
        validate.onError();
      }
    }
  }
}

@@ -105,16 +105,65 @@
 .List;%0A%0A
+import br.com.bloder.blormlib.validation.Action;%0A
 import b
@@ -419,16 +419,46 @@
 w field;
+%0A    private Action onSuccess;
 %0A%0A    pu
@@ -512,32 +512,135 @@
 ist%3C%3E();%0A    %7D%0A%0A
+    public Builder onSuccess(Action action) %7B%0A      this.onSuccess = action;%0A      return this;%0A    %7D%0A%0A
     public Build
@@ -1416,16 +1416,27 @@
 idations
+, onSuccess
 ).onSubm
@@ -1513,16 +1513,91 @@
 ations;%0A
+  private Action onSuccess;%0A  private Boolean allValidationsPassed = true;%0A
   %0A  pub
@@ -1624,32 +1624,50 @@
 ate%3E validations
+, Action onSuccess
 ) %7B%0A    this.val
@@ -1690,16 +1690,48 @@
 ations;%0A
+    this.onSuccess = onSuccess;%0A
   %7D%0A%0A  p
@@ -1846,35 +1846,139 @@
     
-validate.onError();%0A      %7D
+allValidationsPassed = false;%0A        validate.onError();%0A      %7D%0A    %7D%0A    if(allValidationsPassed) %7B%0A      this.onSuccess.call();
 %0A   

Add validation success logic in blorm class