package com.duben.microtribe.ui.widgets

import android.app.Dialog
import android.content.Context
import android.os.Handler
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import com.daimajia.androidanimations.library.Techniques
import com.daimajia.androidanimations.library.YoYo
import com.duben.microtribe.R
import com.duben.microtribe.ui.widgets.countdowntimer.CountDownTimerSupport
import com.duben.microtribe.ui.widgets.countdowntimer.OnCountDownTimerListener
import com.duben.microtribe.utils.SpanUtils
import com.duben.microtribe.utils.TimeRender
import java.util.*

class RuleAgreementDialog(private val context: Context, private val listener: DialogListener) :
    Dialog(
        context, R.style.dialog
    ) {
    private val lp: WindowManager.LayoutParams
    private val iv_agreement_quit: ImageView
    private val tv_agreement_next: TextView
    private val tv_rule_date1: TextView
    private val tv_rule_date2: TextView
    var timer: CountDownTimerSupport? = null

    init {
        setContentView(R.layout.dialog_agreement_rule)
        // 设置window属性
        lp = window!!.attributes
        lp.gravity = Gravity.BOTTOM
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.windowAnimations = R.style.DialogAnimBottom
        window!!.attributes = lp

        // 设置外部不可关闭
        setCancelable(false)
        setCanceledOnTouchOutside(false)
        setOnKeyListener { dialogInterface, i, keyEvent ->
            if (i == KeyEvent.KEYCODE_BACK) {
                true
            } else false
        }

        // 查找View
        tv_agreement_next = findViewById<View>(R.id.tv_agreement_next) as TextView
        iv_agreement_quit = findViewById<View>(R.id.iv_agreement_quit) as ImageView
        tv_rule_date1 = findViewById<View>(R.id.tv_rule_date1) as TextView
        tv_rule_date2 = findViewById<View>(R.id.tv_rule_date2) as TextView
        iv_agreement_quit.setOnClickListener(listener)
        tv_agreement_next.setOnClickListener(listener)

        Handler().postDelayed({
            startTimer()
        }, 200)



    }

    override fun dismiss() {
        super.dismiss()

        if (timer != null) { //防止计时器重复
            timer!!.stop()
            timer = null
        }
    }

    fun startTimer() {
        if (timer != null) { //防止计时器重复
            timer!!.stop()
            timer = null
        }
        timer = CountDownTimerSupport(4000, 1000)
        timer!!.setOnCountDownTimerListener(object : OnCountDownTimerListener {
            override fun onTick(millisUntilFinished: Long) {
                if (isShowing) {
                    tv_agreement_next?.text = "请先阅读试用规则(" + millisUntilFinished / 1000 + ")秒"
                }
            }

            override fun onFinish() {
                if (isShowing) {
                    tv_agreement_next?.text = "立即开通"
                    tv_agreement_next?.setBackgroundResource(R.mipmap.bg_vip_btn)
                    tv_agreement_next?.isEnabled = true
                    YoYo.with(Techniques.Pulse).duration(500).repeat(-1).playOn(tv_agreement_next)
                }
            }
        })
        timer!!.start()
    }

    fun setText(remarks: String,days:Int,cash:Double,title: String) {
        tv_rule_date2.text = TimeRender.getDateAfter(Date(),days) + ":" + remarks
        SpanUtils.with(tv_rule_date1)
                .append(TimeRender.formatDateDot(Date()))
                .append(": ")
                .append("${cash}元开启${title}体验")
                .setForegroundColor(context.resources.getColor(R.color.main_mints))
                .create()
    }
}