package com.gamedog.vididin.features.watchad

import android.app.Activity
import android.content.Intent
import android.os.CountDownTimer
import android.view.LayoutInflater
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope
import com.ama.core.architecture.appBase.AppViewsEmptyViewModelActivity
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.beans.WatchAdNotifyBean
import com.gamedog.vididin.main.interfaces.OnTabStyleListener
import com.gamedog.vididin.manager.WithdrawItem
import com.remax.bill.ads.AdResult
import com.remax.bill.ads.ext.AdShowExt
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import com.viddin.videos.free.databinding.ActivityWatchAdBinding as ViewBinding



@AndroidEntryPoint
class WatchAdActivity : AppViewsEmptyViewModelActivity<ViewBinding>(), OnTabStyleListener {

    override fun inflateViewBinding(inflater: LayoutInflater) = ViewBinding.inflate(inflater)
    private var mRewardedDollarNum: Double = 0.0
    override var mEnableBannerAd = false

    private lateinit var mCountDownTimer: CountDownTimer
    private var mWatchType: Int = 0
    private var mExtraJsonStr: 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()

        showVideoAd()
    }


    private fun notifyAdWatchFinish() {
        var notifyEventType = 0
        var notifyData: NotifyMan.NotifyData<Any> = NotifyMan.NotifyData()
        var shouldNotifyOneAdWatched = true


        when (mWatchType) {
            VidiConst.WATCH_AD_FOR_WITHDRAW_SMALL -> {
                shouldNotifyOneAdWatched = true
                notifyEventType = VididinEvents.EVENT_AD_WATCHED_FOR_WITHDRAW_SMALL

                val extraData = mExtraJsonStr?.let {
                    AndroidUtil.json2Object<Float>(mExtraJsonStr!!)
                }
                notifyData.mData = WatchAdNotifyBean(mRewardedDollarNum, extraData)
            }

            VidiConst.WATCH_AD_FOR_WITHDRAW_BIG -> {
                shouldNotifyOneAdWatched = true
                notifyEventType = VididinEvents.EVENT_AD_WATCHED_FOR_WITHDRAW_BIG

                val extraData = mExtraJsonStr?.let {
                    AndroidUtil.json2Object<WithdrawItem>(mExtraJsonStr!!)
                }
                notifyData.mData = WatchAdNotifyBean(mRewardedDollarNum, extraData)
            }

            VidiConst.WATCH_AD_FOR_DAILY_SIGN_SINGLE -> {
                shouldNotifyOneAdWatched = true
                notifyEventType = VididinEvents.EVENT_AD_WATCHED_FOR_DAILY_SIGN_SINGLE
            }

            VidiConst.WATCH_AD_FOR_DAILY_SIGN_DOUBLE-> {
                shouldNotifyOneAdWatched = true
                notifyEventType = VididinEvents.EVENT_AD_WATCHED_FOR_DAILY_SIGN_DOUBLE
            }

            VidiConst.WATCH_AD_FOR_DAILY_SIGN_COMPLEMENT -> {
                shouldNotifyOneAdWatched = true
                notifyEventType = VididinEvents.EVENT_AD_WATCHED_FOR_DAILY_SIGN_COMPLEMENT
            }

            VidiConst.WATCH_AD_FOR_BOX_TASK -> {
                shouldNotifyOneAdWatched = true
                notifyEventType = VididinEvents.EVENT_AD_WATCHED_FOR_BOX_TASK
            }

            VidiConst.WATCH_AD_FOR_ZERO_EARN_DIAMOND -> {
                shouldNotifyOneAdWatched = false
                notifyEventType = VididinEvents.EVENT_AD_WATCHED_FOR_ZEROBUY_EARN_DIAMOND
            }

            VidiConst.WATCH_AD_FOR_DAILY_WATCH_AD -> {
                shouldNotifyOneAdWatched = true
                notifyEventType = VididinEvents.EVENT_AD_WATCHED_FOR_DAILY_WATCH_AD
            }

            VidiConst.WATCH_AD_FOR_DAILY_EARN_GOLD -> {
                shouldNotifyOneAdWatched = false
                notifyEventType = VididinEvents.EVENT_AD_WATCHED_FOR_EARN_GOLD
            }

            VidiConst.WATCH_AD_FOR_CONVERT_GOLD_2_CASH -> {
                shouldNotifyOneAdWatched = true
                notifyEventType = VididinEvents.EVENT_AD_WATCHED_FOR_CONVERT_GOLD_2_CASH
            }
        }

        NotifyMan.instance().sendEvent(notifyEventType, notifyData)
        if (shouldNotifyOneAdWatched) {
            NotifyMan.instance().sendEvent(VididinEvents.EVENT_FINISHED_ONE_AD, NotifyMan.NotifyData(1))
        }

        finish()
    }

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

            override fun onFinish() {

            }
        }
        mCountDownTimer.start()
    }

    override fun readIntent(intent: Intent) {
        super.readIntent(intent)
        mWatchType = intent.getIntExtra(KEY_TASK_TYPE, 0)
        mExtraJsonStr = intent.getStringExtra(KEY_TASK_DATA)
    }


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

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


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


    private fun showVideoAd() {
        lifecycleScope.launch {
            try {
                when(AdShowExt.showRewardedVideoAd(this@WatchAdActivity, { rewardedData ->
                    mRewardedDollarNum = rewardedData.rewardNum
                }, {
                    notifyAdWatchFinish()
                })) {
                    is AdResult.Success -> {
                        val temp = 111
                    }

                    is AdResult.Failure -> {
                        val temp = 111
                    }

                    AdResult.Loading -> {
                        val temp = 111
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }



    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)
        }
    }

}