package com.gamedog.vididin.manager

import com.ama.core.architecture.util.FileUtil
import com.gamedog.vididin.main.fragments.task.Task
import com.gamedog.vididin.main.fragments.task.TaskBean
import com.gamedog.vididin.manager.helpers.DailyBoxHelper
import com.gamedog.vididin.manager.helpers.DailySignTaskHelper
import com.gamedog.vididin.manager.helpers.DailyWatchVideoTaskHelper
import com.gamedog.vididin.manager.helpers.NewbieEnableNotifyHelper
import com.gamedog.vididin.manager.helpers.NewbieFirstWithdrawHelper
import com.gamedog.vididin.manager.helpers.NewbieJoinDiscordHelper
import com.google.gson.GsonBuilder
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.*

class TaskManager private constructor() {
    companion object {
        // 新手任务分类及其子类
        private const val TAG_CATEGORY_NEWBIE_TASK = "newbie_task"
        private const val TAG_TASK_NEWBIE_FIRST_WITHDRAW = "newbie_first_withdraw"
        private const val TAG_TASK_NEWBIE_PUSH_NOTIFY = "newbie_push_notify"
        private const val TAG_TASK_NEWBIE_JOIN_DISCORD = "newbie_join_discord"
        private const val TAG_TASK_NEWBIE_7DAY_CHECKIN = "newbie_7day_checkin"

        // 日常任务分类及其子类
        private const val TAG_CATEGORY_DAILY_TASK = "daily_task"
        private const val TAG_TASK_DAILY_CHECKIN = "daily_checkin"
        private const val TAG_TASK_DAILY_VIDEO = "daily_video_ladder"
        private const val TAG_TASK_DAILY_AD = "daily_ad_ladder"

        @Volatile
        private var instance: TaskManager? = null

        fun instance(): TaskManager {
            return instance ?: synchronized(this) {
                instance ?: TaskManager().also {
                    instance = it
                }
            }
        }
    }




    @Volatile
    private var initialized = false
    private val mutex = Mutex()
    private val mGson = GsonBuilder().create()
    private var mTaskConfig: TaskBean? = null
    private val mDailySignTaskHelper by lazy { DailySignTaskHelper() }
    private val mDailyWatchVideoTaskHelper by lazy { DailyWatchVideoTaskHelper() }
    private val mDailyBoxHelper by lazy { DailyBoxHelper() }
    private val mNewbieJoinDiscordHelper by lazy { NewbieJoinDiscordHelper() }
    private val mNewbieEnableNotifyHelper by lazy { NewbieEnableNotifyHelper() }
    private val mNewbieFirstWithdrawHelper by lazy { NewbieFirstWithdrawHelper() }




    init {
        loadTaskConfigAsync()

        // TODO - remove test code
        if (true) {
            mDailySignTaskHelper.generateTestDailySignTestBean()
        }
    }


    suspend fun getTaskConfig(): TaskBean? = mutex.withLock {
        if (!initialized) {
            loadTaskConfigFromAsset()
        }
        return mTaskConfig
    }

    private fun loadTaskConfigAsync() {
        CoroutineScope(Dispatchers.IO).launch {
            mutex.withLock {
                if (!initialized) {
                    loadTaskConfigFromAsset()
                    initHelpers()
                }
            }
        }
    }

    private suspend fun loadTaskConfigFromAsset() {
        return withContext(Dispatchers.IO) {
            val configStr = FileUtil.readAssetsFile("taskConfig.json")
            mTaskConfig = mGson.fromJson(configStr, TaskBean::class.java)
            initialized = true
        }
    }

    private suspend fun initHelpers() {
        mDailySignTaskHelper.initDatas(getDailyCheckInTask()!!)
        mDailyWatchVideoTaskHelper.initDatas(getDailyVideoTask()!!)
        mDailyBoxHelper.initDatas(getDailyVideoTask()!!) // TODO - get right box task config
        mNewbieJoinDiscordHelper.initDatas(getNewbieJoinDiscordTask()!!)
        mNewbieEnableNotifyHelper.initDatas(getNewbieEnableNotifyTask()!!)
        mNewbieFirstWithdrawHelper.initDatas(getNewbieFirstWithdrawTask()!!)
    }


    //------------------------- Tasks Getter ------------------------------
    fun dailySignStatus(): DailySignTaskHelper {
        return mDailySignTaskHelper
    }

    fun dailyWatchStatus(): DailyWatchVideoTaskHelper {
        return mDailyWatchVideoTaskHelper
    }

    fun dailyBoxStatus(): DailyBoxHelper {
        return mDailyBoxHelper
    }

    fun newbieJoinDiscordStatus(): NewbieJoinDiscordHelper {
        return mNewbieJoinDiscordHelper
    }

    fun newbieEnableNotifyStatus(): NewbieEnableNotifyHelper {
        return mNewbieEnableNotifyHelper
    }

    fun newbieFirstWithdrawStatus(): NewbieFirstWithdrawHelper {
        return mNewbieFirstWithdrawHelper
    }




    //-------------------------  Get tasks  -------------------------------
    // 日常任务
    fun getDailyCheckInTask(): Task? {
        return mTaskConfig?.task_module_config?.getTaskCategoryById(TAG_CATEGORY_DAILY_TASK)?.getTaskById(TAG_TASK_DAILY_CHECKIN)
    }

    fun getDailyVideoTask(): Task? {
        return mTaskConfig?.task_module_config?.getTaskCategoryById(TAG_CATEGORY_DAILY_TASK)?.getTaskById(TAG_TASK_DAILY_VIDEO)
    }

    fun getDailyAdTask(): Task? {
        return mTaskConfig?.task_module_config?.getTaskCategoryById(TAG_CATEGORY_DAILY_TASK)?.getTaskById(TAG_TASK_DAILY_AD)
    }

    // 新手任务
    fun getNewbieFirstWithdrawTask(): Task? {
        return mTaskConfig?.task_module_config?.getTaskCategoryById(TAG_CATEGORY_NEWBIE_TASK)?.getTaskById(TAG_TASK_NEWBIE_FIRST_WITHDRAW)
    }

    fun getNewbieEnableNotifyTask(): Task? {
        return mTaskConfig?.task_module_config?.getTaskCategoryById(TAG_CATEGORY_NEWBIE_TASK)?.getTaskById(TAG_TASK_NEWBIE_PUSH_NOTIFY)
    }

    fun getNewbieJoinDiscordTask(): Task? {
        return mTaskConfig?.task_module_config?.getTaskCategoryById(TAG_CATEGORY_NEWBIE_TASK)?.getTaskById(TAG_TASK_NEWBIE_JOIN_DISCORD)
    }

    fun getNewbieCheckInTask(): Task? {
        return mTaskConfig?.task_module_config?.getTaskCategoryById(TAG_CATEGORY_NEWBIE_TASK)?.getTaskById(TAG_TASK_NEWBIE_7DAY_CHECKIN)
    }





    //----------------------------- 每日签到 -----------------------------//

    fun getHomeWatchDurationRewardNum(): Int {
        return 28;  // TODO - mTaskConfigBean.xxxx
    }




}



