package com.gh.gamecenter.qa.dialog

import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.SearchActivity
import com.gh.gamecenter.SearchType
import com.gh.gamecenter.common.base.GlobalActivityManager
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.common.baselist.LoadType
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.entity.ForumEntity
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.retrofit.RetrofitManager
import com.gh.gamecenter.retrofit.service.ApiService
import com.halo.assistant.HaloApp
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import okhttp3.ResponseBody

class ChooseForumContainerViewModel(
    application: Application,
    val type: String,
    var searchKey: String,
    var searchType: String,
    var sourceEntrance: String  // 神策sourceEntrance 相当火山云 location
) :
    ListViewModel<ForumEntity, ForumEntity>(application) {

    private val mApi: ApiService = RetrofitManager.getInstance().api
    private val mCompositeDisposable = CompositeDisposable()

    private var mPage = 0

    init {
        if (type != ChooseForumContainerFragment.ChooseForumType.SEARCH.value) {
            load(LoadType.REFRESH)
        }
    }

    override fun provideDataObservable(page: Int): Observable<MutableList<ForumEntity>> {
        mPage = page
        if (mPage == 1 && type == ChooseForumContainerFragment.ChooseForumType.SEARCH.value) {
            NewFlatLogUtils.logSearchBbs(SearchType.fromString(searchType).toChinese(), searchKey, sourceEntrance)
        }
        return when (type) {
            ChooseForumContainerFragment.ChooseForumType.ATTENTION.value -> {
                mApi.getFollowsForum(UserManager.getInstance().userId)
            }

            ChooseForumContainerFragment.ChooseForumType.HOT.value -> {
                mApi.getHotForum(page)
            }

            else -> {
                mApi.searchBbs(searchKey, page)
            }
        }
    }

    fun setSearchKeyAndRefresh(searchKey: String, searchType: String) {
        this.searchKey = searchKey
        this.searchType = searchType
        load(LoadType.REFRESH)
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource<List<ForumEntity>>(mListLiveData) {
            mResultLiveData.postValue(it)
            if (mPage == 1) {
                SensorsBridge.trackForumSearchResultReturn(
                    GlobalActivityManager.getCurrentPageEntity().pageId,
                    GlobalActivityManager.getCurrentPageEntity().pageName,
                    sourceEntrance,
                    searchKey,
                    SearchActivity.toTrackSearchType(searchType),
                    it.isNotEmpty()
                )
            }
        }
    }

    fun followForum(bbsId: String, onSuccess: () -> Unit) {
        val disposable = mApi
            .followForum(bbsId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<ResponseBody>() {
                override fun onSuccess(data: ResponseBody) {
                    onSuccess.invoke()
                }
            })
        mCompositeDisposable.add(disposable)
    }

    fun unFollowForum(bbsId: String, onSuccess: () -> Unit) {
        val disposable = mApi.unFollowForum(bbsId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<ResponseBody>() {
                override fun onSuccess(data: ResponseBody) {
                    onSuccess.invoke()
                }
            })
        mCompositeDisposable.add(disposable)
    }

    override fun onCleared() {
        super.onCleared()
        mCompositeDisposable.clear()
    }

    class Factory(val type: String, val searchKey: String, val searchType: String, private val sourceEntrance: String) :
        ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return ChooseForumContainerViewModel(
                HaloApp.getInstance().application,
                type,
                searchKey,
                searchType,
                sourceEntrance
            ) as T
        }
    }
}