/*
 * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
 */
package kr.co.zumo.app.lifeplus.view.custom;

/**
 * CustomSpinner
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 10. 2.]   [최초 작성]
 * @since 2018. 10. 2.
 */

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Spinner;

public class CustomSpinner extends android.support.v7.widget.AppCompatSpinner {
  private OnSpinnerEventsListener mListener;
  private boolean mOpenInitiated = false;

  public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) {
    super(context, attrs, defStyleAttr, mode);
  }

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

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

  public CustomSpinner(Context context, int mode) {
    super(context, mode);
  }

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

  public interface OnSpinnerEventsListener {

    void onSpinnerOpened(Spinner spin);

    void onSpinnerClosed(Spinner spin);

  }

  @Override
  public boolean performClick() {
    // register that the Spinner was opened so we have a status
    // indicator for the activity(which may lose focus for some other
    // reasons)
    mOpenInitiated = true;
    if (mListener != null) {
      mListener.onSpinnerOpened(this);
    }
    return super.performClick();
  }

  public void setSpinnerEventsListener(OnSpinnerEventsListener onSpinnerEventsListener) {
    mListener = onSpinnerEventsListener;
  }

  /**
   * Propagate the closed Spinner event to the listener from outside.
   */
  public void performClosedEvent() {
    mOpenInitiated = false;
    if (mListener != null) {
      mListener.onSpinnerClosed(this);
    }
  }

  /**
   * A boolean flag indicating that the Spinner triggered an open event.
   *
   * @return true for opened Spinner
   */
  public boolean hasBeenOpened() {
    return mOpenInitiated;
  }

  @Override
  public void onWindowFocusChanged(boolean hasWindowFocus) {
    super.onWindowFocusChanged(hasWindowFocus);
    if (hasBeenOpened() && hasWindowFocus) {
      performClosedEvent();
    }
  }
}
