package com.cygnusa.hyperm.util;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

public class MyEditText extends EditText {
	@SuppressWarnings("deprecation")
	private final Drawable imgCloseButton = getResources().getDrawable(
			android.R.drawable.ic_menu_close_clear_cancel);

	Context context;
	int position = 0;

	public MyEditText(Context context) {
		super(context);
		this.context = context;
		init(null);
	}

	public MyEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		init(attrs);
	}

	public MyEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init(attrs);
	}

	void init(AttributeSet attributeSet) {

		// Set bounds of the Clear button so it will look ok
		imgCloseButton.setBounds(0, 0, imgCloseButton.getIntrinsicWidth(),
				imgCloseButton.getIntrinsicHeight());

		// There may be initial text in the field, so we may need to display the
		// button
		handleClearButton();

		// if the Close image is displayed and the user remove his finger from
		// the button, clear it. Otherwise do nothing
		this.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {

				MyEditText et = MyEditText.this;

				if (et.getCompoundDrawables()[2] == null)
					return false;

				if (event.getAction() != MotionEvent.ACTION_UP)
					return false;

				if (event.getX() > et.getWidth() - et.getPaddingRight()
						- imgCloseButton.getIntrinsicWidth()) {
					et.setText("");
					MyEditText.this.handleClearButton();
				}
				return false;
			}
		});

		// if text changes, take care of the button
		this.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

				MyEditText.this.handleClearButton();
			}

			@Override
			public void afterTextChanged(Editable s) {

			}

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

	void handleClearButton() {
		if (this.hasFocus()) {
			if (this.getText().toString().equals("")) {
				this.setError(null);
				this.setCompoundDrawables(this.getCompoundDrawables()[0],
						this.getCompoundDrawables()[1], null,
						this.getCompoundDrawables()[3]);
			} else {
				this.setCompoundDrawables(this.getCompoundDrawables()[0],
						this.getCompoundDrawables()[1], imgCloseButton,
						this.getCompoundDrawables()[3]);
			}
		}

	}

	@Override
	protected void onFocusChanged(boolean focused, int direction,
			Rect previouslyFocusedRect) {
		super.onFocusChanged(focused, direction, previouslyFocusedRect);

		if (this.getText().toString().equals("")) {
			this.setCompoundDrawables(this.getCompoundDrawables()[0],
					this.getCompoundDrawables()[1], null,
					this.getCompoundDrawables()[3]);
		} else {
			if (focused) {
				this.setCompoundDrawables(this.getCompoundDrawables()[0],
						this.getCompoundDrawables()[1], imgCloseButton,
						this.getCompoundDrawables()[3]);
			} else {
				this.setCompoundDrawables(this.getCompoundDrawables()[0],
						this.getCompoundDrawables()[1], null,
						this.getCompoundDrawables()[3]);

			}

		}

	}

}