package kr.co.zumo.app.lifeplus.view.custom;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import kr.co.zumo.app.R;

/**
 * ClearEditText
 * <pre>
 *   custom clearEditText 구현
 * </pre>
 *
 * @author 하세미
 * @version 1.0
 * @history 하세미   [2018-09-17]   [최초 작성]
 * @since 2018-09-17
 */

// TODO: license 확인  http://gun0912.tistory.com/56
public class ClearEditText extends AppCompatEditText implements TextWatcher, View.OnTouchListener, View.OnFocusChangeListener {

  private Drawable clearDrawable;
  private OnFocusChangeListener onFocusChangeListener;
  private OnTouchListener onTouchListener;

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

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

  public ClearEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  @Override
  public void setOnFocusChangeListener(OnFocusChangeListener onFocusChangeListener) {
    this.onFocusChangeListener = onFocusChangeListener;
  }

  @Override
  public void setOnTouchListener(OnTouchListener onTouchListener) {
    this.onTouchListener = onTouchListener;
  }

  /**
   * clear icon setting 및 eventListener setting
   */
  public void init() {
    Drawable tempDrawable = ContextCompat.getDrawable(getContext(), R.drawable.icon_input_delete);
    clearDrawable = DrawableCompat.wrap(tempDrawable);
    //DrawableCompat.setTintList(clearDrawable, getHintTextColors());
    clearDrawable.setBounds(0, 0, clearDrawable.getIntrinsicWidth(), clearDrawable.getIntrinsicHeight());
    setClearIconVisible(false);

    super.setOnTouchListener(this);
    super.setOnFocusChangeListener(this);
    addTextChangedListener(this);
  }

  @Override
  public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

  }

  @Override
  public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    if (isFocused()) {
      setClearIconVisible(charSequence.length() > 0);
    }
  }

  @Override
  public void afterTextChanged(Editable editable) {

  }

  @Override
  public boolean onTouch(View view, MotionEvent motionEvent) {
    final int x = (int) motionEvent.getX();
    if (clearDrawable.isVisible() && x > getWidth() - getPaddingRight() - clearDrawable.getIntrinsicWidth()) {
      if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
        setError(null);
        setText(null);
      }
      return true;
    }

    if (onTouchListener != null) {
      return onTouchListener.onTouch(view, motionEvent);
    }
    else {
      return false;
    }
  }

  /**
   * icon visible 설정, icon이 보여야 할 경우 Edit Text의 오른쪽에 위치 시킨다.
   * @param visible
   */
  private void setClearIconVisible(boolean visible) {
    clearDrawable.setVisible(visible, false);
    setCompoundDrawables(null, null, visible ? clearDrawable : null, null);
  }

  @Override
  public void onFocusChange(View view, boolean hasFocus) {
    if (hasFocus) {
      setClearIconVisible(getText().length() > 0);
    }
    else {
      setClearIconVisible(false);
    }

    if (onFocusChangeListener != null) {
      onFocusChangeListener.onFocusChange(view, hasFocus);
    }
  }
}
