package com.zj365.health.weight

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

class StatusChartView(context: Context, attrs: AttributeSet?) : View(context, attrs) {

    private val statuses = listOf("Good", "Neutral", "Bad")
    private val colors = listOf(Color.GREEN, Color.YELLOW, Color.RED)
    private val dates = listOf("2023-08-20", "2023-08-21", "2023-08-22")

    private val barHeight = 100f
    private val textMargin = 16f
    private val cornerRadius = 20f

    private val paint = Paint()

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        val widthPerStatus = width.toFloat() / statuses.size

        // 绘制整体圆角背景
        val backgroundRect = RectF(0f, 0f, width.toFloat(), barHeight)
        canvas?.drawRoundRect(backgroundRect, cornerRadius, cornerRadius, paint)

        for ((index, status) in statuses.withIndex()) {
            paint.color = colors[index]

            // 创建矩形区域
            val rect = RectF(
                widthPerStatus * index,
                0f,
                widthPerStatus * (index + 1),
                barHeight
            )

            // 绘制圆角矩形
            if (index == 0) {
                // 第一个块：左上角和左下角为圆角
                canvas?.drawRoundRect(rect, cornerRadius, 0f, paint)
            } else if (index == statuses.size - 1) {
                // 最后一个块：右上角和右下角为圆角
                canvas?.drawRoundRect(rect, 0f, cornerRadius, paint)
            } else {
                // 中间块：直角
                canvas?.drawRect(rect, paint)
            }

            // 绘制块内的文本
            paint.color = Color.BLACK
            paint.textSize = 40f
            val textWidth = paint.measureText(status)
            val textX = widthPerStatus * index + (widthPerStatus - textWidth) / 2
            canvas?.drawText(
                status,
                textX,
                barHeight / 2 + textMargin,
                paint
            )
        }

        // 绘制日期文本
        paint.textSize = 30f
        for ((index, date) in dates.withIndex()) {
            // 计算文本绘制位置，使其居中
            val textWidth = paint.measureText(date)
            val textX = widthPerStatus * index + (widthPerStatus - textWidth) / 2
            canvas?.drawText(
                date,
                textX,
                barHeight + textMargin + 60,
                paint
            )
        }
    }
}