package com.gamedog.vididin.features.watchad

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.os.PersistableBundle
import android.view.LayoutInflater
import androidx.activity.addCallback
import androidx.activity.viewModels
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import com.ama.core.architecture.appBase.AppViewsActivity
import com.ama.core.architecture.ext.toast
import com.ama.core.architecture.util.AndroidUtil
import com.ama.core.architecture.util.eventbus.NotifyMan
import com.gamedog.vididin.R
import com.gamedog.vididin.main.interfaces.OnTabStyleListener
import dagger.hilt.android.AndroidEntryPoint
import kotlin.getValue
import com.gamedog.vididin.databinding.ActivityWatchAdBinding as ViewBinding
import com.gamedog.vididin.main.MainUiState as UiState
import com.gamedog.vididin.main.MainViewModel as ViewModel


@AndroidEntryPoint
class WatchAdActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(), OnTabStyleListener {

    override val mViewModel: ViewModel by viewModels()
    override fun inflateViewBinding(inflater: LayoutInflater) = ViewBinding.inflate(inflater)


    private lateinit var mCountDownTimer: CountDownTimer
    private var mTaskUUID: Int = 0

    override fun ViewBinding.initViews() {
        with(binding) {

        }

    }

    override fun ViewBinding.initWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(contentRoot) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout())
            v.updatePadding(top = systemBars.top)
            insets
        }
    }

    override fun ViewBinding.initListeners() {
        onBackPressedDispatcher.addCallback(this@WatchAdActivity) {
            AndroidUtil.showToast("Can't exit while watching video")
        }

        stateCounter()
    }


    private fun notifyAdWatchFinish() {
        NotifyMan.instance().sendEvent(mTaskUUID, null)
    }

    private fun stateCounter() {
        mCountDownTimer = object : CountDownTimer(5000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                val secondsRemaining = millisUntilFinished / 1000
                binding.tvAdCounter.text = "${secondsRemaining}"
            }

            override fun onFinish() {
                notifyAdWatchFinish()
                finish()
            }
        }
        mCountDownTimer.start()
    }

    override fun readIntent(intent: Intent) {
        super.readIntent(intent)
        mTaskUUID = intent.getIntExtra(KEY_TASKID, 0)
    }


    override fun onDestroy() {
        super.onDestroy()
        mCountDownTimer.cancel()
    }

    override fun ViewBinding.initObservers() {
        //TODO("Not yet implemented")
    }

    override fun ViewBinding.onUiStateCollect(uiState: UiState) {
        //TODO("Not yet implemented")
    }

    override fun onTabIsDarkFont(isDarkFont: Boolean) {
        //TODO("Not yet implemented")
    }


    companion object {
        private val KEY_TASKID = "KEY_TASKID"

        internal fun startActivity(activity: Activity, taskUUID: Int) {
            val intent = Intent(activity.applicationContext, WatchAdActivity::class.java)
            intent.putExtra(KEY_TASKID, taskUUID)
            activity.startActivity(intent)
        }
    }

}