package com.basgeekball.awesomevalidation.validators;

import android.content.Context;
import android.graphics.PorterDuff;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.basgeekball.awesomevalidation.ValidationHolder;
import com.basgeekball.awesomevalidation.utility.ValidationCallback;
import com.basgeekball.awesomevalidation.utility.ViewsInfo;

import java.util.ArrayList;
import java.util.regex.Matcher;

public class UnderlabelValidator extends Validator {

    private Context mContext;
    private ArrayList<ViewsInfo> mViewsInfos = new ArrayList<>();
    private int mColor;
    private boolean mHasFailed = false;

    public void setContext(Context context) {
        mContext = context;
    }

    @Override
    public boolean trigger() {
        halt();
        mColor = mContext.getResources().getColor(android.R.color.holo_red_light);
        return checkFields(new ValidationCallback() {
            @Override
            public void execute(ValidationHolder validationHolder, Matcher matcher) {
                EditText editText = validationHolder.getEditText();
                ViewGroup parent = (ViewGroup) editText.getParent();
                int index = parent.indexOfChild(editText);
                LinearLayout newContainer = new LinearLayout(mContext);
                newContainer.setLayoutParams(editText.getLayoutParams());
                newContainer.setOrientation(LinearLayout.VERTICAL);
                TextView textView = new TextView(mContext);
                textView.setText(validationHolder.getErrMsg());
                textView.setTextColor(mColor);
                textView.setPadding(editText.getPaddingLeft(), 0, editText.getPaddingRight(), 0);
                textView.startAnimation(AnimationUtils.loadAnimation(mContext, android.R.anim.fade_in));
                parent.removeView(editText);
                newContainer.addView(editText);
                newContainer.addView(textView);
                parent.addView(newContainer, index);
                mViewsInfos.add(new ViewsInfo(index, parent, newContainer, editText));
                if (!mHasFailed) {
                    textView.setFocusable(true);
                    textView.setFocusableInTouchMode(true);
                    textView.setClickable(true);
                    textView.requestFocus();
                    mHasFailed = true;
                }
                editText.getBackground().setColorFilter(mColor, PorterDuff.Mode.SRC_IN);
            }
        });
    }

    @Override
    public void halt() {
        for (ViewsInfo viewsInfo : mViewsInfos) {
            viewsInfo.restoreViews();
        }
        if (mValidationHolderList.size() > 0) {
            mValidationHolderList.get(0).getEditText().requestFocus();
        }
        mViewsInfos.clear();
        mHasFailed = false;
    }

}
@@ -1166,1054 +1166,56 @@
     
-EditText editText = validationHolder.getEditText();%0A                ViewGroup parent = (ViewGroup) editText.getParent();%0A                int index = parent.indexOfChild(editText);%0A                LinearLayout newContainer = new LinearLayout(mContext);%0A                newContainer.setLayoutParams(editText.getLayoutParams());%0A                newContainer.setOrientation(LinearLayout.VERTICAL);%0A                TextView textView = new TextView(mContext);%0A                textView.setText(validationHolder.getErrMsg());%0A                textView.setTextColor(mColor);%0A                textView.setPadding(editText.getPaddingLeft(), 0, editText.getPaddingRight(), 0);%0A                textView.startAnimation(AnimationUtils.loadAnimation(mContext, android.R.anim.fade_in));%0A                parent.removeView(editText);%0A                newContainer.addView(editText);%0A                newContainer.addView(textView);%0A                parent.addView(newContainer, index);%0A                mViewsInfos.add(new ViewsInfo(index, parent, newContainer, editText)
+TextView textView = replaceView(validationHolder
 );%0A 
@@ -1528,16 +1528,38 @@
     
-e
+validationHolder.getE
 ditText
+()
 .get
@@ -1943,24 +1943,24 @@
 os.clear();%0A
-
         mHas
@@ -1982,9 +1982,1048 @@
 %0A    %7D%0A%0A
+    private TextView replaceView(ValidationHolder validationHolder) %7B%0A        EditText editText = validationHolder.getEditText();%0A        ViewGroup parent = (ViewGroup) editText.getParent();%0A        int index = parent.indexOfChild(editText);%0A        LinearLayout newContainer = new LinearLayout(mContext);%0A        newContainer.setLayoutParams(editText.getLayoutParams());%0A        newContainer.setOrientation(LinearLayout.VERTICAL);%0A        TextView textView = new TextView(mContext);%0A        textView.setText(validationHolder.getErrMsg());%0A        textView.setTextColor(mColor);%0A        textView.setPadding(editText.getPaddingLeft(), 0, editText.getPaddingRight(), 0);%0A        textView.startAnimation(AnimationUtils.loadAnimation(mContext, android.R.anim.fade_in));%0A        parent.removeView(editText);%0A        newContainer.addView(editText);%0A        newContainer.addView(textView);%0A        parent.addView(newContainer, index);%0A        mViewsInfos.add(new ViewsInfo(index, parent, newContainer, editText));%0A        return textView;%0A    %7D%0A%0A
 %7D

Refactor UnderlabelValidator