package com.gamedog.vididin.manager.helpers

import com.ama.core.architecture.util.DateUtil
import com.gamedog.statisticreporter.StatisticUtil
import com.gamedog.vididin.beans.RECORD_GOLD_PLUS_TASK_DAILY_AD
import com.gamedog.vididin.beans.RECORD_GOLD_PLUS_TASK_DAILY_VIDEO
import com.gamedog.vididin.beans.RecordGold
import com.gamedog.vididin.core.login.login.AccountManager
import com.gamedog.vididin.main.fragments.task.Task
import com.gamedog.vididin.manager.taskbeans.BaseDailyTaskState
import com.gamedog.vididin.manager.taskbeans.BaseTaskState.Companion.STATE_CLAIMED
import com.gamedog.vididin.manager.taskbeans.BaseTaskState.Companion.STATE_FINISH
import com.gamedog.vididin.manager.taskbeans.BaseTaskState.Companion.STATE_ONGOING
import com.gamedog.vididin.manager.taskbeans.TaskStateWatchVideo
import com.gamedog.vididin.manager.taskbeans.TaskStateWatchAd



abstract class BaseDailyTaskHelper<T : BaseDailyTaskState> : BaseTaskHelper<T, Task>() {

    abstract protected fun notifyEvents()
    abstract protected fun generateTodayTask()

    fun claimSubTaskReward(subTaskIndex: Int, isEarnGoldByAd: Boolean) : Boolean {
        if (subTaskIndex >= 0 && subTaskIndex < mStateBean.getSubTaskRewardState().size) {
            try {
                val subTask = mStateBean.getSubTaskRewardState()[subTaskIndex]
                if (subTask.state == STATE_FINISH) {
                    val goldNum = subTask.mRewardNum.toLong()
                    AccountManager.adjustGold(goldNum, RecordGold(
                        if (isEarnGoldByAd) RECORD_GOLD_PLUS_TASK_DAILY_AD else RECORD_GOLD_PLUS_TASK_DAILY_VIDEO, goldNum))

                    subTask.state = STATE_CLAIMED
                    saveState2Sp()
                    notifyEvents()
                    return true
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        return false
    }

    override fun onDateChanged() {
        super.onDateChanged()
        generateTodayTask()
        notifyEvents()
    }


    protected fun calculateNewState() {
        val todayWatchedCount = mStateBean.getTodayWatchedCount()

        mStateBean.getSubTaskRewardState().forEachIndexed { index, subTaskState ->
            val targetCount = subTaskState.mTargetAdCount
            when(subTaskState.state) {
                STATE_ONGOING -> {
                    if (todayWatchedCount >= targetCount) {
                        subTaskState.state = STATE_FINISH


                        StatisticUtil.reportEvents(StatisticUtil.KEY_Daily_Task_Complete, mapOf(
                            "task_type" to if (mStateBean is TaskStateWatchAd) "watch_ad" else "watch_video",
                            "task_id" to (index + 1).toString(),
                            "reward_coins" to subTaskState.mRewardNum,
                        ))
                    }
                }
            }
        }
    }


    protected fun isTodayStatusBean(stateBean: TaskStateWatchAd): Boolean {
        return DateUtil.isToday(stateBean.todayMs)
    }

    protected fun isTodayStatusBean(stateBean: TaskStateWatchVideo): Boolean {
        return DateUtil.isToday(stateBean.todayMs)
    }

}