package kr.co.zumo.app.lifeplus.view.fragment.onboarding;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.view.Event;
import kr.co.zumo.app.lifeplus.view.presenter.Presenter;

/**
 * OnBoardingPagerAdapter
 * <pre>
 *  tutorial viewpager adapter 클래스
 * </pre>
 *
 * @author 하세미
 * @version 1.0
 * @history 하세미   [2018-09-18]   [최초 작성]
 * @since 2018-09-18
 */
public class OnBoardingPagerAdapter extends PagerAdapter {

  protected View view;
  protected View bottomBar;
  protected View tabLayout;
  protected LayoutInflater layoutInflater;
  private Presenter presenter;
  private static final int VIEW_COUNT = 5;

  /**
   * event 처리를 위한 presenter 설정
   *
   * @param presenter
   */
  public void setPresenter(Presenter presenter) {
    this.presenter = presenter;
  }

  public void setTabLayout(View tabLayout) {this.tabLayout = tabLayout;}

  public OnBoardingPagerAdapter(Context context) {
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }

  public void setBottomBar(View bottomBar) {
    this.bottomBar = bottomBar;
  }

  /**
   * viewPager의 item 갯수 return
   *
   * @return
   */
  @Override
  public int getCount() {
    return VIEW_COUNT;
  }

  /**
   * instantiateItem에서 생성한 객체를 이용할 것인지 여부 반환
   *
   * @param view
   * @param object
   * @return
   */
  @Override
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return view == object;

  }


  /**
   * viewPager에서 사용할 View 생성 및 등록
   *
   * @param container item이 포함되는 view
   * @param position  item 선택값
   * @return
   */
  @NonNull
  @Override
  public Object instantiateItem(@NonNull ViewGroup container, int position) {
    view = layoutInflater.inflate(R.layout.tutorial_layout, container, false);
    ImageView imageViewTutorial = (ImageView) view.findViewById(R.id.image_view_tutorial);
    TextView textViewTitle = (TextView) view.findViewById(R.id.text_view_tutorial_title);
    TextView textViewDetail = (TextView) view.findViewById(R.id.text_view_tutorial_detail);
    tabLayout.setVisibility(View.VISIBLE);

    switch (position) {
      case 0:
        imageViewTutorial.setImageResource(R.drawable.mockup_step_01);
        textViewTitle.setText(R.string.tutorial_title1);
        textViewDetail.setText(R.string.tutorial_title1_message);
        break;
      case 1:
        imageViewTutorial.setImageResource(R.drawable.mockup_step_02);
        textViewTitle.setText(R.string.tutorial_title2);
        textViewDetail.setText(R.string.tutorial_title2_message);
        break;
      case 2:
        imageViewTutorial.setImageResource(R.drawable.mockup_step_03);
        textViewTitle.setText(R.string.tutorial_title3);
        textViewDetail.setText(R.string.tutorial_title3_message);
        break;
      case 3:
        imageViewTutorial.setImageResource(R.drawable.mockup_step_04);
        textViewTitle.setText(R.string.tutorial_title4);
        textViewDetail.setText(R.string.tutorial_title4_message);
        break;
      case 4:
        //하단 바 사라짐
        view = layoutInflater.inflate(R.layout.tutorial_last, container, false);
        Button buttonStart = view.findViewById(R.id.button_start_main);
        buttonStart.setOnClickListener(v -> presenter.onEvent(new Event.Builder(Event.TUTORIAL_END).build()));
        break;
      default:
        break;

    }
    container.addView(view);
    return view;
  }

  /**
   * 화면에 보이지 않는 view 삭제
   *
   * @param container
   * @param position
   * @param object
   */
  @Override
  public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    container.removeView((View) object);
  }

  @Override
  public int getItemPosition(@NonNull Object object) {
    return POSITION_NONE;
  }
}

