package com.example.cloudmusic.adapter;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class StartDecoration extends RecyclerView.ItemDecoration {
    private int groupHeadHeight;
    private Context context;
    private Paint mPaint;
    private Paint headPaint;
    public StartDecoration(Context context) {
        super();
        this.context = context;
        mPaint = new Paint();
        headPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(50);
        headPaint.setColor(Color.RED);
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (parent.getAdapter() instanceof SongListAdapter){
            SongListAdapter starAdapter = (SongListAdapter) parent.getAdapter();
            //当前屏幕的item个数
            int count = parent.getChildCount();
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
            for (int i = 0; i < count; i++) {
                View view = parent.getChildAt(i);
                int position = parent.getChildLayoutPosition(view);
                //是头部
                if(i>6){
                    c.drawRect(left, view.getTop()-groupHeadHeight,right,view.getTop(),headPaint);
                    c.drawText("播放全部",left+10,view.getTop()-groupHeadHeight/2+10,
                            mPaint);
                }
            }
        }
    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        if(parent.getAdapter() instanceof SongListAdapter){
            SongListAdapter starAdapter = (SongListAdapter) parent.getAdapter();
            int left = parent.getPaddingLeft();
            int top = parent.getPaddingTop();
            int right = parent.getWidth() - parent.getPaddingRight();
            int position = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
            //获取对应VIew
            View view = parent.findViewHolderForAdapterPosition(position).itemView;
            boolean isGroupHead = starAdapter.isGroupHead(position+1);
            if (true){
                int bottom = Math.min(groupHeadHeight,view.getBottom() - top);
                c.drawRect(left,top,right,top+bottom,headPaint);
                c.drawText(starAdapter.getGroupName(position),left+10,top + bottom - groupHeadHeight/2+10,mPaint);

            }else{
                c.drawRect(left,top,right,top+groupHeadHeight,headPaint);
                c.drawText(starAdapter.getGroupName(position),left+10,top+groupHeadHeight/2+10,mPaint);
            }
        }
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if(parent.getAdapter() instanceof SongListAdapter){
            SongListAdapter starAdapter = (SongListAdapter) parent.getAdapter();
            int position = parent.getChildLayoutPosition(view);
            boolean isGroupHead = starAdapter.isGroupHead(position+1);
            if(true){
                outRect.set(0,groupHeadHeight,0,0);
            }else {
                outRect.set(0,2,0,0);
            }
        }

    }
    private int dp2px(Context context,float dpValue){
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue *scale * 0.5f);
    }
}

