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.PathMeasure
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/3/28/028.
 * @link
 * @description:
 */
class MyRoomView : View {

    private lateinit var mContext: Context
    private var mWidth = 0
    private var mHeight = 0

    private lateinit var mPaint: Paint
    private lateinit var mGrayPaint: Paint

    private lateinit var mLeftHalfPath: Path
    private lateinit var mRightHalfPath: Path
    private lateinit var mLeftHalfPathMeasure: PathMeasure
    private lateinit var mRightHalfPathMeasure: PathMeasure

    private lateinit var mCloseAnimator: ValueAnimator
    private lateinit var mOpenAnimator: ValueAnimator
    private val mAnimatorDuration = 2500L

    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)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        mContext = context
        //禁用硬件加速,防止返回到界面多次执行onDraw
        setLayerType(LAYER_TYPE_SOFTWARE, null)

        mWidth = AppUtils.dpToPx(150)
        mHeight = mWidth

        mPaint = Paint().apply {
            strokeWidth = AppUtils.dpToPx(8f)
            style = Paint.Style.STROKE
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            color = ContextCompat.getColor(mContext, R.color.black)
        }
        mGrayPaint = Paint().apply {
            strokeWidth = AppUtils.dpToPx(8f)
            style = Paint.Style.STROKE
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            color = ContextCompat.getColor(mContext, R.color.white_gray_color)
        }

        mLeftHalfPathMeasure = PathMeasure()
        mLeftHalfPath = Path().apply {
            moveTo(mWidth / 2f, 0f + mPaint.strokeWidth / 2)
            lineTo(0f + mPaint.strokeWidth / 2, mHeight / 2f)
            mLeftHalfPathMeasure.setPath(this, false)
            val node = FloatArray(2)
            val tempPathLength = mLeftHalfPathMeasure.length
            //获取path指定长度某个的点
            mLeftHalfPathMeasure.getPosTan(tempPathLength - tempPathLength / 6, node, null)
            lineTo(node[0], node[1])
            lineTo(node[0], mHeight.toFloat() - mPaint.strokeWidth / 2)
            lineTo(mWidth / 2f, mHeight.toFloat() - mPaint.strokeWidth / 2)
        }
        mLeftHalfPathMeasure.setPath(mLeftHalfPath, false)

        mRightHalfPathMeasure = PathMeasure()
        mRightHalfPath = Path().apply {
            moveTo(mWidth / 2f, 0f + mPaint.strokeWidth / 2)
            lineTo(mWidth - mPaint.strokeWidth / 2, mHeight / 2f)
            mRightHalfPathMeasure.setPath(this, false)
            val node = FloatArray(2)
            val tempPathLength = mRightHalfPathMeasure.length
            //获取path指定长度某个的点
            mRightHalfPathMeasure.getPosTan(tempPathLength - tempPathLength / 6, node, null)
            lineTo(node[0], node[1])
            lineTo(node[0], mHeight.toFloat() - mPaint.strokeWidth / 2)
            lineTo(mWidth / 2f, mHeight.toFloat() - mPaint.strokeWidth / 2)
        }
        mRightHalfPathMeasure.setPath(mRightHalfPath, false)

        val pathLength = mLeftHalfPathMeasure.length
        // 创建一个值动画器来更新animatedValue
        mCloseAnimator = ValueAnimator.ofFloat(0f, pathLength).apply {
            duration = mAnimatorDuration
            interpolator = LinearInterpolator()
            addUpdateListener {
                mAnimatedValue = it.animatedValue as Float
                invalidate()
            }
        }
        mOpenAnimator = ValueAnimator.ofFloat(pathLength, 0f).apply {
            duration = mAnimatorDuration
            interpolator = LinearInterpolator()
            addUpdateListener {
                mAnimatedValue = it.animatedValue as Float
                invalidate()
            }
        }
    }

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

    private var mAnimatedValue = 0f

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

    private fun startDraw(canvas: Canvas) {
        canvas.drawPath(mLeftHalfPath, mGrayPaint)
        canvas.drawPath(mRightHalfPath, mGrayPaint)

//        val dst = FloatArray(2)
        // 使用animatedValue来计算路径上的点,获取path指定长度一个的点
//        mLeftHalfPathMeasure.getPosTan(mAnimatedValue, dst, null)
//        canvas.drawCircle(dst[0], dst[1], 10f, mPaint)

        val path = Path()
        //获取path指定长度某段
        mLeftHalfPathMeasure.getSegment(0f, mAnimatedValue, path, true)
        canvas.drawPath(path, mPaint)

        val path1 = Path()
        mRightHalfPathMeasure.getSegment(0f, mAnimatedValue, path1, true)
        canvas.drawPath(path1, mPaint)
    }

    fun playOpenAnimator() {
        mOpenAnimator.start()
    }

    fun playCloseAnimator() {
        mCloseAnimator.start()
    }
}