package com.bc_chat.bc_base.adapter;

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

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

/**
 * author：Created by zhaohaoting on 2019/3/13
 * email：526309416@qq.com
 * desc：
 */
public abstract class BaseMessageAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    protected Context mContext;
    protected List<T> mList;

    public BaseMessageAdapter(Context context) {
        this(context, new ArrayList());
    }

    public BaseMessageAdapter(Context context, List<T> mList) {
        this.mContext = context;
        if (mList == null) {
            this.mList = new ArrayList<>();
        } else {
            this.mList = mList;
        }
    }

    protected <T extends View> T findViewById(View view, int id) {
        return view.findViewById(id);
    }

    public int findPosition(T message) {
        int index = this.getCount();
        int position = -1;

        while (index-- > 0) {
            if (message.equals(this.getItem(index))) {
                position = index;
                break;
            }
        }

        return position;
    }

    public int findPosition(long id) {
        int index = this.getCount();
        int position = -1;

        while (index-- > 0) {
            if (this.getItemId(index) == id) {
                position = index;
                break;
            }
        }

        return position;
    }

    public void addCollection(Collection<T> collection) {
        this.mList.addAll(collection);
    }

    public void addCollection( int index,Collection<T> collection) {
        this.mList.addAll(index,collection);
    }

    public void addCollection(T... collection) {
        Object[] var2 = collection;
        int var3 = collection.length;

        for (int var4 = 0; var4 < var3; ++var4) {
            T t = (T) var2[var4];
            this.mList.add(t);
        }

    }

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

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

    public void remove(int position) {
        this.mList.remove(position);
    }

    public void removeAll() {
        this.mList.clear();
    }

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

    public int getCount() {
        return this.mList == null ? 0 : this.mList.size();
    }

    public T getItem(int position) {
        if (this.mList == null) {
            return null;
        } else {
            return position >= this.mList.size() ? null : this.mList.get(position);
        }
    }

    /**
     * 占未处理带headview/footerview的情况
     * @return
     */
    public int getHeaderViewsCount() {
        return 0;
    }

    public int getFooterViewsCount() {
        return 0;
    }

}
