package com.abbidot.tracker.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import androidx.core.content.ContextCompat
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseView

/**
 *Created  by .yzq on 2024/10/11/011.
 * @link
 * @description:电池图标
 */
class BatteryView : BaseView {

    private val mMaxValue = 4
    private var mValueWith = 0f

    //圆形矩形的半径
    private var mRectCircleRadius = 0f

    //椭圆的半径
    private var mHalfCircleRadius = 0f

    constructor(context: Context) : super(context) {
        init(context, null)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context, attrs, defStyleAttr
    ) {
        init(context, attrs)
    }

    override fun init(context: Context, attrs: AttributeSet?) {
        super.init(context, attrs)
        mWidth = AppUtils.dpToPx(24)
        mHeight = AppUtils.dpToPx(13)
        mRectCircleRadius = AppUtils.dpToPx(3f)
        mHalfCircleRadius = AppUtils.dpToPx(3f)
        mValueWith = AppUtils.dpToPx(2f)

        mPaint.apply {
            color = ContextCompat.getColor(mContext, R.color.data_black_color)
            strokeWidth = mStrokeWidth
            strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            //开启抗锯齿
            flags = Paint.ANTI_ALIAS_FLAG
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(mWidth, mHeight)
    }

    override fun startDraw(canvas: Canvas) {
        canvas.apply {
            val rectLeft = mStrokeWidth / 2
            val rectRight = mWidth - mStrokeWidth / 2 - mHalfCircleRadius
            val rectTop = mStrokeWidth / 2
            val rectBottom = mHeight - mStrokeWidth / 2

            val arcLeft = mWidth - mStrokeWidth / 2 - mHalfCircleRadius * 2
            val arcTop = mHeight / 4 + mStrokeWidth / 2
            val arcRight = mWidth - mStrokeWidth / 2
            val arcBottom = mHeight / 4 * 3 - mStrokeWidth / 2

            when (mValue) {
                -1 -> {
                    mPaint.color = ContextCompat.getColor(mContext, R.color.data_black_color)
                    drawRoundRect(
                        rectLeft,
                        rectTop,
                        rectRight,
                        rectBottom,
                        mRectCircleRadius,
                        mRectCircleRadius,
                        mPaint
                    )
                    drawArc(
                        arcLeft, arcTop, arcRight, arcBottom, -90f, 180f, false, mPaint
                    )

                    val sX = mWidth - mStrokeWidth * 2 - mHalfCircleRadius
                    val line1StartX = sX / 6 * 2 + mStrokeWidth * 2
                    val line1StartY = (mHeight - mStrokeWidth * 2) / 4 + mStrokeWidth
                    val line1StopX = sX / 6 * 4 + mStrokeWidth
                    val line1StopY = (mHeight - mStrokeWidth * 2) / 4 * 3 + mStrokeWidth
                    mPaint.color = ContextCompat.getColor(mContext, R.color.select_color3)
                    drawLine(
                        line1StartX, line1StartY, line1StopX, line1StopY, mPaint
                    )
                    drawLine(
                        line1StartX, line1StopY, line1StopX, line1StartY, mPaint
                    )
                }

                0 -> {
                    mPaint.color = ContextCompat.getColor(mContext, R.color.yellow_red_color2)
                    drawRoundRect(
                        rectLeft,
                        rectTop,
                        rectRight,
                        rectBottom,
                        mRectCircleRadius,
                        mRectCircleRadius,
                        mPaint
                    )
                    drawArc(
                        arcLeft, arcTop, arcRight, arcBottom, -90f, 180f, false, mPaint
                    )
                }

                else -> {
                    mPaint.color = ContextCompat.getColor(mContext, R.color.data_black_color)
                    drawRoundRect(
                        rectLeft,
                        rectTop,
                        rectRight,
                        rectBottom,
                        mRectCircleRadius,
                        mRectCircleRadius,
                        mPaint
                    )
                    drawArc(
                        arcLeft, arcTop, arcRight, arcBottom, -90f, 180f, false, mPaint
                    )
                    val valueWidth = mWidth - mStrokeWidth * 2 - mHalfCircleRadius
                    val valueSpace = (valueWidth - mValueWith * mMaxValue) / (mMaxValue + 3)
                    mPaint.color = ContextCompat.getColor(mContext, R.color.select_color)
                    for (i in 0 until mValue) {
                        val lineX = mStrokeWidth + 3 * valueSpace + mValueWith * i + valueSpace * i
                        drawLine(
                            lineX,
                            mHeight / 4f + mStrokeWidth,
                            lineX,
                            mHeight / 4f * 3 - mStrokeWidth,
                            mPaint
                        )
                    }
                }
            }
        }
    }

    fun setValue(value: Int) {
        mValue = value
        invalidate()
    }

}