package com.gh.common.util

import com.gh.common.util.ViewPagerFragmentHelper.TYPE_MY_HALO
import com.gh.gamecenter.R
import com.gh.gamecenter.common.entity.Display
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.utils.toJson
import com.gh.gamecenter.core.utils.GsonUtils
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.entity.BottomTab
import com.gh.gamecenter.entity.SubjectRecommendEntity
import com.google.gson.reflect.TypeToken
import com.halo.assistant.HaloApp

object HomeBottomBarHelper {
    private const val GAME_BAR_KEY = "game_bar_key"
    private const val KEY_HOME_BOTTOM_TAB = "home_bottom_tab"

    @JvmStatic
    fun getDefaultGameBarData(): SubjectRecommendEntity {
        try {
            val json = SPUtils.getString(GAME_BAR_KEY)
            if (json.isNotEmpty()) {
                return GsonUtils.fromJson(json, SubjectRecommendEntity::class.java)
            }
        } catch (ignore: Exception) {

        }
        return SubjectRecommendEntity(
            link = "5de21b5d75e6fa054f784882",
            type = "block",
            text = "游戏库",
            name = "游戏库",
            position = 2,
            default = false,
            display = Display()
        )
    }

    @JvmStatic
    fun getDefaultBottomTabHomeEntity(): BottomTab {
        val animationCode = HaloApp.getInstance().application.assets
            .open("lottie/tab_home.json")
            .bufferedReader().use { it.readText() }
        return BottomTab(name = "首页", jsCode = animationCode, iconSelector = R.drawable.selector_ic_home, default = true)
    }

    @JvmStatic
    fun getDefaultBottomTabMineEntity(): BottomTab {
        val animationCode = HaloApp.getInstance().application.assets
            .open("lottie/tab_mine.json")
            .bufferedReader().use { it.readText() }
        return BottomTab(name = "我的光环", jsCode = animationCode, iconSelector = R.drawable.selector_ic_user, link = LinkEntity(type = TYPE_MY_HALO))
    }

    @JvmStatic
    fun getDefaultHomeBottomTabData(): List<BottomTab> {
        try {
            val json = SPUtils.getString(KEY_HOME_BOTTOM_TAB)
            if (json.isNotEmpty()) {
                val type = object : TypeToken<List<BottomTab>>() {}.type
                return GsonUtils.gson.fromJson(json, type)
            }
        } catch (ignore: Exception) {

        }
        return listOf(getDefaultBottomTabHomeEntity(), getDefaultBottomTabMineEntity())
    }

    @JvmStatic
    fun updateDefaultHomeBottomTabData(data: List<BottomTab>) {
        SPUtils.setString(KEY_HOME_BOTTOM_TAB, data.toJson())
    }
}