package com.android.launcher3.settings;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * Created by SKR-CYW on 2016/11/2.
 */

public class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected List<T> mList;
    protected int mItemViewId;
    protected MyViewHolder myViewHolder;
    protected Context mContext;
    protected Resources mResources;
    private OnItemClickListener mItemClickListener;
    private OnItemLongClickListener mItemLongClickListener;
    private OnLoadMoreListener mOnLoadMoreListener;
    //Healder and footer
    public static final int ITEM_TYPE_HEADER = 0;
    public static final int ITEM_TYPE_CONTENT = 1;
    public static final int ITEM_TYPE_FOOTER = 2;
    public static final int ITEM_TYPE_HEADER_COUNT = 3;
    protected int mHeaderCount = 0;//头部默认View个数
    protected int mFooterCount = 0;//底部默认View个数

    protected int mFirstItemPosition;
    protected int mLastItemPosition;

    public BaseRecyclerViewAdapter() {
    }

    public void setList(List<T> list) {
        this.mList = list;
    }

    public List<T> getList() {
        return mList;
    }


    public void setItemViewId(int id) {
        this.mItemViewId = id;
    }

    public void setFirstItemPosition(int position) {
        mFirstItemPosition = position;
    }

    public void setLastItemPosition(int position) {
        mLastItemPosition = position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();//该context为activity上下文对象
        mResources = mContext.getResources();
        switch (viewType) {
            case ITEM_TYPE_HEADER:
                return null;
            case ITEM_TYPE_CONTENT:
                MyViewHolder contentHolder = new MyViewHolder(LayoutInflater.from(mContext).inflate(mItemViewId, parent, false));
                setOnItemClickListener(contentHolder);
                setOnItemLongClickListener(contentHolder);
                return contentHolder;
            case ITEM_TYPE_FOOTER:
                return null;
            default:
                return null;
        }
    }

    private void setOnItemClickListener(final RecyclerView.ViewHolder holder) {
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (holder instanceof BaseRecyclerViewAdapter.MyViewHolder) {
                        mItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
                    }
                    if (holder instanceof BaseRecyclerViewAdapter.FooterViewHolder) {
                        if (mOnLoadMoreListener != null) {
                            hideFooter();
                            mOnLoadMoreListener.onLoadMore();
                        }
                    }
                }
            });
        }
    }

    private void hideFooter() {
        notifyItemRemoved(getItemCount());
    }

    private void setOnItemLongClickListener(final RecyclerView.ViewHolder holder) {
        if (mItemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mItemLongClickListener.onItemLongClick(holder.itemView, holder.getLayoutPosition());
                    return true;
                }
            });
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //holder instanceof MyViewHolder 报错：illegal generic type for instanceof
//        if (holder instanceof BaseRecyclerViewAdapter.MyViewHolder) {
//            myViewHolder = (MyViewHolder) holder;
//        }
    }

    /**
     * item 长度
     *
     * @return
     */
    public int getContentItemCount() {
        return mList.size();
    }

    /**
     * 判断当前item是否是hander
     *
     * @param position
     * @return
     */
    public boolean isHeaderView(int position) {
        return mHeaderCount != 0 && position < mHeaderCount;
    }

    /**
     * 判断当前item是否是footer
     *
     * @param position
     * @return
     */
    public boolean isFooterView(int position) {
        return mFooterCount != 0 && position >= (mHeaderCount + getContentItemCount());
    }


    @Override
    public int getItemViewType(int position) {
        if (mHeaderCount != 0 && position < mHeaderCount) {
            //头部View
            return ITEM_TYPE_HEADER;
        } else if (mFooterCount != 0 && position >= (mHeaderCount + getContentItemCount())) {
            //底部View
            return ITEM_TYPE_FOOTER;
        } else {
            //内容View
            return ITEM_TYPE_CONTENT;
        }
    }

    @Override
    public int getItemCount() {
        if (mList == null) {
            return 0;
        } else {
            return mHeaderCount + getContentItemCount() + mFooterCount;
        }
    }

    /**
     * 内容viewholder
     */
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        View mItemView;
        SparseArray<View> mViews;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.mItemView = itemView;
            mViews = new SparseArray<View>();
        }

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

    /**
     * 头部 ViewHolder
     */
    public static class HeaderViewHolder extends RecyclerView.ViewHolder {
        TextView tvFooter;

        public HeaderViewHolder(View itemView) {
            super(itemView);
//            tvFooter = (TextView) itemView.findViewById(R.id.tv_footer);
        }
    }

    /**
     * 底部 ViewHolder
     */
    public static class FooterViewHolder extends RecyclerView.ViewHolder {

        public FooterViewHolder(View itemView) {
            super(itemView);
        }
    }

    /**
     * *******************************对外接口*************************************
     * 点击，长按，加载更多
     */
    public interface OnItemClickListener {
        void onItemClick(View itemView, int position);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(View itemView, int position);
    }

    public interface OnLoadMoreListener {
        void onLoadMore();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.mItemLongClickListener = onItemLongClickListener;
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.mOnLoadMoreListener = onLoadMoreListener;
    }

}
