package com.gamedog.vididin.manager.helpers

import com.ama.core.architecture.util.SpUtil
import com.ama.core.architecture.util.eventbus.NotifyMan
import com.gamedog.vididin.VididinEvents
import com.gamedog.vididin.beans.RECORD_GOLD_PLUS_TASK_FIRST_WITHDRAW
import com.gamedog.vididin.beans.RECORD_GOLD_PLUS_TASK_JOIN_DISCORD
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.TaskStateNewBieJoinDiscord

class NewbieJoinDiscordHelper: BaseTaskHelper<TaskStateNewBieJoinDiscord, Task>() {

    override val mSpKey = SpUtil.KEY_NEWBIE_JOIN_DISCORD

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

    private fun generateStateBeanFromConfig(): TaskStateNewBieJoinDiscord {
        return TaskStateNewBieJoinDiscord(mTaskConfig.reward_value)
    }

    fun claimReward() : Boolean {
        if (!mStateBean.hasClaimReward) {
            val goldNum = mStateBean.rewardGoldNum.toLong()
            AccountManager.adjustGold(goldNum, RecordGold(RECORD_GOLD_PLUS_TASK_JOIN_DISCORD, goldNum))
            mStateBean.hasClaimReward = true
            saveState2Sp()
            notifyStateChangeEvent()
            return true
        }
        return false
    }

    private fun notifyStateChangeEvent() {
        NotifyMan.instance().sendEvent(VididinEvents.EVENT_NEWBIE_DISCORD_TASK_CHANGED, null)
    }

}