package com.doverfuelingsolutions.issp.view.widget

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

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

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

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

    private val countdownTime: Int
    private var currentTime: Int = 0
    private var countdownAutoStart = true
    private var countdownEvent: (() -> Unit)? = null

    private var backHandler: (() -> Unit)? = null
    private 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)
                countdownAutoStart = getBoolean(R.styleable.FragmentToolbar_countdownAutoStart, true)
            } finally {
                recycle()
            }
        }

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

    private lateinit var job: Job

    override fun onWindowVisibilityChanged(visibility: Int) {
        super.onWindowVisibilityChanged(visibility)

        if (visibility == VISIBLE) {
            startCountdown()
        } else {
            stopCountdown()
        }
    }

    private fun startCountdown(repeatTime: Int = countdownTime) {
        stopCountdown()
        job = GlobalScope.launch(Dispatchers.Main) {
            repeat(repeatTime) {
                val num = repeatTime - it
                currentTime = num
                countdownNum.text = num.toString()
                delay(1000)
                if (num <= 1) countdownEvent?.invoke()
            }
        }
    }

    private fun stopCountdown() {
        if (this::job.isInitialized && job.isActive) {
            job.cancel()
        }
    }

    fun pauseCountdown() {
        job.cancel()
    }

    fun resumeCountdown() {
        startCountdown(currentTime)
    }

    fun setOnCountdownFinish(listener: () -> Unit) {
        countdownEvent = listener
    }

    fun setBackHandler(handler: () -> Unit) {
        backHandler = handler
    }

    fun setHomeHandler(handler: () -> Unit) {
        homeHandler = handler
    }
}