package com.gh.gamecenter.fragment

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.gh.gamecenter.BuildConfig
import com.gh.gamecenter.R
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.common.utils.singleToMain
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.entity.HomeDataEntity
import com.gh.gamecenter.entity.SubjectRecommendEntity
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp

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

    var defaultTabPosition = 1

    var forumTabPairs: ArrayList<Pair<Int, String>> = arrayListOf()

    val tabs = MutableLiveData<ArrayList<SubjectRecommendEntity>>()
    val homeDataLiveData = MutableLiveData<HomeDataEntity>()
    val error = MutableLiveData<Exception>()

    var appBarOffset = 0

    init {
        getHomeContentUnion()
    }

    @SuppressLint("CheckResult")
    fun getHomeContentUnion(isRefresh: Boolean = false) {
        RetrofitManager.getInstance().api
            .getHomeContentUnion(BuildConfig.VERSION_NAME, HaloApp.getInstance().channel)
            .compose(singleToMain())
            .subscribe(object : BiResponse<HomeDataEntity>() {
                override fun onSuccess(data: HomeDataEntity) {
                    val homeTab = data.homeTab
                    if (homeTab.isNotEmpty()) {
                        forumTabPairs.clear()
                        for ((index, tab) in homeTab.withIndex()) {
                            if (tab.type == "top_game_comment") {
                                tab.primaryColor = R.color.amway_primary_color.toColor(getApplication())
                                tab.useLightStyle = true
                                tab.currentSelectColor = tab.primaryColor
                            }

                            if (tab.type == "home") {
                                tab.useLightStyle = true
                                if (data.homeSlide.isEmpty()) {
                                    tab.isSlideEmpty = true
                                    tab.isTopViewShow = false
                                }
                            }

                            if (tab.type == "bbs") {
                                forumTabPairs.add(Pair(index, tab.link ?: ""))
                            }

                            if (tab.default) defaultTabPosition = index
                        }
                        if (homeTab.size == 0) {
                            homeTab.add(SubjectRecommendEntity(type = "home"))
                        }
                        if (!isRefresh) {
                            tabs.postValue(homeTab)
                        }
                    }
                    homeDataLiveData.postValue(data)
                }

                override fun onFailure(exception: Exception) {
                    super.onFailure(exception)
                    if (!isRefresh) {
                        error.postValue(exception)
                    }
                }
            })
    }

}