package com.dinect.checker;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.dinect.checker.R;
import com.dinect.checker.SearchType;

public class StatedImageButton extends LinearLayout {
  private Context mContext;
  private View mRootView;

  private ImageButton mBtnImageState;
  private SearchType mCurrentButtonState = SearchType.CARD;
  private StatedImageButtonInteractorListener statedImageButtonInteractorListener;

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

  public SearchType getCurrentState() {
    return mCurrentButtonState;
  }

  public StatedImageButton(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init(context);
  }

  public StatedImageButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
  }

  private void init(Context context) {
    mContext = context;
    mRootView = inflate(mContext, R.layout.partial_buttons_layout, this);
    mBtnImageState = (ImageButton) mRootView.findViewById(R.id.btn_image_state);
    mBtnImageState.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (mCurrentButtonState == SearchType.CARD) {
          mCurrentButtonState = SearchType.PHONE_NUMBER;
        } else {
          mCurrentButtonState = SearchType.CARD;
        }
        setButtonState(mCurrentButtonState);
        if (statedImageButtonInteractorListener != null) {
          statedImageButtonInteractorListener.onStateChanged(mCurrentButtonState);
        }
      }
    });
  }

  public void setButtonState(SearchType searchType) {
    mCurrentButtonState = searchType;

    switch (mCurrentButtonState) {
      case CARD:
        mBtnImageState.setBackground(getResources().getDrawable(R.drawable.ic_card));
        break;
      case PHONE_NUMBER:
        mBtnImageState.setBackground(getResources().getDrawable(R.drawable.ic_phone));
        break;
    }

  }

  public void setStatedImageButtonInteractorListener(StatedImageButtonInteractorListener statedImageButtonInteractorListener) {
    this.statedImageButtonInteractorListener = statedImageButtonInteractorListener;
  }

  public interface StatedImageButtonInteractorListener {
    void onStateChanged(SearchType searchType);
  }
}
