package com.wasu.widgets.adapter;

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

import com.wasu.widgets.focuswidget.FocusRecyclerView;


/**
 * Base RecyclerViewHolder
 * 这里实现了对ItemView的点击、长按、焦点监听和焦点绘制
 * Created by Danxingxi on 2016/3/31.
 */
public abstract class BaseFocusRecyclerViewHolder extends RecyclerView.ViewHolder{

    /**
     * 创建FocusRecyclerView的itemView
     *
     * @param itemView          item View
     * @param focusRecyclerView item parent ViewGroup(Parent must instanceof FocusRecyclerView)
     * @param mOnItemListener   item click、longClick、focusChange listener
     */
    public BaseFocusRecyclerViewHolder(final View itemView, final FocusRecyclerView focusRecyclerView, final BaseFocusRecyclerViewAdapter.OnItemListener mOnItemListener) {
        super(itemView);
        if (itemView == null || focusRecyclerView == null) {
            return;
        }
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemListener != null)
                    mOnItemListener.onItemClick(getAdapterPosition());
            }
        });
        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (mOnItemListener != null)
                    mOnItemListener.onItemLongClick(getAdapterPosition());
                return false;
            }
        });
        final float scaleValue = 1.06f;
        itemView.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    itemView.setScaleX(scaleValue);
                    itemView.setScaleY(scaleValue);
                    focusRecyclerView.modifyUI(view, getAdapterPosition());
                } else {
                    itemView.setScaleX(1.0f);
                    itemView.setScaleY(1.0f);
                    focusRecyclerView.modifyUI(null, getAdapterPosition());
                }
                if (mOnItemListener != null)
                    mOnItemListener.onItemFocusChange(itemView, getAdapterPosition(), hasFocus);
            }
        });
    }

    protected abstract View getView();

}
