package com.strohwitwer.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.strohwitwer.awesomevalidation.ValidationHolder;
import com.strohwitwer.awesomevalidation.utility.ValidationCallback;
import com.strohwitwer.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;
    private TextView mFocusedTextView;

    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));
                editText.getBackground().setColorFilter(mColor, PorterDuff.Mode.SRC_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);
                    mFocusedTextView = textView;
                    mHasFailed = true;
                }
                editText.requestFocus();
                mFocusedTextView.requestFocus();
            }
        });
    }

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

}
@@ -761,47 +761,8 @@
 lse;
-%0A    private TextView mFocusedTextView;
 %0A%0A  
@@ -1934,97 +1934,8 @@
 ));%0A
-                editText.getBackground().setColorFilter(mColor, PorterDuff.Mode.SRC_IN);%0A
     
@@ -1967,32 +1967,32 @@
 View(editText);%0A
+
                 
@@ -2428,35 +2428,31 @@
     
-mFocusedTextView = textView
+textView.requestFocus()
 ;%0A  
@@ -2535,70 +2535,69 @@
 ext.
-requestFocus();%0A                mFocusedTextView.requestFocus(
+getBackground().setColorFilter(mColor, PorterDuff.Mode.SRC_IN
 );%0A 
@@ -2919,24 +2919,24 @@
 os.clear();%0A
+
         mHas
@@ -2955,41 +2955,8 @@
 se;%0A
-        mFocusedTextView = null;%0A
     

Fix focus and underline fail color issue.