//Copyright 2018 Huawei Technologies Co.,Ltd.
package com.huawei.mediacloud.livepushdemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Xfermode;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import com.huawei.mediacloud.livepushdemo.utils.ScreenUtils;

public class XfermodeRecyclerView extends RecyclerView {

    private Context recyclerContext;
    private Paint mPaint;
    private LinearGradient linearGradient;
    private int layerId;
    private int preWidth = 0;
    private int itemViewWidth;

    public XfermodeRecyclerView(Context context) {
        this(context, null);
    }

    public XfermodeRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        recyclerContext = context;
        // item宽度
        itemViewWidth = ScreenUtils.dip2px(recyclerContext, 49);
        doTopGradualEffect();
    }

    public void doTopGradualEffect() {
        mPaint = new Paint();
        // dst_in 模式
        final Xfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
        mPaint.setXfermode(xfermode);

        addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
                super.onDrawOver(canvas, parent, state);
                // 当linearGradient为空即第一次绘制 或 Recyclerview宽度发生改变时 重新计算透明位置
                if (linearGradient == null || preWidth != parent.getWidth()) {
                    // 透明位置从最左边一个 itemView 到其1/3处
                    linearGradient = new LinearGradient(0.0f, 0.0f, itemViewWidth / 3, 0.0f, new int[]{0, Color.BLACK}, null, Shader.TileMode.CLAMP);
                    preWidth = parent.getWidth();
                }
                mPaint.setXfermode(xfermode);
                mPaint.setShader(linearGradient);
                canvas.drawRect(0.0f, 0.0f, parent.getRight(), parent.getBottom(), mPaint);
                mPaint.setXfermode(null);
                canvas.restoreToCount(layerId);
            }

            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
                layerId = c.saveLayer(0.0f, 0.0f, (float) parent.getWidth(), (float) parent.getHeight(), null, Canvas.ALL_SAVE_FLAG);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                if (parent.getChildLayoutPosition(view) == 0) {
                    outRect.left = itemViewWidth / 3;
                }
            }
        });
    }
}
