package com.gh.gamecenter.forum.detail

import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.core.utils.PageSwitchDataHelper
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.core.utils.UrlFilterUtils
import com.gh.gamecenter.entity.ForumDetailEntity
import com.gh.gamecenter.feature.entity.ForumVideoEntity
import com.gh.gamecenter.feature.entity.AnswerEntity
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.Observable

class ForumArticleAskListViewModel(application: Application, val bbsId: String = "", val mPath: String = "") :
    ListViewModel<AnswerEntity, AnswerEntity>(application) {

    var sort: String = "time.reply"
    var filter: String = "section_id"
    var selectedSection = ForumDetailEntity.Section("", "全部")
    var videoSort: String = "time.upload"
    var videoList = arrayListOf<ForumVideoEntity>()

    override fun provideDataObservable(page: Int): Observable<MutableList<AnswerEntity>> {
        return when (mPath) {
            "全部" -> {
                val data = PageSwitchDataHelper.popLastPageData()
                val map = hashMapOf<String, Any?>()

                if (selectedSection.id.isNotEmpty()) {
                    map["filter"] = UrlFilterUtils.getFilterQuery(filter, selectedSection.id)
                }

                if (data != null && data.containsKey(EntranceConsts.KEY_TOP_ID)) {
                    map["top_id"] = data[EntranceConsts.KEY_TOP_ID]
                }

                if (bbsId == TEMP_FIXED_TOP_BBS && SPUtils.getBoolean("$FIXED_TOP_KEY$bbsId", true)) {
                    val fixedTopId = TEMP_FIXED_TOP_POST
                    SPUtils.setBoolean("$FIXED_TOP_KEY$bbsId", false)
                    RetrofitManager.getInstance().api.getAllForumList(
                        bbsId,
                        fixedTopId,
                        UrlFilterUtils.getFilterQuery(sort, "-1"),
                        page,
                        map
                    )
                } else {
                    RetrofitManager.getInstance().api.getAllForumList(
                        bbsId,
                        UrlFilterUtils.getFilterQuery(sort, "-1"),
                        page,
                        map
                    )
                }

            }
            "精华" -> {
                RetrofitManager.getInstance().api.getEssenceForumList(bbsId, page)
            }
            "问答" -> {
                RetrofitManager.getInstance().api.getAskForumList(
                    bbsId,
                    UrlFilterUtils.getFilterQuery(sort, "-1"),
                    page
                )
            }
            else -> {
                RetrofitManager.getInstance().api.getVideoForumList(
                    bbsId,
                    UrlFilterUtils.getFilterQuery(videoSort, "-1"),
                    page
                )
            }
        }
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) { list ->
            val modifiedList = ArrayList(list)

            // 添加顶部 "全部列表" placeholder
            modifiedList.add(0, AnswerEntity().apply { id = "" })

            videoList = ArrayList(modifiedList.map {
                if (mPath == "视频") it.type = "video"
                it.transformForumVideoEntity()
            })

            mResultLiveData.postValue(modifiedList)
        }
    }

    class Factory(private val bbsId: String, val path: String) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return ForumArticleAskListViewModel(HaloApp.getInstance().application, bbsId, path) as T
        }
    }

    companion object {

        const val FIXED_TOP_KEY = "one_time_bbs_fixed_top_"

        const val TEMP_FIXED_TOP_BBS = "63201e1a7e4c92e5420f30ec"
        const val TEMP_FIXED_TOP_POST = "6322f759047515fa1e09fe16"

    }
}