package com.kfzs.duanduan.adp;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.kfzs.appstore.utils.adapter.recyclerview.ViewHolder;
import com.sheep.gamegroup.util.ListUtil;

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

/**
 * @ Created by Dlg
 * @ <p>TiTle:  AdpCommonRecy</p>
 * @ <p>Description:</p>
 * @ date:  2017/6/15 11:36
 * @ QQ:    315096953
 */

public abstract class AdpCommonRecy<T> extends RecyclerView.Adapter<ViewHolder> {
    protected Context mContext;
    protected List<T> mListDatas = new ArrayList<>();

    public AdpCommonRecy(Context context) {
        mContext = context;
    }

    public Context getmContext() {
        return mContext;
    }

    public List<T> getmListDatas() {
        return mListDatas;
    }

    public void addAll(List<T> mListDatas) {
        this.mListDatas.addAll(mListDatas);
    }

    public void add(T t) {
        this.mListDatas.add(t);
    }

    public void add(T t, int position) {
        ListUtil.set(this.mListDatas, t, position);
    }

    public void clear() {
        this.mListDatas.clear();
    }

    public abstract int getViewIdByType(int viewType);

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        ViewHolder viewHolder = ViewHolder.get(mContext, null, parent,
                getViewIdByType(viewType), -1);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.updatePosition(position);
        convert(holder, mListDatas.get(position));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            holder.updatePosition(position);
            convert(holder, mListDatas.get(position));
        }
    }



    public abstract void convert(ViewHolder holder, T t);

    @Override
    public int getItemCount() {
        return mListDatas.size();
    }

}
