/*
 * 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.ViewPager;
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.TutorialModel;
import kr.co.zumo.app.lifeplus.view.Event;
import kr.co.zumo.app.lifeplus.view.ITutorialView;
import kr.co.zumo.app.lifeplus.view.fragment.tutorial.TutorialPagerAdapter;
import kr.co.zumo.app.lifeplus.view.presenter.TutorialPresenter;

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

  private View view;
  private TutorialPagerAdapter tutorialPagerAdapter;
  private ViewPager viewPager;
  private TabLayout tabLayout;
  private TextView buttonTutorial1;
  private TextView buttonTutorial2;

  @Override
  void onAfterCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_tutorial);
    presenter = new TutorialPresenter(new TutorialModel(), this);
    view = (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) view.findViewById(R.id.button1_tutorial);
    buttonTutorial2 = (TextView) view.findViewById(R.id.button2_tutorial);

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


    tutorialPagerAdapter = new TutorialPagerAdapter(this);
    tutorialPagerAdapter.setPresenter(presenter);
    tutorialPagerAdapter.setBottomView(view);


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

      }

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

      @Override
      public void onPageScrollStateChanged(int state) {

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

  }

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

  @Override
  TutorialPresenter definePresenter() {
    return new TutorialPresenter(getModel(TutorialModel.class), this);
  }

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

}
