package com.yidian.shenghuoquan.newscontent.utils

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.CountDownTimer
import android.widget.TextView

/**
 * Author: sym
 * Date: 2021/5/18 7:43 PM
 * Describe:
 */
class CountDownTimerUtils(textView: TextView, millisInFuture: Long, countDownInterval: Long) :
  CountDownTimer(millisInFuture, countDownInterval) {

  constructor(textView: TextView, millisInFuture: Long, countDownInterval: Long, btnText: String) : this(
    textView,
    millisInFuture,
    countDownInterval
  ) {
    btnContent = btnText
  }

  private var mTextView: TextView? = textView
  private var btnContent: String? = "获取验证码"

  @SuppressLint("SetTextI18n")
  override fun onTick(millisUntilFinished: Long) {
    mTextView!!.isClickable = false //设置不可点击
    mTextView!!.setTextColor(Color.rgb(0xc2, 0xc2, 0xc2))
    val temp: Long = millisUntilFinished / 1000
    if (temp.toString() != "0") {
      mTextView!!.text = temp.toString() + "s"//设置倒计时时间
    } else {
      mTextView!!.setTextColor(Color.rgb(0x18, 0x52, 0xf1))
      mTextView!!.text = btnContent
    }
  }

  override fun onFinish() {
    mTextView!!.setTextColor(Color.rgb(0x18, 0x52, 0xf1))
    mTextView!!.text = btnContent
    mTextView!!.isClickable = true //重新获得点击
  }
}
