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.DailyStateWatchVideoBean


class DailyWatchVideoTaskHelper : BaseTaskHelper<DailyStateWatchVideoBean>() {

    override val mSpKey = SpUtil.KEY_DAILY_WATCH


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

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

    private fun notifyEvents() {
        // TODO check read need info
        NotifyMan.instance().sendEvent(VididinEvents.Event_Toady_Watched_Total, NotifyMan.NotifyData(11111L))
    }


    private fun generateTodayTask() {
        mStateBean.clearDatas()
        mStateBean = DailyStateWatchVideoBean(DateUtil.getCurTimeMs())
    }

    private fun isCurBeanInToday(stateBean: DailyStateWatchVideoBean): Boolean {
        return DateUtil.isToday(stateBean.todayStartMs)
    }


    private fun handleVideoWatched(dataPair: Pair<String, Long>) {
        mStateBean.run {
            addWatchedVideoInfo(dataPair)

        }
    }


}