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.manager.taskbeans.TaskStateWatchAd
import com.gamedog.vididin.manager.taskbeans.TaskStateWatchVideo


class DailyWatchVideoTaskHelper : BaseDailyTaskHelper<TaskStateWatchVideo>()  {

    override val mSpKey = SpUtil.KEY_DAILY_WATCH_VIDEO


    init {
        registerEvents( { eventData->
            val dataPair: Pair<String, Long> = eventData?.mData as Pair<String, Long>
            handleOneVideoWatched(dataPair)
        }, VididinEvents.Event_Finish_One_Video)
    }

    override fun loadTaskFromSp() {
        val spStateBean = SpUtil.instance().getObject<TaskStateWatchVideo>(mSpKey)
        if (spStateBean == null || !isTodayStatusBean(spStateBean)) {
            generateTodayTask()
        } else {
            mStateBean = spStateBean
            notifyEvents()
        }
    }

    override fun generateTodayTask() {
        mStateBean = TaskStateWatchVideo(DateUtil.getCurTimeMs())
        mStateBean.initSubTaskRewardList(mTaskConfig.reward_details)
        saveState2Sp()
    }

    override fun notifyEvents() {
        NotifyMan.instance().sendEvent(VididinEvents.EVENT_DAILY_WATCHED_VIDEO_NUM_CHANGED, null)
    }


    private fun handleOneVideoWatched(dataPair: Pair<String, Long>) {
        mStateBean.run {
            if (!isAllTaskFinish()) {
                if (addWatchedVideoInfo(dataPair)) {
                    calculateNewState()
                    saveState2Sp()
                    notifyEvents()
                }
            }
        }
    }


}