package com.gamedog.vididin.main.fragments.task

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.gamedog.vididin.VidiConst
import com.gamedog.vididin.VididinEvents
import com.viddin.videos.free.databinding.DialogDailySignBinding
import com.gamedog.vididin.manager.TaskManager
import com.gamedog.vididin.router.Router
import com.gamedog.vididin.widget.DailySignItemView


class DailySignDialog(context: Activity) : BindingDialog<DialogDailySignBinding>(context, DialogDailySignBinding::inflate) {
    private var mCurDayIndex: Int = 0
    private var mConfigList: List<RewardDetail>? = emptyList()
    private var mSignItemViewList: MutableList<DailySignItemView> = mutableListOf()
    private var mBottomButtonsState = 0

    companion object {
        const val STATE_DOUBLE_BUTTONS_SHOWING = 1
        const val STATE_DOUBLE_ONLY_WATCH_AD_SHOWING = 2
        const val STATE_DOUBLE_COMPLEMENT_SHOWING = 3
        const val STATE_DOUBLE_TOMORROW_SHOWING = 4
    }

    init {
        build()
        mConfigList = TaskManager.instance().getDailyCheckInTask()?.reward_details
        initSignItemViews()
        initSignState()
    }

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


        mBinding.run {
            setOnClickBatch(butSign, butWatchAd, actionComplement, actionWatchAd, ivClose) {
                when (this) {
                    ivClose -> {
                        dismiss()
                    }

                    butSign -> {
                        clickedNormalSign()
                    }

                    butWatchAd -> {
                        clickedAdDoubleBut()

                        sendClickStatistics(STATE_DOUBLE_BUTTONS_SHOWING)
                    }

                    actionWatchAd -> {
                        clickedAdSingleBut()

                        sendClickStatistics(STATE_DOUBLE_ONLY_WATCH_AD_SHOWING)
                    }

                    actionComplement -> {
                        clickedComplementSign()

                        sendClickStatistics(STATE_DOUBLE_COMPLEMENT_SHOWING)
                    }
                }
            }
        }



        registerEvents(
            { data ->
                when (data?.mEventType) {
                    VididinEvents.EVENT_AD_WATCHED_FOR_DAILY_SIGN_SINGLE -> {
                        if (TaskManager.instance().dailySignStatus()
                                .executeDailySign(mCurDayIndex, false, true)
                        ) {
                            DailySignSuccessDialog(mActivity).initData(
                                mConfigList?.get(mCurDayIndex)?.value,
                                false
                            ).show()
                            updateUI(mCurDayIndex)
                        }
                    }

                    VididinEvents.EVENT_AD_WATCHED_FOR_DAILY_SIGN_DOUBLE -> {
                        if (TaskManager.instance().dailySignStatus()
                                .executeDailySign(mCurDayIndex, true, true)
                        ) {
                            DailySignSuccessDialog(mActivity).initData(
                                (mConfigList?.get(mCurDayIndex)?.value)?.times(
                                    2
                                ), false
                            ).show()
                            updateUI(mCurDayIndex)
                        }
                    }

                    VididinEvents.EVENT_AD_WATCHED_FOR_DAILY_SIGN_COMPLEMENT -> {
                        val firstForgotSignDayIndex =
                            TaskManager.instance().dailySignStatus().getForgotSignFirstDayIndex()
                        if (TaskManager.instance().dailySignStatus()
                                .executeDailySign(firstForgotSignDayIndex, false, false)
                        ) {
                            DailySignSuccessDialog(mActivity).initData(
                                mConfigList?.get(
                                    firstForgotSignDayIndex
                                )?.value, false
                            ).show()
                            updateUI(firstForgotSignDayIndex)
                        }
                    }
                }
            }, VididinEvents.EVENT_AD_WATCHED_FOR_DAILY_SIGN_SINGLE,
            VididinEvents.EVENT_AD_WATCHED_FOR_DAILY_SIGN_DOUBLE,
            VididinEvents.EVENT_AD_WATCHED_FOR_DAILY_SIGN_COMPLEMENT
        )
    }

    private fun clickedNormalSign() {
        if (TaskManager.instance().dailySignStatus().executeDailySign(mCurDayIndex, false, false)) {
            DailySignSuccessDialog(mActivity).initData(mConfigList?.get(mCurDayIndex)?.value, true)
                .show()
            updateUI(mCurDayIndex)
        }
    }

    private fun clickedAdSingleBut() {
        startAdTask(VidiConst.WATCH_AD_FOR_DAILY_SIGN_SINGLE)
    }

    private fun clickedAdDoubleBut() {
        startAdTask(VidiConst.WATCH_AD_FOR_DAILY_SIGN_DOUBLE)
    }

    private fun clickedComplementSign() {
        val firstForgotSignDayIndex =
            TaskManager.instance().dailySignStatus().getForgotSignFirstDayIndex()
        if (firstForgotSignDayIndex > 0) {
            startAdTask(VidiConst.WATCH_AD_FOR_DAILY_SIGN_COMPLEMENT)
        } else {
            updateUI(firstForgotSignDayIndex)
        }
    }


    private fun startAdTask(startType: Int) {
        Router.WatchAd.startActivity(mActivity, startType)
    }


    private fun initSignItemViews() {
        with(mBinding) {
            signItem7.setLargeWidthMode()

            mSignItemViewList.add(signItem1)
            mSignItemViewList.add(signItem2)
            mSignItemViewList.add(signItem3)
            mSignItemViewList.add(signItem4)
            mSignItemViewList.add(signItem5)
            mSignItemViewList.add(signItem6)
            mSignItemViewList.add(signItem7)


            mConfigList?.let {
                mSignItemViewList.forEachIndexed { index, itemView ->
                    itemView.setData(
                        mConfigList!![index],
                        TaskManager.instance().dailySignStatus().getDailySignStateBean(index)
                    )
                }
            }
        }
    }

    private fun initSignState() {
        mCurDayIndex = TaskManager.instance().dailySignStatus().getCurDayIndexOfDailyCheckIn()
        mSignItemViewList[mCurDayIndex].setSelectState(true)
        updateUI(mCurDayIndex)

        mSignItemViewList[mCurDayIndex].setOnClickListener {
            val curDayView = mSignItemViewList[mCurDayIndex]
            val curDayStateBean = curDayView.getStateBean()
            if (!curDayStateBean.hasSigned) {
                clickedNormalSign()
            }
        }
    }


    private fun updateUI(signedDayIndex: Int) {
        val curDayView = mSignItemViewList[mCurDayIndex]
        val curDayStateBean = curDayView.getStateBean()


        mBinding.tvSignedTotal.text = buildString {
            append(TaskManager.instance().dailySignStatus().getSignDaysTotal())
            append("/7")
        }
        mSignItemViewList[signedDayIndex].updateUI()

        var newButtonsState = mBottomButtonsState
        if (!curDayStateBean.hasSigned) {
            mBinding.actionNormalTwoButs.isVisible = true
            mBinding.actionWatchAd.isVisible = false
            mBinding.actionComplement.isVisible = false
            mBinding.actionTomorrowSign.isVisible = false

            newButtonsState = STATE_DOUBLE_BUTTONS_SHOWING
        } else {
            if (!curDayStateBean.hasWatchedAd) {
                mBinding.actionNormalTwoButs.isVisible = false
                mBinding.actionWatchAd.isVisible = true
                mBinding.actionComplement.isVisible = false
                mBinding.actionTomorrowSign.isVisible = false

                /*mBinding.tvGoldNum.text = buildString {
                    "+"
                    mConfigList?.get(mCurDayIndex)?.value
                }*/

                newButtonsState = STATE_DOUBLE_ONLY_WATCH_AD_SHOWING
            } else {
                val forgotSignDays = TaskManager.instance().dailySignStatus().getForgotSignDays()
                if (forgotSignDays > 0) {
                    mBinding.actionNormalTwoButs.isVisible = false
                    mBinding.actionWatchAd.isVisible = false
                    mBinding.actionComplement.isVisible = true
                    mBinding.actionTomorrowSign.isVisible = false

                    newButtonsState = STATE_DOUBLE_COMPLEMENT_SHOWING
                } else {
                    mBinding.actionNormalTwoButs.isVisible = false
                    mBinding.actionWatchAd.isVisible = false
                    mBinding.actionComplement.isVisible = false
                    mBinding.actionTomorrowSign.isVisible = true

                    newButtonsState = STATE_DOUBLE_TOMORROW_SHOWING
                }
            }
        }


        if (newButtonsState != mBottomButtonsState) {
            mBottomButtonsState = newButtonsState

            sendShowStatistics()
        }
    }

    private fun sendShowStatistics() {
        var fromType = ""
        when (mBottomButtonsState) {
            STATE_DOUBLE_BUTTONS_SHOWING -> {
                fromType = "RV_Double"
            }

            STATE_DOUBLE_ONLY_WATCH_AD_SHOWING -> {
                fromType = "RV_Extra"
            }

            STATE_DOUBLE_COMPLEMENT_SHOWING -> {
                fromType = "RV_SupplementarySigning"
            }
        }

        if (fromType.isNotEmpty()) {
            StatisticUtil.reportEvents(StatisticUtil.KEY_RV_Button_Show, mapOf("Position" to fromType))
        }
    }

    private fun sendClickStatistics(clickedType: Int) {
        var fromType = ""
        when (clickedType) {
            STATE_DOUBLE_BUTTONS_SHOWING -> {
                fromType = "RV_Double"
            }

            STATE_DOUBLE_ONLY_WATCH_AD_SHOWING -> {
                fromType = "RV_Extra"
            }

            STATE_DOUBLE_COMPLEMENT_SHOWING -> {
                fromType = "RV_SupplementarySigning"
            }
        }

        if (fromType.isNotEmpty()) {
            StatisticUtil.reportEvents(StatisticUtil.KEY_RV_Button_Click_Game, mapOf("Position" to fromType))
        }
    }

}

