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.vididin.databinding.DialogDailySignBinding
import com.gamedog.vididin.manager.TaskManager
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()


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

                    actionWatchAd ->  {
                        clickedAdSingleBut()
                    }

                    actionComplement ->  {
                        clickedComplementSign()
                    }
                }
            }
        }
    }

    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("signalAd", {
            if (TaskManager.instance().dailySignStatus().executeDailySign(mCurDayIndex, false, true)) {
                DailySignSuccessDialog(mActivity).initData(mConfigList?.get(mCurDayIndex)?.value, false).show()
                updateUI(mCurDayIndex)
            }
        })
    }

    private fun clickedAdDoubleBut() {
        startAdTask("doubleAd", {
            if (TaskManager.instance().dailySignStatus().executeDailySign(mCurDayIndex, true, true)) {
                DailySignSuccessDialog(mActivity).initData((mConfigList?.get(mCurDayIndex)?.value)?.times(
                    2
                ), false).show()
                updateUI(mCurDayIndex)
            }
        })
    }

    private fun clickedComplementSign() {
        val firstForgotSignDayIndex = TaskManager.instance().dailySignStatus().getForgotSignFirstDayIndex()
        if (firstForgotSignDayIndex > 0) {
            startAdTask("complementAd", {
                if (TaskManager.instance().dailySignStatus().executeDailySign(firstForgotSignDayIndex, false, false)){
                    DailySignSuccessDialog(mActivity).initData(mConfigList?.get(firstForgotSignDayIndex)?.value, false).show()
                    updateUI(firstForgotSignDayIndex)
                }
            })
        } else {
            updateUI(firstForgotSignDayIndex)
        }
    }


    private fun startAdTask(taskUUid: String, completeCallback: ()->Unit) {
        // start ad page with uuidKey, register notify of ad done event
        completeCallback.invoke()
    }




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



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

        if (!curDayStateBean.hasSigned) {
            mBinding.actionNormalTwoButs.isVisible = true
            mBinding.actionWatchAd.isVisible = false
            mBinding.actionComplement.isVisible = false
            mBinding.actionTomorrowSign.isVisible = false
        } 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
                }
            } 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
                } else {
                    mBinding.actionNormalTwoButs.isVisible = false
                    mBinding.actionWatchAd.isVisible = false
                    mBinding.actionComplement.isVisible = false
                    mBinding.actionTomorrowSign.isVisible = true
                }
            }
        }
    }



}

