package com.wasu.widgets.adapter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.animation.LinearInterpolator;

import com.wasu.widgets.focuswidget.IRecyclerView;
import com.wasu.widgets.focuswidget.TvRecyclerView;
import com.wasu.widgets.tools.UIUtil;

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

    /**
     * 创建FocusRecyclerView的itemView
     *
     * @param itemView          item View
     * @param tvRecyclerView item parent ViewGroup(Parent must instanceof TvRecyclerView)
     * @param mOnItemListener   item click、longClick、focusChange listener
     */
    public BaseFocusRecyclerViewHolderTv(final View itemView, final IRecyclerView tvRecyclerView, final BaseFocusRecyclerViewAdapterTv.OnItemListener mOnItemListener) {
        super(itemView);
        if (itemView == null || tvRecyclerView == null) {
            return;
        }
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemListener != null)
                    mOnItemListener.onItemClick(tvRecyclerView, itemView, getAdapterPosition());
            }
        });
        itemView.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    itemView.setScaleX(1.1f);
                    itemView.setScaleY(1.1f);
                    //使用缩放动画需要注释上面的直接放大setScaleX/Y
//                    startScaleAnimation(view, tvRecyclerView);
                    tvRecyclerView.modifyUI(view, getAdapterPosition());
                    //局部刷新,如果使用的话就注释掉modifyUI中的全局刷新
//                    postInvalidatePart(view,tvRecyclerView);
                } else {
                    itemView.setScaleX(1.0f);
                    itemView.setScaleY(1.0f);
//                    startResetAnimation(view);
                    tvRecyclerView.modifyUI(null, getAdapterPosition());
                    //局部刷新
//                    postInvalidatePart(view,tvRecyclerView);
                }
                if (mOnItemListener != null)
                    mOnItemListener.onItemFocusChanged(tvRecyclerView, itemView, getAdapterPosition(), hasFocus);
            }
        });
    }

    protected abstract View getView();

    protected AnimatorSet animSet;
    private Rect toFocusedViewRect;

    /**
     * 放大动画
     * @param focusItem
     * @param parent
     */
    public void startScaleAnimation(final View focusItem, final TvRecyclerView parent) {

        if (focusItem == null) {
            return;
        }
        /**获取焦点itemView所在的绘制矩形**/
        toFocusedViewRect = UIUtil.createViewRect(parent, focusItem, 0);
        /**放大绘制矩形**/
        UIUtil.scaleRect(toFocusedViewRect,1.3f,1.3f);
        animSet = new AnimatorSet();
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(focusItem, "scaleX", 1.1f);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(focusItem, "scaleY", 1.1f);
        final LinearInterpolator interpolator = new LinearInterpolator();
        animatorX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //实现放大动画的局部刷新
                if(toFocusedViewRect!=null)
                    parent.postInvalidate(toFocusedViewRect.left,toFocusedViewRect.top,toFocusedViewRect.right,toFocusedViewRect.bottom);
            }
        });
        animSet.setInterpolator(interpolator);
        animSet.playTogether(animatorX, animatorY);
        animSet.setDuration(300);
        animSet.start();

    }
    protected AnimatorSet animSetReset;

    /**
     * 缩小动画
     * @param resetView
     */
    public void startResetAnimation(View resetView) {
        if (resetView == null) {
            return;
        }
        animSetReset = new AnimatorSet();
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(resetView, "scaleX", 1.0f);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(resetView, "scaleY", 1.0f);
        LinearInterpolator interpolator = new LinearInterpolator();
        animSetReset.setInterpolator(interpolator);
        animSetReset.playTogether(animatorX, animatorY);
        animSetReset.setDuration(300);
        animSetReset.start();

    }

    /**
     * 局部刷新
     * @param focusItem
     * @param parent
     */
    private void postInvalidatePart(View focusItem, TvRecyclerView parent){
        Rect partRect = UIUtil.createViewRect(parent, focusItem, 0);
        /**放大绘制矩形**/
        UIUtil.scaleRect(partRect,1.3f,1.3f);
        parent.postInvalidate(partRect.left,partRect.top,partRect.right,partRect.bottom);
    }

}
