package com.doverfuelingsolutions.issp.view.widget

import android.content.Context
import android.util.AttributeSet
import android.view.SoundEffectConstants
import android.view.View
import android.widget.FrameLayout
import com.doverfuelingsolutions.issp.R
import kotlinx.android.synthetic.main.widget_fragment_toolbar.view.*
import kotlinx.coroutines.*

class FragmentToolbar(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    val view: View = View.inflate(context, R.layout.widget_fragment_toolbar, this)

    var isBackVisible = true
        set(value) {
            field = value
            buttonBack.visibility = if (value) VISIBLE else GONE
        }
    var isHomeVisible = true
        set(value) {
            field = value
            buttonHome.visibility = if (value) VISIBLE else GONE
        }
    private var isTimerVisible = true
        set(value) {
            field = value
            countdownNum.visibility = if (value) VISIBLE else GONE
        }
    var title = ""
        set(value) {
            field = value
            titleText.text = value
        }

    // 当值=-1时，不启用倒计时
    var countdownTime: Int = 60
        set(value) {
            field = value
            isTimerVisible = value > 0
        }
    private var currentTime: Int = 0
    private var countdownAutoStart = true

    var backHandler: (() -> Unit)? = null
    var homeHandler: (() -> Unit)? = null

    init {
        context.theme.obtainStyledAttributes(attrs, R.styleable.FragmentToolbar, 0, 0).apply {
            try {
                isBackVisible = getBoolean(R.styleable.FragmentToolbar_backVisible, true)
                isHomeVisible = getBoolean(R.styleable.FragmentToolbar_homeVisible, true)
                title = getString(R.styleable.FragmentToolbar_title) ?: ""
                countdownTime = getInteger(R.styleable.FragmentToolbar_countdownTime, 60)
                currentTime = countdownTime
                countdownAutoStart =
                    getBoolean(R.styleable.FragmentToolbar_countdownAutoStart, true)
            } finally {
                recycle()
            }
        }

        buttonBack.setOnClickListener {
            buttonBack.playSoundEffect(SoundEffectConstants.CLICK)
            backHandler?.invoke()
        }
        buttonHome.setOnClickListener {
            buttonHome.playSoundEffect(SoundEffectConstants.CLICK)
            homeHandler?.invoke()
        }
    }

    private var job: Job? = null

    fun startCountdown(repeatTime: Int = countdownTime) {
        stopCountdown()
        if (repeatTime <= 0) {
            isTimerVisible = false
            return
        }

        currentTime = repeatTime
        job = GlobalScope.launch(Dispatchers.Main) {
            repeat(repeatTime) {
                val num = repeatTime - it
                currentTime = num
                countdownNum.text = num.toString()
                delay(1000)
                if (num <= 1) {
                    stopCountdown()
                    homeHandler?.invoke()
                }
            }
        }
    }

    fun stopCountdown() {
        if (job?.isActive == true) {
            job?.cancel()
        }
    }

    fun resumeCountdown() {
        startCountdown(currentTime)
    }
}