package com.abbidot.tracker.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.os.CountDownTimer
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.ContextCompat
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.R

/**
 *Created  by .yzq on 2023/3/3/003.
 * @link
 * @description: 倒计时圆形
 */
class CircleCountDownView : AppCompatImageView {

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

    //倒计时默认30秒
    private var mCountDownTimer: CountDownTimer? = null
    private var mSweepAngle: Float = 360f

    //倒计时变化的速率，越小越快
    private var mSpeed = 100L

    private var mOnCircleCountDownCallback: OnCircleCountDownCallback? = null


    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)
        //开启抗锯齿
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)

        if (null == attrs) {
            mPaint.color = ContextCompat.getColor(mContext, R.color.btn_yellow_color)
            mPaint.alpha = 255 / 2
        } else {
            val obtainStyledAttributes =
                mContext.obtainStyledAttributes(attrs, R.styleable.CircleCountDownView)
            mPaint.color = obtainStyledAttributes.getColor(R.styleable.CircleCountDownView_color,
                    ContextCompat.getColor(mContext, R.color.btn_yellow_color))
            mPaint.alpha =
                obtainStyledAttributes.getInt(R.styleable.CircleCountDownView_colorAlpha, 255 / 2)
            obtainStyledAttributes.recycle()
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        LogUtil.e("onMeasure")
        mWidth = measuredWidth
        mHeight = measuredHeight
//        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
//        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
//        if (widthMode == MeasureSpec.AT_MOST) {
//            background?.apply {
//                mWidth = this.minimumWidth
//            }
//        }
//        if (heightMode == MeasureSpec.AT_MOST) {
//            mHeight = this.minimumHeight
//        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.apply {
            drawArc(0f, 0f, mWidth.toFloat(), mHeight.toFloat(), 270f, -mSweepAngle, true, mPaint)
        }
    }

    fun startCountDown(second: Int) {
        LogUtil.d("倒计时开始")
        stopCountDown()
        if (second <= 0) {
            //不需要扇形背景
            mSweepAngle = 0f
            invalidate()
            LogUtil.e("CircleCountDownView,时间秒不能小于等于0")
            return
        }
        mSweepAngle = 360f
        mSpeed = if (second < (5 * 60)) {
            80L
        } else {
            180L
        }
        val quantity = mSweepAngle / second
        mCountDownTimer = object : CountDownTimer(second * 1000L, mSpeed) {
            override fun onTick(millisUntilFinished: Long) {
                val time = millisUntilFinished / 1000f
                mSweepAngle = quantity * time
                postInvalidate()
            }

            override fun onFinish() {
                LogUtil.d("倒计时结束")
                mSweepAngle = 360f
                invalidate()
                mOnCircleCountDownCallback?.countDownCompleteCallback()
            }
        }
        mCountDownTimer?.apply {
            start()
            mOnCircleCountDownCallback?.startCountDownCallback()
        }
    }

    fun stopCountDown() {
        mCountDownTimer?.apply {
            LogUtil.d("倒计时停止")
            cancel()
            mSweepAngle = 360f
            invalidate()
        }
    }

    fun setOnCircleCountDownCallback(onCircleCountDownCallback: OnCircleCountDownCallback) {
        mOnCircleCountDownCallback = onCircleCountDownCallback
    }

    interface OnCircleCountDownCallback {
        fun startCountDownCallback()
        fun countDownCompleteCallback()
    }
}