package com.gamedog.vididin.manager.helpers

import com.ama.core.architecture.util.DateUtil
import com.ama.core.architecture.util.SpUtil
import com.ama.core.architecture.util.eventbus.NotifyMan
import com.gamedog.vididin.VididinEvents
import com.gamedog.vididin.core.login.login.AccountManager
import com.gamedog.vididin.main.fragments.task.Task
import com.gamedog.vididin.manager.taskbeans.TaskDailySignBean
import com.gamedog.vididin.manager.taskbeans.DailySignDayInfoBean


class DailySignTaskHelper : BaseTaskHelper<TaskDailySignBean, Task>() {
    private var mCurDayIndexDailySign: Int = 0
    override val mSpKey = SpUtil.KEY_TASK_BEAN


    override fun loadTaskFromSp() {
        val taskStateBeanInSp = SpUtil.instance().getObject<TaskDailySignBean>(mSpKey)
        if (taskStateBeanInSp == null) {
            mStateBean = TaskDailySignBean()
            saveState2Sp()
        } else {
            mStateBean = taskStateBeanInSp
        }
    }

    fun getCurDayIndexOfDailyCheckIn() : Int {
        var dayIndexOfPeriod = 0

        val periodStartMs = mStateBean.startDurationMs
        if (periodStartMs > 0) {
            dayIndexOfPeriod = DateUtil.getPassedDayNum(periodStartMs, DateUtil.getCurTimeMs())
            if (dayIndexOfPeriod > 6) {
                mStateBean?.startDurationMs = DateUtil.getCurTimeMs()
                saveState2Sp()
            }
        } else {
            mStateBean?.startDurationMs = DateUtil.getCurTimeMs()
            saveState2Sp()
        }

        mCurDayIndexDailySign = dayIndexOfPeriod
        return dayIndexOfPeriod
    }

    fun getDailySignStateBean(dayIndex: Int): DailySignDayInfoBean {
        return mStateBean.signStateList[dayIndex]
    }

    fun executeDailySign(dayIndex: Int, isDoubleReward: Boolean, isByAd: Boolean) : Boolean {
        val daySignState = mStateBean.signStateList[dayIndex]

        if (!daySignState.hasSigned || (dayIndex == mCurDayIndexDailySign && !daySignState.hasWatchedAd)) {
            val dayReward = getRewardNumOfDailySign(dayIndex)
            val finalReward = if (isDoubleReward) 2*dayReward else dayReward
            daySignState.hasRewardedNum += finalReward
            daySignState.hasSigned = true
            daySignState.hasWatchedAd = isByAd
            saveState2Sp()
            AccountManager.addGold(finalReward)
            notifySignStateChanged(Pair(dayIndex, daySignState))
            return true
        }
        return false
    }

    private fun notifySignStateChanged(dataPair: Pair<Int, DailySignDayInfoBean>) {
        NotifyMan.instance().sendEvent(VididinEvents.Event_Sign_State_Changed, NotifyMan.NotifyData(dataPair))
        NotifyMan.instance().sendEvent(VididinEvents.Event_Finish_One_Sign, null)
    }

    fun generateTestDailySignTestBean() {
        mStateBean = TaskDailySignBean().apply {
            startDurationMs = DateUtil.getCurTimeMs() - 4 * DateUtil.MS_NUM_ONE_DAY
            signStateList.clear()
            signStateList.add(DailySignDayInfoBean(hasSigned = true, hasWatchedAd = true))
            signStateList.add(DailySignDayInfoBean(hasSigned = false, hasWatchedAd = false))
            signStateList.add(DailySignDayInfoBean(hasSigned = true, hasWatchedAd = false))
            signStateList.add(DailySignDayInfoBean(hasSigned = false, hasWatchedAd = false))
            signStateList.add(DailySignDayInfoBean(hasSigned = false, hasWatchedAd = false))
            signStateList.add(DailySignDayInfoBean(hasSigned = false, hasWatchedAd = false))
            signStateList.add(DailySignDayInfoBean(hasSigned = false, hasWatchedAd = false))
        }

        saveState2Sp()
        AccountManager.saveBankAccount(null)
    }

    fun getForgotSignDays(): Int {
        var forgotSignDays = 0
        val curDayIndex = getCurDayIndexOfDailyCheckIn()
        mStateBean.signStateList.forEachIndexed { index, item->
            if (!item.hasSigned && index < curDayIndex) {
                forgotSignDays++
            }
        }
        return forgotSignDays
    }

    fun getSignDaysTotal(): Int {
        var signDays = 0
        mStateBean.signStateList.forEachIndexed { index, item->
            if (item.hasSigned) {
                signDays++
            }
        }
        return signDays
    }

    fun getForgotSignFirstDayIndex(): Int {
        val curDayIndex = getCurDayIndexOfDailyCheckIn()
        mStateBean.signStateList.forEachIndexed { index, item->
            if (!item.hasSigned && index < curDayIndex) {
                return index
            }
        }
        return -1;
    }


    fun getDayStateList(): List<DailySignDayInfoBean> {
        return mStateBean.signStateList
    }

    fun getRewardNumOfDailySign(dayIndex: Int) : Int {
        return  mTaskConfig.reward_details[dayIndex].value
    }

    fun isDailySignAllOperationDone(): Boolean {
        val curDayState = getDailySignStateBean(mCurDayIndexDailySign)
        return curDayState.hasSigned && curDayState.hasWatchedAd && getForgotSignFirstDayIndex() <= 0
    }

}