package com.gh.gamecenter.forum.home.follow.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.common.utils.singleToMain
import com.gh.gamecenter.entity.FollowOperateTopRequest
import com.gh.gamecenter.entity.FollowUserEntity
import com.gh.gamecenter.forum.home.follow.AllFollowedNormalItem
import com.gh.gamecenter.forum.home.follow.model.AllFollowRepository
import com.gh.gamecenter.livedata.Event
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.disposables.CompositeDisposable
import okhttp3.ResponseBody

class AllFollowedViewModel : ViewModel() {

    private val repository = AllFollowRepository(RetrofitManager.getInstance().newApi)

    private val compositeDisposable = CompositeDisposable()

    private var pageNo = 1

    private val _dataList = MutableLiveData<List<AllFollowedNormalItem>>()
    val dataList: LiveData<List<AllFollowedNormalItem>> = _dataList

    val hasReadSetIds = mutableSetOf<String>()

    fun loadFirst() {
        pageNo = 1
        loadData()
    }

    fun loadMore() {
        loadData()
    }

    private fun loadData() {
        repository.loadMyFollowedUser(pageNo)
            .compose(singleToMain())
            .subscribe(object : BiResponse<List<AllFollowedNormalItem>>() {
                override fun onSuccess(data: List<AllFollowedNormalItem>) {
                    addData(data)
                }

            }).let(compositeDisposable::add)
    }

    private val _operateTopSuccessfully = MutableLiveData<Event<Unit>>()
    val operateTopSuccessfully: LiveData<Event<Unit>> = _operateTopSuccessfully

    fun operateTop(topItem: List<FollowOperateTopRequest>) {
        repository.operateTop(topItem)
            .compose(singleToMain())
            .subscribe(object : BiResponse<ResponseBody>() {
                override fun onSuccess(data: ResponseBody) {
                    _operateTopSuccessfully.value = Event(Unit)
                }

            }).let(compositeDisposable::add)
    }

    private fun addData(newData: List<AllFollowedNormalItem>) {
        val oldData = dataList.value
        _dataList.value = if (oldData == null) {
            newData
        } else {
            oldData + newData
        }
        pageNo++
    }

    private val _detailDestination = MutableLiveData<Event<FollowUserEntity>>()
    val detailDestination: LiveData<Event<FollowUserEntity>> = _detailDestination
    fun navigateToDetailPage(item: AllFollowedNormalItem) {
        val entity = item.data
        _detailDestination.value = Event(entity)
        val id = if (entity.isUser) {
            entity.user.id
        } else {
            entity.bbs.id
        }
        if (entity.isShowTip) {
            repository.postRead(entity.type, id ?: "")
                .compose(singleToMain())
                .subscribe(object : BiResponse<ResponseBody>() {
                    override fun onSuccess(data: ResponseBody) {
                        hasReadSetIds.add(item.data.id)
                        updateReadStatus(item)
                    }
                }).let(compositeDisposable::add)
        }

    }

    private fun updateReadStatus(item: AllFollowedNormalItem) {
        val oldData = _dataList.value ?: return
        val position = oldData.indexOfFirst {
            item.data.id == it.data.id
        }
        val newData = oldData.toMutableList()
        val newItem = item.copy(data = item.data.copy(_isShowTip = 0))
        newData[position] = newItem
        _dataList.value = newData

    }

    override fun onCleared() {
        compositeDisposable.clear()
    }
}