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

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import java.util.ArrayList;
import java.util.List;

import kr.co.zumo.app.lifeplus.activity.FAQActivity;
import kr.co.zumo.app.lifeplus.bean.api.FAQBean;

/**
 * FaqPagerAdapter
 * <pre>
 * </pre>
 *
 * @author 하세미
 * @version 1.0
 * @history 하세미   [2018-10-01]   [최초 작성]
 * @since 2018-10-01
 */
public class FAQPagerAdapter extends FragmentStatePagerAdapter implements FAQFragment.IFAQFragmentListener {

  private String[] tab;
  private static final int PAGE_COUNT = 4;
  private List<FAQBean> faqBeans;

  public FAQPagerAdapter(FragmentManager fm, String[] tab, List<FAQBean> beans) {
    super(fm);

    this.tab = tab;
    this.faqBeans = beans;
  }

  @Override
  public Fragment getItem(int position) {
    // position 에 매칭되는 카테고리 인덱스

    FAQFragment faqFragment = FAQFragment.newInstance(position);
    faqFragment.setListener(this);
    return faqFragment;
  }

  @Override
  public int getCount() {
    return PAGE_COUNT;
  }

  @Nullable
  @Override
  public CharSequence getPageTitle(int position) {
    return tab[position];
  }

  @Override
  public void onInit(FAQFragment faqFragment, int category) {
    /**
     * 카테고리 별로 데이터를 구분해서 전달한다.
     */

    List<FAQBean> beans = new ArrayList<>();
    if (category == FAQActivity.CATEGORY_KEY_ALL) {
      beans = faqBeans;
    }
    else {
      String categoryName = tab[category];

      for (FAQBean faqBean : faqBeans) {
        if (faqBean.getCategory().equals(categoryName)) {
          beans.add(faqBean);
        }
      }
    }

    faqFragment.init(beans);
  }
}
