package com.tofly.ghrq_route.common.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.lcp.arecyclerview.widget.ARecycleView;
import com.tofly.ghrq_route.R;
import java.util.List;

/**
 * Created by dhc on 16/6/23.
 */
public class JobListPageAdapter extends PagerAdapter {
    private Context mContext;
    private View views[];
    private List<String> mLabels;

    public JobListPageAdapter(Context context, List<String> labels){
        mContext = context;
        mLabels = labels;
        int pages = labels.size();
        views = new View[pages];
        for (int i = 0;  i < pages; i++){
            View rootView = View.inflate(context, R.layout.recycler_view_page, null);
            views[i] = rootView;
            if (rootView != null){
                ARecycleView recyclerView = rootView.findViewById(R.id.recycler_view);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
                recyclerView.setLayoutManager(linearLayoutManager);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
                Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.bg_recyclerview_divider);
                dividerItemDecoration.setDrawable(drawable);
                recyclerView.addItemDecoration(dividerItemDecoration);
            }
        }
    }

    public View getView(int postion){
        return views[postion];
    }

    public ARecycleView getRecycleView(int position){
        ARecycleView recyclerView = null;
        View view = views[position];
        if (view != null){
            recyclerView = view.findViewById(R.id.recycler_view);
        }
        return recyclerView;
    }


    @Override
    public int getCount() {
        return views.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView(views[position]);
    }

    public Object instantiateItem(View container, int position) {
        ((ViewPager) container).addView(views[position]);
        return views[position];
    }
}
