package com.ivanwooll.floatinglabelvalidator.lib;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * Created by Ivan on 15/03/2015.
 */
public class FloatingLabelTextView extends FrameLayout implements TextWatcher, View.OnFocusChangeListener {
    private EditText mEditText;
    private TextView mTextViewHintTop;
    private String validationMessage;
    private String hintText;
    private boolean allowEmpty;
    private int validatorType;
    private Validator validator;
    private boolean isEmpty;
    private int mMainTextSize;

    private int red;
    private int green;

    public FloatingLabelTextView(Context context) {
        super(context);
    }

    public FloatingLabelTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.FloatingLabelTextView, 0, 0);
        try {
            validationMessage = a.getString(R.styleable.FloatingLabelTextView_validationMessage);
            hintText = a.getString(R.styleable.FloatingLabelTextView_hint);
            allowEmpty = a.getBoolean(R.styleable.FloatingLabelTextView_allowEmpty, true);
            validatorType = a.getInt(R.styleable.FloatingLabelTextView_validatorType, -1);
            mMainTextSize = a.getInt(R.styleable.FloatingLabelTextView_textSize, 18);
        } finally {
            a.recycle();
        }
        init();
    }

    public FloatingLabelTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.floating_label_text_view, this);
        mTextViewHintTop = (TextView) findViewById(R.id.textViewHintTop);
        mEditText = (EditText) findViewById(R.id.editText);
        mEditText.setTextSize((float) mMainTextSize);
        mEditText.addTextChangedListener(this);
        mTextViewHintTop.setTextSize(mMainTextSize * .7f);
        mTextViewHintTop.setText(hintText);
        red = getResources().getColor(android.R.color.holo_red_light);
        green = getResources().getColor(android.R.color.holo_green_light);
        validator = new Validator(allowEmpty, validatorType);

        mTextViewHintTop.setY(70);
        mEditText.setOnFocusChangeListener(this);
        isEmpty = true;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        isEmpty = s.length() < 1;
        validationMessage = validator.validate(s);
        boolean isValid = TextUtils.isEmpty(validationMessage);
        mTextViewHintTop.setTextColor(isValid ? green : red);
        mTextViewHintTop.setText(isValid ? hintText + "" : hintText + " - " + validationMessage);
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            mTextViewHintTop.animate().translationY(0);
        } else {
            if (isEmpty) {
                mTextViewHintTop.animate().translationY(70);
            }

        }
    }

    public Editable getText() {
        return mEditText.getText();
    }
}

@@ -609,17 +609,18 @@
  String 
-v
+mV
 alidatio
@@ -648,17 +648,18 @@
  String 
-h
+mH
 intText;
@@ -679,17 +679,18 @@
 boolean 
-a
+mA
 llowEmpt
@@ -708,17 +708,18 @@
 ate int 
-v
+mV
 alidator
@@ -746,17 +746,18 @@
 lidator 
-v
+mV
 alidator
@@ -778,17 +778,18 @@
 boolean 
-i
+mI
 sEmpty;%0A
@@ -818,16 +818,46 @@
 extSize;
+%0A    private boolean mIsValid;
 %0A%0A    pr
@@ -1213,33 +1213,34 @@
 y %7B%0A            
-v
+mV
 alidationMessage
@@ -1320,17 +1320,18 @@
         
-h
+mH
 intText 
@@ -1397,17 +1397,18 @@
         
-a
+mA
 llowEmpt
@@ -1481,33 +1481,34 @@
 e);%0A            
-v
+mV
 alidatorType = a
@@ -2309,17 +2309,18 @@
 setText(
-h
+mH
 intText)
@@ -2471,25 +2471,26 @@
 t);%0A        
-v
+mV
 alidator = n
@@ -2502,17 +2502,18 @@
 lidator(
-a
+mA
 llowEmpt
@@ -2515,17 +2515,18 @@
 wEmpty, 
-v
+mV
 alidator
@@ -2618,33 +2618,34 @@
 (this);%0A        
-i
+mI
 sEmpty = true;%0A 
@@ -2861,17 +2861,18 @@
         
-i
+mI
 sEmpty =
@@ -2896,17 +2896,18 @@
         
-v
+mV
 alidatio
@@ -2917,17 +2917,18 @@
 ssage = 
-v
+mV
 alidator
@@ -2953,17 +2953,10 @@
     
-boolean i
+mI
 sVal
@@ -2978,17 +2978,18 @@
 isEmpty(
-v
+mV
 alidatio
@@ -3037,17 +3037,18 @@
 xtColor(
-i
+mI
 sValid ?
@@ -3095,17 +3095,18 @@
 setText(
-i
+mI
 sValid ?
@@ -3106,17 +3106,18 @@
 Valid ? 
-h
+mH
 intText 
@@ -3123,17 +3123,18 @@
  + %22%22 : 
-h
+mH
 intText 
@@ -3143,17 +3143,18 @@
 %22 - %22 + 
-v
+mV
 alidatio
@@ -3425,17 +3425,18 @@
     if (
-i
+mI
 sEmpty) 
@@ -3558,24 +3558,24 @@
 getText() %7B%0A
-
         retu
@@ -3603,11 +3603,74 @@
 );%0A    %7D
+%0A%0A    public boolean isValid() %7B%0A        return mIsValid;%0A    %7D
 %0A%7D%0A

Add isValid method