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

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.gh.gamecenter.entity.FollowUserEntity
import com.gh.gamecenter.forum.home.follow.model.FollowDynamicRepository
import com.gh.gamecenter.livedata.Event

class FollowDynamicViewModel : ViewModel() {

    private var userPageNo = 1

    private val repository = FollowDynamicRepository.newInstance()

    private val _followedUsers = MutableLiveData<Pair<Boolean, List<FollowUserEntity>>>()
    val followedUsers: LiveData<Pair<Boolean, List<FollowUserEntity>>> = _followedUsers

    private val _isStartLoadAction = MutableLiveData<Boolean>()
    val isStartLoadAction: LiveData<Boolean> = _isStartLoadAction
    fun startLoad() {
        _isStartLoadAction.value = true
    }

    fun addFirstPageUsers(users: List<FollowUserEntity>) {
        userPageNo = 1
        addData(users, true)
    }

    private fun addData(newData: List<FollowUserEntity>, isFirstPage: Boolean) {
        val oldData = followedUsers.value?.second
        _followedUsers.value = isFirstPage to if (oldData == null) {
            newData
        } else {
            oldData + newData
        }
        userPageNo++
    }

    private val _finishAction = MutableLiveData<Event<Unit>>()
    val finishAction: LiveData<Event<Unit>> = _finishAction

    fun finish() {
        _finishAction.value = Event(Unit)
    }

    override fun onCleared() {

    }
}