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

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.bean.api.FAQBean;
import kr.co.zumo.app.lifeplus.util.ResourceUtil;

/**
 * FaqFragment
 * <pre>
 * </pre>
 *
 * @author 하세미
 * @version 1.0
 * @history 하세미   [2018-10-01]   [최초 작성]
 * @since 2018-10-01
 */
public class FAQFragment extends Fragment {

  private static final String PARAM_INDEX = "index";

  private RecyclerView expandableListViewFaq;

  private List<FAQBean> faqBeans;
  private IFAQFragmentListener listener;
  private int index;

  public void setListener(IFAQFragmentListener listener) {
    this.listener = listener;
  }

  public static FAQFragment newInstance(int index) {
    FAQFragment fragment = new FAQFragment();
    Bundle args = new Bundle();
    args.putInt(PARAM_INDEX, index);
    fragment.setArguments(args);
    return fragment;
  }

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.index = getArguments().getInt(PARAM_INDEX);
  }

  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_faq, container, false);
    return view;
  }

  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    listener.onInit(this, this.index);
  }

  /**
   * 초기화
   *
   * @param beans
   */
  public void init(List<FAQBean> beans) {
    View view = getView();
    expandableListViewFaq = view.findViewById(R.id.expandable_list_view_faq);
    expandableListViewFaq.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
    faqBeans = beans;

    FAQExpandableListViewAdapter adapter = new FAQExpandableListViewAdapter(faqBeans);
    expandableListViewFaq.setAdapter(adapter);
    expandableListViewFaq.addItemDecoration(new RecyclerView.ItemDecoration() {
      @Override
      public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.top = ResourceUtil.dpToPx(12);
      }
    });
  }

  interface IFAQFragmentListener {
    void onInit(FAQFragment faqFragment, int category);
  }
}
