package com.wasu.widgets.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.wasu.widgets.focuswidget.IRecyclerView;

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

/**
 * 支持FocusRecyclerView的设置headerView，多选模式，焦点监听，长按，单击和焦点框绘制
 * Created by Danxingxi on 2016/3/31.
 */
public abstract class BaseFocusRecyclerViewAdapterTv<T> extends RecyclerView.Adapter<BaseFocusRecyclerViewHolderTv> {
    public OnItemListener mOnItemListener;

    protected IRecyclerView tvRecyclerView;

//    private List<Model> mData = new ArrayList<Model>();
//
//    public void setData(List<? extends Model> data){
//        mData.clear();
//        mData.addAll(data);
//    }
    public BaseFocusRecyclerViewAdapterTv(IRecyclerView recyclerView) {
        if (recyclerView != null) {
            this.tvRecyclerView = recyclerView;
        } else {
            new IllegalArgumentException("Parent FocusRecycerView can't empty");
        }
    }

    private List<T> mData = new ArrayList<T>();

    /**
     * 首次设置数据
     * @param data
     */
    public void setData(List<T> data) {
        mData.clear();
        mData.addAll(data);
    }

    /**
     * 添加数据并通知更新
     * @param data
     */
    public void addData(List<T> data){
        mData.addAll(data);
        notifyDataSetChanged();
    }

    /**
     * 清楚所有数据
     */
    public void clearData() {
        if (mData != null) {
            mData.clear();
        }
    }

    public T getItemData(int position) {
        T res = null;
        /**计算正确的位置**/
//        int realPos = (mHeaderView == null ? position : position - 1);
        int realPos = position;
        if (realPos < mData.size()) {
            res = mData.get(realPos);
        }

        return res;
    }

    public void setOnItemListener(OnItemListener li) {
        mOnItemListener = li;
    }


    @Override
    public BaseFocusRecyclerViewHolderTv onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseFocusRecyclerViewHolderTv viewHolder = createItem(parent, viewType);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final BaseFocusRecyclerViewHolderTv holder, int position) {
        /**如果是header view就直接返回，不需要绑定数据**/
        bindData(holder, position);
    }

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


    /**
     * 创建item view
     *
     * @param parent
     * @param viewType
     * @return
     */
    protected abstract BaseFocusRecyclerViewHolderTv createItem(ViewGroup parent, int viewType);

    /**
     * 绑定数据
     *
     * @param holder
     * @param position
     */
    protected abstract void bindData(BaseFocusRecyclerViewHolderTv holder, int position);

    /**
     * item 点击事件接口
     *
     */
    public interface OnItemListener {
        /**
         * 单击监听
         **/
          void onItemClick(IRecyclerView parent, View itemView, int position);

//        /**
//         * 长按监听
//         **/
//        void onItemLongClick(int position);

        /**
         * 焦点监听
         **/
        void onItemFocusChanged(IRecyclerView parent, View itemView, int position, boolean hasFocus);
    }

    /**
     * 获取ItemView的parent也就是FocusRecyclerView
     * @return
     */
    protected IRecyclerView getItemParent(){
        return tvRecyclerView;
    }

    /**
     * 获取ItemView的监听器
     * @return
     */
    public OnItemListener getItemListener() {
        return mOnItemListener;
    }
}

