package com.xunao.effectdemo.activity

import android.app.Activity
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Message
import androidx.annotation.RequiresApi
import com.xuexiang.xui.widget.progress.HorizontalProgressView.LINEAR_INTERPOLATOR
import com.xunao.effectdemo.R
import kotlinx.android.synthetic.main.activity_count_down.*

/**
 * author : 程中强
 * e-mail : 740479946@qq.com
 * date : 2022/8/1815:10
 * desc :
 * version: 1.0
 */
class CountDownActivity: Activity() {

    val handler = object: Handler(){
        @RequiresApi(Build.VERSION_CODES.M)
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                101 -> {
                    hpv_language.progress = 100*currentProgress/600.toFloat()

                    dial_progress.setValue(currentProgress/10.toFloat())
                    if (currentProgress == 300){
                        hpv_language.setEndColor(getColor(R.color.xui_config_color_red))
                        hpv_language.setStartColor(getColor(R.color.xui_config_color_red))
                        dial_progress.setProgressColor( getColor(R.color.xui_config_color_red))
                    }

                }
            }
        }

    }

    var currentProgress =600
    var isLoop = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_count_down)
        hpv_language.setEndProgress(60f)
        hpv_language.setAnimateType(LINEAR_INTERPOLATOR)
        btn_start.setOnClickListener{v->

            isLoop = true
//            handler.removeCallbacks(run)
            Thread(run).start()
        }
    }

    private val run = Runnable {
        try {
            while (isLoop) {
                if (currentProgress <= 0) {
                    currentProgress = 600
                    isLoop = false
                }
                Thread.sleep(100)
                currentProgress--;
                handler.sendEmptyMessage(101)
            }
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
    }

}