package com.yidianling.course.courseNew.sections;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yidianling.common.tools.LogUtil;
import com.yidianling.course.R;
import com.yidianling.course.courseNew.BannerBean;
import com.yidianling.course.courseNew.CourseHeadPagerAdapter;
import com.yidianling.ydlcommon.adapter.section.Section;
import com.yidianling.ydlcommon.view.LoopViewPager;

import java.util.List;

/**
 * Created by Jim on 2018/6/22 0022.
 * 轮播图模版
 */
public class BannerSection extends Section {

    private List<BannerBean> mBanners;
    private final int IDENTIFY_TYPE = BANNER_SECTION;

    public BannerSection(List<BannerBean> banners) {
        super(R.layout.item_course_banner);
        this.mBanners=banners;
    }

    @Override
    public void bindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof BannerViewHolder){
            ((BannerViewHolder) holder).notifyData(mBanners);
        }
    }

    @Override
    public int getTotalDataNum() {
        return 1;
    }

    @Override
    public int getItemViewType(int position) {
        return combineType(IDENTIFY_TYPE, TYPE_BODY);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(itemResourceId,parent,false);
        return new BannerViewHolder(view,mBanners);
    }

    @Override
    public boolean isBelongTo(int viewType) {
        return String.valueOf(viewType).startsWith(String.valueOf(IDENTIFY_TYPE));
    }

    @Override
    public boolean isHeadType(int position) {
        return false;
    }

    @Override
    public boolean isFooterType(int position) {
        return false;
    }

    @Override
    public int getSectionId() {
        return 0;
    }

    static class BannerViewHolder extends RecyclerView.ViewHolder {
        private LoopViewPager mHeadViewPager;
        private CourseHeadPagerAdapter mAdapter;

        public BannerViewHolder(View itemView,List<BannerBean> banners) {
            super(itemView);
            mHeadViewPager=itemView.findViewById(R.id.vp_head_bar);
            mAdapter=new CourseHeadPagerAdapter(banners,itemView.getContext());
            mHeadViewPager.setAdapter(mAdapter);
        }

        public void notifyData(List<BannerBean> banners){
            mAdapter=new CourseHeadPagerAdapter(banners,itemView.getContext());
            mHeadViewPager.setAdapter(mAdapter);
        }

    }

}
