package com.gamedog.vididin.main

import android.app.Activity
import androidx.core.view.isVisible
import com.ama.core.architecture.util.setOnClickBatch
import com.ama.core.architecture.widget.BindingDialog
import com.gamedog.statisticreporter.StatisticUtil
import com.viddin.videos.free.databinding.DialogWatchVideoBinding
import com.gamedog.vididin.router.Router


class WatchAdDialog(context: Activity, private val mWatchAdType: Int, statisticType: Int, private val mGoldNum: Int? = 0,
                    private val mTaskDataJson: String? = "")
    : BindingDialog<DialogWatchVideoBinding>(context, DialogWatchVideoBinding::inflate) {

    private var mOnClosed: (() -> Unit)? = null

    init {
        build()
    }


    private fun build() {
        with()
        setCenter()
        setMaskValue(0.93f)
        setCanCancel(false)

        mBinding.run {
            setOnClickBatch(flAction, ivClose) {
                when (this) {
                    ivClose ->  {
                        mOnClosed?.invoke()
                        dismiss()
                    }
                    flAction ->  {
                        gotoWatchVideo()

                        sendClickStatistic()
                    }
                }
            }

            mGoldNum?.let {
                tvGoldNum.text = "+$mGoldNum"
            }
            if (mGoldNum == null) {
                tvGoldNum.isVisible = false
            }

        }


        sendShowStatistic()
    }



    private fun getShowFromStr(watchAdType: Int): String {
        val fromStr = ""
        when (watchAdType) {
            FROM_WATCH_5VIDEOS -> {
                return "RV_VideoStream"
            }

            FROM_TASK_FRAGMENT_ICON -> {
                return "RV_Coin"
            }

            FROM_CONVERT_GOLD_TO_CASH -> {
                return "RV_Exchange"
            }

            FROM_DIALY_TASKS -> {
                return "RV_Daily_Task"
            }

            FROM_HOME_MENU -> {
                return "RV_Home_Menu"
            }

        }
        return fromStr
    }

    private fun sendShowStatistic() {
        StatisticUtil.reportEvents(StatisticUtil.KEY_RV_Button_Show, mapOf("Position" to getShowFromStr(mWatchAdType)))
    }

    private fun sendClickStatistic() {
        StatisticUtil.reportEvents(StatisticUtil.KEY_RV_Button_Click_Game, mapOf("Position" to getShowFromStr(mWatchAdType)))
    }

    private fun gotoWatchVideo() {
        Router.WatchAd.startActivity(mActivity, mWatchAdType, mTaskDataJson)
        dismiss()
    }


    fun setOnCloseListener(onClose: ()->Unit) {
        mOnClosed = onClose
    }

    companion object {

        val FROM_WATCH_5VIDEOS: Int = 1
        val FROM_TASK_FRAGMENT_ICON: Int = 2
        /*val FROM_SIGN_AD_FOR_DOUBLE: Int = 3
        val FROM_SIGN_AD_FOR_EXTRA: Int = 4
        val FROM_SIGN_AD_FOR_SUPPLEMENT: Int = 5
        val FROM_WITHDRAW_PROGRESS: Int = 6*/
        val FROM_DIALY_TASKS: Int = 7
        val FROM_CONVERT_GOLD_TO_CASH: Int = 8
        val FROM_HOME_MENU: Int = 9



        /*1每观看5个视频后，插入一条激励视频广告。用户可选择观看完15-30秒广告以获得大额金币奖励。
        2"看视频获得金币奖励
        3"签到页面观看视频获得双倍奖励
        4签到页面观看视频获得额外奖励
        5签到页面观看视频进行补签
        6观看视频加速小额进度提现
        7观看视频加速大额进度提现
        8观看视频，可以金币兑换现金*/
    }

}

