package com.adealink.weparty.commonui.widget.banner.indicator;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.util.AttributeSet;

import com.adealink.frame.util.DisplayUtil;

/**
 * 限制指示器数量的矩形（条形）指示器
 * 矩形（条形）指示器
 * 1、可以设置选中和默认的宽度、指示器的圆角
 * 2、如果需要正方形将圆角设置为0，可将宽度和高度设置为一样
 * 3、如果不想选中时变长，可将选中的宽度和默认宽度设置为一样
 */
public class RoomGameCompRectangleIndicator extends BaseIndicator {
    RectF rectF;

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

    public RoomGameCompRectangleIndicator(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoomGameCompRectangleIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        rectF = new RectF();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = config.getIndicatorSize();
        if (count <= 1) {
            return;
        }
        //间距*（总数-1）+默认宽度*（总数-1）+选中宽度
        int space = config.getIndicatorSpace() * (count - 1);
        int normal = config.getNormalWidth() * (count - 1);
        setMeasuredDimension(space + normal + config.getSelectedWidth(), config.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 限制最大显示数量
        int maxCount = 7;
        int totalIndicators = config.getIndicatorSize();
        if (totalIndicators <= 1) {
            return;
        }

        int selectedIndex = config.getCurrentPosition();

// 计算绘制起始下标
        int half = maxCount / 2;
        int startIndex = Math.max(0, selectedIndex - half);
        int endIndex = Math.min(totalIndicators, startIndex + maxCount);

// 如果到了尾部，重新调整起始位置
        if (endIndex - startIndex < maxCount && startIndex > 0) {
            startIndex = Math.max(0, endIndex - maxCount);
        }

        boolean isRTL = (getLayoutDirection() == LAYOUT_DIRECTION_RTL);

        int selectedWidth = DisplayUtil.dp2px(6f); // 选中项宽度
        int circleDiameter = DisplayUtil.dp2px(3f); // 小圆直径
        int circleRadius = circleDiameter / 2;
        int spacing = DisplayUtil.dp2px(2f); // 间距

        int count = endIndex - startIndex;
        int totalWidth = (count - 1) * (circleDiameter + spacing) + selectedWidth;
        float startX = (getWidth() - totalWidth) / 2f; // 居中起点
        float centerY = getHeight() / 2f;

        float x = startX;

        if (!isRTL) {
            // LTR 正常绘制，从左到右
            for (int i = startIndex; i < endIndex; i++) {
                boolean isSelected = (i == selectedIndex);
                mPaint.setColor(isSelected ? config.getSelectedColor() : config.getNormalColor());

                if (isSelected) {
                    float top = centerY - circleRadius;
                    float bottom = centerY + circleRadius;
                    rectF.set(x, top, x + selectedWidth, bottom);
                    canvas.drawRoundRect(rectF, circleRadius, circleRadius, mPaint);
                    x += selectedWidth + spacing;
                } else {
                    canvas.drawCircle(x + circleRadius, centerY, circleRadius, mPaint);
                    x += circleDiameter + spacing;
                }
            }
        } else {
            // RTL 方向，需要从右向左绘制
            // 这里从右侧起点开始绘制，x 向左递减
            x = startX + totalWidth;

            for (int i = startIndex; i < endIndex; i++) {
                boolean isSelected = (i == selectedIndex);
                mPaint.setColor(isSelected ? config.getSelectedColor() : config.getNormalColor());

                if (isSelected) {
                    float top = centerY - circleRadius;
                    float bottom = centerY + circleRadius;
                    rectF.set(x - selectedWidth, top, x, bottom);
                    canvas.drawRoundRect(rectF, circleRadius, circleRadius, mPaint);
                    x -= selectedWidth + spacing;
                } else {
                    canvas.drawCircle(x - circleRadius, centerY, circleRadius, mPaint);
                    x -= circleDiameter + spacing;
                }
            }
        }


    }
}
