package com.abbidot.tracker.widget

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.core.content.ContextCompat
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.tracker.R

/**
 *Created  by .yzq on 2024/6/19/019.
 * @link
 * @description:
 */
class MyBluetoothAnimView : View {
    private lateinit var mContext: Context
    private lateinit var mPathPaint: Paint
    private lateinit var mArcPaint: Paint
    private lateinit var mPath: Path

    private var mWidth = 0
    private var mHeight = 0
    private var mStrokeWidth = 0f

    private lateinit var mLeftSmallRectF: RectF
    private lateinit var mLeftBigRectF: RectF
    private lateinit var mRightSmallRectF: RectF
    private lateinit var mRightBigRectF: RectF

    private lateinit var mValueAnimator: ValueAnimator

    private var mValue = 0

    //正在动画中
    private var isAnim = false

    //是否需要动画
    private var mNeedAnim = true

    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 onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(mWidth, mHeight)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        mContext = context

        //禁用硬件加速,防止返回到界面多次执行onDraw
        setLayerType(LAYER_TYPE_SOFTWARE, null)

        mWidth = AppUtils.dpToPx(30)
        mHeight = AppUtils.dpToPx(16)
        mStrokeWidth = AppUtils.dpToPx(1f)

        mPathPaint = Paint().apply {
            color = ContextCompat.getColor(context, R.color.data_black_color)
            strokeWidth = mStrokeWidth
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            style = Paint.Style.STROKE
        }

        mArcPaint = Paint().apply {
            color = ContextCompat.getColor(context, R.color.block_color2)
            strokeWidth = mStrokeWidth
            style = Paint.Style.STROKE
        }

        initDrawData()
    }

    private fun initDrawData() {
        val centerX = mWidth / 2 - mPathPaint.strokeWidth / 2
        val centerY = mHeight / 2 - mPathPaint.strokeWidth / 2
        mPath = Path().apply {
            moveTo(centerX, 0f)
            lineTo(centerX, mHeight.toFloat())
            lineTo(centerX + centerX / 3, centerY + centerY / 2)
            lineTo(centerX - centerX / 4, centerY / 2)

            moveTo(centerX, 0f)
            lineTo(centerX + centerX / 3, centerY / 2)
            lineTo(centerX - centerX / 4, centerY + centerY / 2)
        }

        val bigRadius = centerY
        val smallRadius = centerY / 2
        val diffRadius = bigRadius - smallRadius
        mLeftSmallRectF = RectF(
            bigRadius - smallRadius + mPathPaint.strokeWidth / 2,
            bigRadius - smallRadius + mPathPaint.strokeWidth / 2,
            bigRadius + smallRadius,
            bigRadius + smallRadius
        )
        mRightSmallRectF = RectF(
            mWidth - bigRadius * 2 + diffRadius,
            bigRadius - smallRadius + mPathPaint.strokeWidth / 2,
            mWidth - diffRadius,
            bigRadius + smallRadius
        )
        mLeftBigRectF = RectF(
            mPathPaint.strokeWidth / 2, mPathPaint.strokeWidth / 2, bigRadius * 2, bigRadius * 2
        )
        mRightBigRectF = RectF(
            mWidth - bigRadius * 2 + mPathPaint.strokeWidth / 2,
            mPathPaint.strokeWidth / 2,
            mWidth.toFloat() - mPathPaint.strokeWidth / 2,
            bigRadius * 2
        )

        //动画
        mValueAnimator = ValueAnimator.ofInt(0, 3).apply {
            duration = 1800
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                mValue = it.animatedValue as Int
                postInvalidate()
                if (!isAnim && mValue == 2) {
                    cancel()
                }
            }
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.apply {
            startDraw(this)
        }
    }

    private fun startDraw(canvas: Canvas) {
        canvas.apply {
            drawPath(mPath, mPathPaint)
            if (mNeedAnim) {
                if (!isAnim || mValue >= 1) {
                    drawArc(mLeftSmallRectF, 120f, 120f, false, mArcPaint)
                    drawArc(mRightSmallRectF, -60f, 120f, false, mArcPaint)
                }
                if (!isAnim || mValue == 2) {
                    drawArc(mLeftBigRectF, 130f, 100f, false, mArcPaint)
                    drawArc(mRightBigRectF, -50f, 100f, false, mArcPaint)
                }
            } else {
                if (mValue > 0) {
                    drawArc(mLeftSmallRectF, 120f, 120f, false, mArcPaint)
                    drawArc(mRightSmallRectF, -60f, 120f, false, mArcPaint)
                    drawArc(mLeftBigRectF, 130f, 100f, false, mArcPaint)
                    drawArc(mRightBigRectF, -50f, 100f, false, mArcPaint)
                }
            }
        }
    }

    /**
     * 设置动画开始和停止
     */
    fun setBluetoothAnimStartAndStop(show: Boolean) {
        isAnim = show
        if (show) {
            mValueAnimator.start()
        }
    }

    fun setValue(value: Int) {
        if (value >= 0) {
            mNeedAnim = false
            mValue = value
            invalidate()
        }
    }
}