package com.devmarvel.creditcardentry.fields;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.Gravity;

import com.devmarvel.creditcardentry.R;
import com.devmarvel.creditcardentry.internal.CreditCardUtil;
import com.devmarvel.creditcardentry.library.CardType;

public class CreditCardText extends CreditEntryFieldBase {
	private CardType type;
	private String previousNumber;

	public CreditCardText(Context context) {
		super(context);
		init();
	}

	public CreditCardText(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

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

	@SuppressLint("RtlHardcoded")
	@Override
	void init() {
		super.init();
		setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
	}

	/* TextWatcher Implementation Methods */
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		previousNumber = s.toString();
	}

	@Override
	public void afterTextChanged(Editable s) {
		String number = s.toString();

		if (number.length() >= CreditCardUtil.CC_LEN_FOR_TYPE) {
			CardType type = CreditCardUtil.findCardType(number);

			if (type.equals(CardType.INVALID)) {
				this.removeTextChangedListener(this);
				this.setText(previousNumber);
				this.setSelection(3);
				this.addTextChangedListener(this);
				delegate.onBadInput(this);
				setValid(false);
				return;
			}

			if (this.type != type) {
				delegate.onCardTypeChange(type);
			}
			this.type = type;

			String formatted = CreditCardUtil.formatForViewing(number, type);

			if (!number.equalsIgnoreCase(formatted)) {
				this.removeTextChangedListener(this);
				this.setText(formatted);
				this.setSelection(formatted.length());
				this.addTextChangedListener(this);
			}

			if (formatted.length() >= CreditCardUtil
					.lengthOfFormattedStringForType(type)) {
				if (CreditCardUtil.isValidNumber(formatted)) {
					delegate.onCreditCardNumberValid();
					setValid(true);
				} else {
					delegate.onBadInput(this);
					setValid(false);
				}
			}

		} else {
			if (this.type != null) {
				this.type = null;
				delegate.onCardTypeChange(CardType.INVALID);
			}
		}
	}

	public CardType getType() {
		return type;
	}

	@Override
	public String helperText() {
		return context.getString(R.string.CreditCardNumberHelp);
	}
}

@@ -450,40 +450,8 @@
 ype;
-%0A%09private String previousNumber;
 %0A%0A%09p
@@ -923,34 +923,33 @@
 ds */%0A%09@Override
-%0A%09
+ 
 public void befo
@@ -1016,43 +1016,8 @@
 r) %7B
-%0A%09%09previousNumber = s.toString();%0A%09
 %7D%0A%0A%09
@@ -1102,17 +1102,16 @@
 ring();%0A
-%0A
 %09%09if (nu
@@ -1262,149 +1262,8 @@
 ) %7B%0A
-%09%09%09%09this.removeTextChangedListener(this);%0A%09%09%09%09this.setText(previousNumber);%0A%09%09%09%09this.setSelection(3);%0A%09%09%09%09this.addTextChangedListener(this);%0A
 %09%09%09%09
@@ -1489,17 +1489,16 @@
  type);%0A
-%0A
 %09%09%09if (!
@@ -1690,24 +1690,24 @@
 his);%0A%09%09%09%7D%0A%0A
+
 %09%09%09if (forma
@@ -1737,22 +1737,16 @@
 CardUtil
-%0A%09%09%09%09%09
 .lengthO

allow setting/typing invalid numbers