package com.sheep.gamegroup.view.adapter;

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


import com.sheep.gamegroup.util.ViewHolder;

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

/**
 * Created by ljy on 2018/3/20.
 */

public abstract class AdbCommonRecycler<T> extends RecyclerView.Adapter<ViewHolder> {
    Context context;
    List<T> listDatas = new ArrayList<T>();

    public AdbCommonRecycler(Context context) {
        this.context = context;
    }
    public AdbCommonRecycler(Context context, List<T> list) {
        this.context = context;
        listDatas = list;
    }
    public Context getContext(){
        return context;
    }

    public List<T> getListDatas(){
        return listDatas;
    }
    public void addAll(List<T> listDatas){
        this.listDatas.addAll(listDatas);
    }
    public void add(T t){
        this.listDatas.add(t);
    }

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

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

    public abstract int getViewIdByType(int type);

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

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

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        ViewHolder viewHolder = ViewHolder.get(context, null, viewGroup,
                getViewIdByType(i), -1);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.updatePosition(i);
        convert(viewHolder, listDatas.get(i));
    }

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