package com.swago.room.gift

import android.content.Context
import android.graphics.*
import android.os.CountDownTimer
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.MotionEvent
import android.view.View
import com.swago.room.R

/**
 *@date 2022/1/30 12:14
 *description:
 */
class ComboView : View {

    //内圆画笔
    private  lateinit var innerPaint: Paint
    //圆环画笔
    private lateinit var anchorPaint:Paint
    //Combo画笔
    private lateinit var textComboPaint: Paint
    private lateinit var timerPaint: Paint
    //缩小圆环画笔
    private lateinit var scalePaint:Paint

    private var centerX = 0f
    private var centerY = 0f
    private var scaleRadius = 0f

    private lateinit var path:Path
    private lateinit var rect:RectF

    private lateinit var comboRect:Rect
    private lateinit var timeRect:Rect
    private val combo = "Combo"
    private var time = "2.6s"

    private lateinit var timeCount:CircleTimeCount

    private var linearGrouping:LinearGradient? = null
    private var llGrouping:LinearGradient? = null


    /**
     * 渐变色的X轴上的起始位置和结束位置
     */
    private val startX by lazy {
        dp2px(40f)
    }

    private val endX by lazy {
        dp2px(100f)
    }


    private val rangeRadius by lazy {
        dp2px(13f)
    }


    constructor(context: Context) : this(context,null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs,0)
    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle){
        initView()
    }

    private fun initView() {
        //倒计时
        timeCount = CircleTimeCount(3000,50)
        //Combo文字
        textComboPaint = Paint()
        textComboPaint.isAntiAlias =true
        textComboPaint.textSize = dp2px(12f)
        textComboPaint.color = resources.getColor(R.color.white)
        comboRect = Rect()
        textComboPaint.getTextBounds(combo,0,combo.length,comboRect)
        //倒计时文字
        timerPaint = Paint()
        timerPaint.isAntiAlias =true
        timerPaint.textSize = dp2px(14f)
        timerPaint.color = resources.getColor(R.color.white)
        timeRect = Rect()
        timerPaint.getTextBounds(time,0,time.length,timeRect)
        //内圆
        innerPaint = Paint()
        innerPaint.style = Paint.Style.FILL_AND_STROKE
        innerPaint.isAntiAlias = true
        //圆弧
        anchorPaint = Paint()
        anchorPaint.style = Paint.Style.STROKE
        anchorPaint.isAntiAlias = true
        anchorPaint.color = resources.getColor(R.color.arcColor)
        anchorPaint.strokeWidth = dp2px(5f)
        //缩放圆弧
        scalePaint = Paint()
        scalePaint.style = Paint.Style.STROKE
        scalePaint.isAntiAlias = true
        scalePaint.color = resources.getColor(R.color.scaleColor)
        scalePaint.strokeWidth = dp2px(3f)

        path = Path()
        rect = RectF(dp2px(17f),dp2px(17f),dp2px(82f),dp2px(82f))
        path.addArc(rect,0f,360f)
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val  measureWidth = MeasureSpec .getSize(widthMeasureSpec)
        val  measureHeight = MeasureSpec.getSize(heightMeasureSpec)

        centerX = measureWidth/2.toFloat()
        centerY = measureHeight/2.toFloat()

        linearGrouping = LinearGradient(startX,
            measureHeight/2f,
            endX,
            measureHeight/2f,
            resources.getColor(R.color.startColor),
            resources.getColor(R.color.endColor)
            ,Shader.TileMode.CLAMP)

        llGrouping = LinearGradient(startX,
            measureHeight/2f,
            endX,
            measureHeight/2f,
            resources.getColor(R.color.startDownColor),
            resources.getColor(R.color.startDownColor)
            ,Shader.TileMode.CLAMP)

        innerPaint.shader = llGrouping

    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //画内圆
        canvas?.drawCircle(centerX,centerY,dp2px(30f),innerPaint)
        //画倒计时
//        canvas?.drawPath(path,anchorPaint)
        //画缩小框
        canvas?.drawCircle(centerX,centerY,scaleRadius,scalePaint)
        //画文字
        canvas?.drawText(combo,centerX-comboRect.width()/2f,dp2px(45f),textComboPaint)
        canvas?.drawText(time,centerX-timeRect.width()/2f,dp2px(65f),timerPaint)
    }

    private fun dp2px(value: Float): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            value,
            resources.displayMetrics
        )
    }


    inner class CircleTimeCount : CountDownTimer{

        constructor(millisInFuture:Long,countDownInterval:Long):super(millisInFuture,countDownInterval)


        override fun onFinish() {
            Log.d("hhq","倒计时结束")
            clickListener?.countFinish()
        }

        override fun onTick(millisUntilFinished: Long) {
            time = "${((millisUntilFinished/1000f).toString().substring(0,3))}s"
            path.reset()
            path.addArc(rect,-90f,360f*millisUntilFinished/3000)
            scaleRadius = rangeRadius*millisUntilFinished/3000 + dp2px(35f)
            postInvalidate()
        }

    }


    private var clickListener:OnClick? = null

    interface OnClick{
        fun click()
        fun countFinish()
    }

    fun setOnClick(click: OnClick){
        this.clickListener = click
    }


    fun start(){
        timeCount.cancel()
        timeCount.start()
    }

    fun cancel(){
        timeCount.cancel()

    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when(event?.action){
            MotionEvent.ACTION_DOWN -> {
                innerPaint.shader = llGrouping
            }

            MotionEvent.ACTION_UP -> {
                innerPaint.shader = linearGrouping
                postInvalidate()
                start()
                clickListener?.click()
            }
        }
        return true
    }

    override fun onDetachedFromWindow() {
        cancel()
        super.onDetachedFromWindow()
    }

}