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

import android.content.Context
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.DailySignBean
import com.gamedog.vididin.manager.TaskManager
import com.gamedog.vididin.widget.DailySignItemView


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


    init {
        build()
        mRewardDetailList = 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, ivClose) {
                when (this) {
                    ivClose ->  {
                        dismiss()
                    }
                    butSign ->  {
                        handleNormalSignClicked()
                    }
                    butWatchAd ->  {
                        gotoWatchAdPage()
                        dismiss()
                    }
                }
            }
        }
    }

    private fun handleNormalSignClicked() {
        val rewardConfig = mRewardDetailList?.get(mSelectedDayIndex)
        if (TaskManager.instance().executeDailySign(mSelectedDayIndex, rewardConfig?.value ?: 0)) {
            DailySignSuccessDialog(context).setRewardNum(mRewardDetailList?.get(mCurDayIndex)?.value).show()
        }
    }


    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)

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

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



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

        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
            } else {
                val forgotSignDays = TaskManager.instance().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
                }
            }
        }
    }


    private fun gotoWatchAdPage() {

    }

}

