package com.chaoran.entiry;

import com.chaoran.imp.InputInterface;

import android.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;

public class SelfEditText extends EditText implements InputInterface {
	public String defaultValue = "";
	public String nextFocus;// 下一个组件焦点

	public String focusInFun;//
	public String focusOutFun;
	public boolean isNull;
	public String label;
	public boolean isTextChange = false;
	public boolean closeKeyBoard;
	public String showBorder="inset";
	private Paint paint;

	public SelfEditText(Context context) {
		super(context);
		setBackgroundResource(R.drawable.editbox_background);
		this.addTextChangedListener(textWatcher);
	}

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

	@Override
	protected void onDraw(Canvas canvas) {
		if (!showBorder.equals("none")) {
			if (paint == null) {
				paint = new Paint();
			}
			paint.setStyle(Style.STROKE);
			paint.setStrokeWidth(5);
			if (this.isFocused() == true){
				paint.setColor(Color.parseColor("#FFA500"));
			}else if (this.isEnabled()) {
				paint.setColor(Color.parseColor("#708090"));
				paint.setShadowLayer(5, 3, 3, Color.parseColor("#DCDCDC"));
			} else {
				paint.setColor(Color.parseColor("#DCDCDC"));
			}
			canvas.drawRoundRect(new RectF(this.getScrollX(),
					this.getScrollY(), this.getWidth() - 1 + this.getScrollX(),
					this.getHeight() + this.getScrollY() - 1), 3, 3, paint);
		}
		super.onDraw(canvas);

	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getNextFocus() {
		return nextFocus;
	}

	public void setNextFocus(String nextFocus) {
		this.nextFocus = nextFocus;
	}

	public String getFocusInFun() {
		return focusInFun;
	}

	public void setFocusInFun(String focusInFun) {
		this.focusInFun = focusInFun;
	}

	public String getFocusOutFun() {
		return focusOutFun;
	}

	public void setFocusOutFun(String focusOutFun) {
		this.focusOutFun = focusOutFun;
	}

	public boolean getIsNull() {
		return isNull;
	}

	public void setIsNull(boolean isNull) {
		this.isNull = isNull;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public boolean getIsTextChange() {
		return isTextChange;
	}

	public void setIsTextChange(boolean isTextChange) {
		this.isTextChange = isTextChange;
	}

	public boolean getCloseKeyBoard() {
		return closeKeyBoard;
	}

	public void setCloseKeyBoard(boolean closeKeyBoard) {
		this.closeKeyBoard = closeKeyBoard;
	}

	private TextWatcher textWatcher = new TextWatcher() {
		private int start = 0;
		private int length = 0;
		private boolean isCome = true;
		private boolean isGood = false;
		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			if (!isTextChange) {
				isTextChange = true;
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			if (/*"TIAOMA".equalsIgnoreCase(nextFocus) && start != 0  &&  */ after > 1 && isCome) {
				this.start = start;
				this.length = after;
				isGood = true;
			}
		}

		@Override
		public void afterTextChanged(Editable s) {
			if (isGood) {
				isGood = false;
				isCome = false;
				if (isNumeric(s.toString())) {
					CharSequence str = s.subSequence(start, start + length);
					s.clear();
					s.append(str);
				}
				isCome = true;
			}
		}
	};
	
	public static boolean isNumeric(String str) {
		for (int i = 0; i < str.length(); i++) {
			if (!Character.isLetterOrDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}
}
