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

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.manager.ActionBarManager;
import kr.co.zumo.app.lifeplus.model.BlankModel;
import kr.co.zumo.app.lifeplus.view.Event;
import kr.co.zumo.app.lifeplus.view.IOnBoardingView;
import kr.co.zumo.app.lifeplus.view.fragment.onboarding.OnBoardingPagerAdapter;
import kr.co.zumo.app.lifeplus.view.presenter.OnBoardingPresenter;

/**
 * OnBoardingActivity
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 9. 19.]   [최초 작성]
 * @since 2018. 9. 19.
 */
public class OnBoardingActivity extends ActivityBase<OnBoardingPresenter> implements IOnBoardingView {

  private View bottomBar;
  private PagerAdapter pagerAdapter;
  private ViewPager viewPager;
  private TabLayout tabLayout;
  private TextView buttonTutorial1;
  private TextView buttonTutorial2;

  @Override
  void onAfterCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_tutorial);

    bottomBar = (View) findViewById(R.id.tutorial_bottom_layout);
    viewPager = (ViewPager) findViewById(R.id.tutorial_view_pager);
    tabLayout = (TabLayout) findViewById(R.id.tutorial_tab_layout);
    buttonTutorial1 = (TextView) bottomBar.findViewById(R.id.button1_tutorial);
    buttonTutorial2 = (TextView) bottomBar.findViewById(R.id.button2_tutorial);

    viewPager = findViewById(R.id.tutorial_view_pager);
    tabLayout = findViewById(R.id.tutorial_tab_layout);

    OnBoardingPagerAdapter onBoardingPagerAdapter = presenter.getPagerAdapter(this);
    onBoardingPagerAdapter.setPresenter(presenter);
    onBoardingPagerAdapter.setBottomBar(bottomBar);
    onBoardingPagerAdapter.setTabLayout(tabLayout);

    pagerAdapter = onBoardingPagerAdapter;

    // 1개 일 때 인디케이터 표시하지 않기
//    if (pagerAdapter.getCount() < 2) {
//    }

    tabLayout.setupWithViewPager(viewPager, true);
    viewPager.setAdapter(pagerAdapter);
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

      }

      public void onPageSelected(int position) {
        bottomBar.setVisibility(position != 4 ? View.VISIBLE : View.INVISIBLE);
      }

      @Override
      public void onPageScrollStateChanged(int state) {
        Log.w("APP# OnBoardingActivity | onPageScrollStateChanged", "|" + state);

      }
    });
    buttonTutorial1.setOnClickListener(v -> presenter.onEvent(new Event.Builder(Event.TUTORIAL_LEFT).build()));
    buttonTutorial2.setOnClickListener(v -> presenter.onEvent(new Event.Builder(Event.TUTORIAL_RIGHT).build()));

  }

  @Override
  protected void defineActionBar() {
    ActionBarManager actionBarManager = ActionBarManager.getInstance();
    actionBarManager.hide();
  }

  @Override
  OnBoardingPresenter definePresenter() {
    return new OnBoardingPresenter(getModel(BlankModel.class), this);
  }

  @Override
  public void onNextButtonClick() {
    viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
  }

}
