package com.gamedog.vididin.features.watchad

import android.app.Activity
import android.content.Intent
import android.os.CountDownTimer
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.util.AndroidUtil
import com.ama.core.architecture.util.eventbus.NotifyMan
import com.gamedog.vididin.VidiConst
import com.gamedog.vididin.VididinEvents
import com.gamedog.vididin.main.fragments.task.DailySignSuccessDialog
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 mTaskType: Int = 0
    private var mTaskData: String? = null

    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(mTaskType, null)

        if (mTaskType != VididinEvents.Event_AD_TASK_TYPE_Watch_Ad_Earn_Gold) {
            NotifyMan.instance().sendEvent(VididinEvents.Event_Finish_One_Ad, NotifyMan.NotifyData(1))
        }
    }

    private fun stateCounter() {
        mCountDownTimer = object : CountDownTimer(3000, 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)
        mTaskType = intent.getIntExtra(KEY_TASK_TYPE, 0)
        mTaskData = intent.getStringExtra(KEY_TASK_DATA)
    }


    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_TASK_TYPE = "KEY_TASK_TYPE"
        private val KEY_TASK_DATA = "KEY_TASK_DATA"

        internal fun startActivity(activity: Activity, taskType: Int, taskDataJson: String?) {
            val intent = Intent(activity.applicationContext, WatchAdActivity::class.java)
            intent.putExtra(KEY_TASK_TYPE, taskType)
            intent.putExtra(KEY_TASK_DATA, taskDataJson)
            activity.startActivity(intent)
        }
    }

}