package com.jimind.sportdemo.base;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.LinkedList;
import java.util.List;

/**
 * ListView的通用适配器
 *
 * @param <T>
 * @author Any
 */
public abstract class BaseListAdapter<T> extends android.widget.BaseAdapter {
    protected final Context mContext;
    private LayoutInflater mInflater;
    private List<T> mData = new LinkedList<>();

    protected BaseListAdapter(Context context) {
        mContext=context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    final public int getCount() {
        return mData.size();
    }

    @Override
    final public T getItem(int position) {
        return mData.get(position);
    }

    /**
     * 设置数据
     */
    final public void setData(List<T> data, boolean reset) {
        if (reset) {
            mData.clear();
        }
        if (data != null) {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    @Override
    final public long getItemId(int position) {
        return position;
    }

    @Override
    final public View getView(int position, View convertView, ViewGroup parent) {
        int itemViewType = getItemViewType(position);
        T item = getItem(position);
        ViewHolder holder;
        boolean reused = false;
        if (convertView == null) {
            int itemLayoutId = getItemLayoutId(itemViewType);
            convertView = mInflater.inflate(itemLayoutId, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            reused = true;
            holder = (ViewHolder) convertView.getTag();
        }
        handleItem(itemViewType, position, item, holder, reused);
        return convertView;
    }

    /**
     * 返回视图类型
     *
     * @param itemViewType 视图类型
     * @return 布局id
     */
    protected abstract int getItemLayoutId(int itemViewType);

    /**
     * 处理item，主要是填充数据
     *
     * @param itemViewType item对应的视图类型
     * @param position     实体对应的索引位置
     * @param item         具体实体
     * @param holder       holder
     * @param reused       reused
     */
    protected abstract void handleItem(int itemViewType, int position, T item, ViewHolder holder, boolean reused);

    protected static class ViewHolder {
        private View mItemLayout;
        SparseArray<View> mViews = new SparseArray<>();

        protected ViewHolder(View itemLayout) {
            mItemLayout = itemLayout;
        }

        public View getItemLayout() {
            return mItemLayout;
        }

        public <T extends View> T get(int viewId) {
            View childView = mViews.get(viewId);
            if (childView == null) {
                childView = mItemLayout.findViewById(viewId);
                mViews.put(viewId, childView);
            }
            return (T) childView;
        }

        public <T extends View> T get(int viewId, Class<T> viewClass) {
            View childView = mViews.get(viewId);
            if (childView == null) {
                childView = mItemLayout.findViewById(viewId);
                mViews.put(viewId, childView);
            }
            return (T) childView;
        }
    }

}
