package com.bonait.bnframework.common.linktab;

import static java.lang.Math.min;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bonait.bnframework.R;

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

public class TopItemDecoration extends RecyclerView.ItemDecoration{

    public Context mContext;
    //间隔高度
    private int mHeight = 60;
    //矩形画笔
    private Paint mPaint=new Paint();
    //标签画笔
    private Paint textPaint=new Paint(Paint.ANTI_ALIAS_FLAG);

    private Rect mRound =new Rect();

    public List<String> typeListener=new ArrayList<>();
    public TopItemDecoration(Context _context,List<String> type)
    {
        mContext=_context;
        mPaint.setColor(ContextCompat.getColor(mContext, R.color.main_background));
        textPaint.setColor(ContextCompat.getColor(mContext, R.color.app_color_blue));
        textPaint.setTextSize(25f);
        typeListener=type;
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        int left = parent.getPaddingLeft();
        int right =parent.getWidth() - parent.getPaddingLeft();
        int childCount = parent.getChildCount();
        for(int i=0;i<childCount;i++)
        {
            View childView = parent.getChildAt(i);
            int bottom = childView.getTop();
            int top = bottom - mHeight;

            //绘制矩形间隔
            c.drawRect(left, top, right, bottom, mPaint);
            //根据位置获取当前item的标签
            String tag =typeListener.get(parent.getChildAdapterPosition(childView));

            //绘制标签文本内容
            textPaint.getTextBounds(tag, 0, tag.length(), mRound);
            c.drawText(tag, left + textPaint.getTextSize(), bottom - mHeight / 2 + mRound.height() / 2, textPaint);
        }
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        //设置间隔高度
        outRect.top = mHeight;
    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);

        int left = parent.getPaddingLeft();
        int right =parent.getWidth() - parent.getPaddingLeft();

        LinearLayoutManager manager = (LinearLayoutManager)parent.getLayoutManager();
        //第一个可见item位置
        int index = manager.findFirstVisibleItemPosition();
        if (index != -1) {
            //获取指定位置item的View信息
            View childView = parent.findViewHolderForLayoutPosition(index).itemView;
            int top = parent.getPaddingTop();
            String tag = typeListener.get(index);
            int bottom = parent.getPaddingTop() + mHeight;
            //悬浮置顶判断，其实也就是一直在绘制一个矩形加文本内容(上滑时取值bottom，下滑时取值childView.bottom.toFloat())
            bottom = min(childView.getBottom(), bottom);
            c.drawRect(0f, top, right, bottom, mPaint);
            textPaint.getTextBounds(tag, 0, tag.length(), mRound);
            c.drawText(tag, left + textPaint.getTextSize(), bottom - mHeight / 2 + mRound.height() / 2, textPaint);
        }
    }
}
