package com.gh.gamecenter.personalhome

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.entity.FollowersOrFansEntity
import com.gh.gamecenter.eventbus.EBUserFollow
import com.gh.gamecenter.personalhome.fans.FansActivity
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import com.lightgame.utils.Utils
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.ResponseBody
import org.greenrobot.eventbus.EventBus
import retrofit2.HttpException

class FollowersOrFansViewModel(
    application: Application,
    var mPageSource: String,
    val userId: String
) : ListViewModel<FollowersOrFansEntity, FollowersOrFansEntity>(application) {

    val followingLiveData: MutableLiveData<Int> = MutableLiveData()

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) { mResultLiveData.postValue(it) }
    }

    override fun provideDataObservable(page: Int): Observable<List<FollowersOrFansEntity>> {
        return if (mPageSource == FansActivity::class.java.name) {
            RetrofitManager.getInstance().api.getPersonalFans(userId, HaloApp.getInstance().channel, page)
        } else {
            RetrofitManager.getInstance().api.getPersonalFollowers(userId, HaloApp.getInstance().channel, page)
        }
    }

    fun followingCommand(isFollow: Boolean, targetUserId: String, position: Int) {
        val observable = if (isFollow) {
            RetrofitManager.getInstance().api.postFollowing(targetUserId)
        } else {
            RetrofitManager.getInstance().api.deleteFollowing(targetUserId)
        }
        observable
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    if (isFollow) {
                        // 关注成功
                        Utils.toast(getApplication(), com.gh.gamecenter.feature.R.string.concern_success)
                    }

                    val list = mResultLiveData.value
                    if (list != null && list.size > position) {
                        list[position].me.isFollower = isFollow
                    }

                    followingLiveData.postValue(position)
                    EventBus.getDefault().post(EBUserFollow(userId, isFollow))
                }

                override fun onFailure(e: HttpException?) {
                    Utils.toast(getApplication(), R.string.post_failure_hint)
                }
            })
    }

    class Factory(
        private val mApplication: Application,
        private val mPageSource: String,
        private val mUserId: String
    ) : ViewModelProvider.NewInstanceFactory() {

        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return FollowersOrFansViewModel(mApplication, mPageSource, mUserId) as T
        }
    }
}
