package com.gh.gamecenter.servers.gametest2

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.common.constant.Config
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.core.utils.SPUtils

class GameServerTestV2ViewModel(application: Application) : AndroidViewModel(application) {

    val timeFilterList = arrayListOf(RECENT_GAME, TODAY_GAME, FUTURE_GAME)
    val categoryFilterList = arrayListOf("单机游戏", "网络游戏", "福利游戏", "国际服游戏")
    var selectedCategoryFilterSet = SPUtils.getStringSet(Constants.SP_GAME_SERVER_TEST_V2_CATEGORY_SET).toHashSet()
    var tempCategoryFilterSet = SPUtils.getStringSet(Constants.SP_GAME_SERVER_TEST_V2_CATEGORY_SET).toHashSet()
    var selectedTimeFilterLiveData = MutableLiveData(timeFilterList[GameServerTestV2Fragment.DEFAULT_TIME_FILTER_INDEX])
    var bigImageMode = SPUtils.getBoolean(
        Constants.SP_GAME_SERVER_TEST_V2_BIG_IMAGE_MODE,
        Config.getSettings() == null || Config.getSettings()?.columnTestV2Setting?.defaultView == "on"
    )

    fun getCategoryFilterString(): String {
        var selectedCategoryFilter = "全部类型"
        selectedCategoryFilterSet.forEachIndexed { index, category ->
            if (index == 0) {
                selectedCategoryFilter = category
            } else {
                selectedCategoryFilter += "+$category"
            }
        }
        return selectedCategoryFilter
    }

    fun getCategoryFilterValue(category: String): GameCategory {
        return when (category) {
            "单机游戏" -> GameCategory.LOCAL
            "网络游戏" -> GameCategory.ONLINE
            "福利游戏" -> GameCategory.WELFARE
            "国际服游戏" -> GameCategory.GJONLINE
            else -> GameCategory.ALL
        }
    }

    fun getSelectedTimeFilterString(dayFilter: String): String {
        return when (dayFilter) {
            TimeFilter.PAST_DAY.value -> RECENT_GAME
            TimeFilter.TODAY.value -> TODAY_GAME
            TimeFilter.UPCOMING_DAY.value -> FUTURE_GAME
            else -> TODAY_GAME
        }
    }

    fun getCurrentSelectedTimeFilter(): String {
        return when (selectedTimeFilterLiveData.value) {
            RECENT_GAME -> TimeFilter.PAST_DAY.value
            TODAY_GAME -> TimeFilter.TODAY.value
            FUTURE_GAME -> TimeFilter.UPCOMING_DAY.value
            else -> TimeFilter.TODAY.value
        }
    }

    fun setSelectedTimeFilter(filter: String?) {
        if (filter.isNullOrEmpty()) return
        selectedTimeFilterLiveData.value = filter
    }

    enum class TimeFilter(val value: String) {
        PAST_DAY("recent"),
        TODAY("today"),
        UPCOMING_DAY("future")
    }

    enum class GameCategory(val value: String) {
        ALL("all"),
        LOCAL("local"),
        ONLINE("online"),
        WELFARE("welfare"),
        GJONLINE("gjonline"),
    }

    companion object {
        const val RECENT_GAME = "近期"
        const val TODAY_GAME = "今天"
        const val FUTURE_GAME = "预约"
    }

    class Factory(private val mApplication: Application) :
        ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return GameServerTestV2ViewModel(mApplication) as T
        }
    }
}