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

import android.view.ViewGroup
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.gh.common.util.ErrorHelper
import com.gh.gamecenter.common.baselist.LoadStatus
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.common.utils.observableToMain
import com.gh.gamecenter.entity.PersonalHistoryEntity
import com.gh.gamecenter.feature.entity.AnswerEntity
import com.gh.gamecenter.forum.home.follow.FollowDynamicItem
import com.gh.gamecenter.forum.home.follow.model.FollowDynamicRepository
import com.gh.gamecenter.livedata.Event
import com.halo.assistant.HaloApp
import io.reactivex.disposables.CompositeDisposable
import retrofit2.HttpException

class FollowDynamicListViewModel : ViewModel() {

    private val compositeDisposable = CompositeDisposable()

    private val repository = FollowDynamicRepository.newInstance()

    private var pageNo = 1

    private var userId: String = ""
    var bbsId: String = ""

    private val _loadStatus = MutableLiveData<Pair<LoadStatus, Boolean>>()
    val loadStatus: LiveData<Pair<LoadStatus, Boolean>> = _loadStatus

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

    fun loadFirst(userId: String, bbsId: String, isPullRefresh: Boolean) {
        pageNo = 1
        this.userId = userId
        this.bbsId = bbsId
        loadData(userId, bbsId, isPullRefresh)
    }

    fun loadMore() {
        loadData(userId, bbsId, false)
    }

    private fun loadData(userId: String, bbsId: String, isPullRefresh: Boolean) {
        if (pageNo == 1) {
            _loadStatus.value = LoadStatus.INIT_LOADING to isPullRefresh
        } else {
            _loadStatus.value = LoadStatus.LIST_LOADING to isPullRefresh
        }
        repository.loadData(userId, bbsId, pageNo)
            .compose(observableToMain())
            .subscribe(object : Response<List<FollowDynamicItem>>() {

                override fun onResponse(data: List<FollowDynamicItem>?) {
                    if (data.isNullOrEmpty()) {
                        if (pageNo == 1) {
                            _loadStatus.value = LoadStatus.INIT_EMPTY to isPullRefresh
                        } else {
                            _loadStatus.value = LoadStatus.LIST_OVER to isPullRefresh
                        }
                    } else {
                        if (pageNo == 1) {
                            _loadStatus.value = LoadStatus.INIT_LOADED to isPullRefresh
                        } else {
                            _loadStatus.value = LoadStatus.LIST_LOADED to isPullRefresh
                        }
                    }
                    if (pageNo == 1) {
                        _dataList.value = data ?: listOf()
                        pageNo++
                    } else {
                        addData(data ?: listOf())
                    }


                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    if (pageNo == 1) {
                        _loadStatus.value = LoadStatus.INIT_FAILED to isPullRefresh
                    } else {
                        _loadStatus.value = LoadStatus.LIST_FAILED to isPullRefresh
                    }
                    ErrorHelper.handleError(HaloApp.getInstance().application, e?.response()?.errorBody()?.string())
                }
            })
    }

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


    private val _personDetailDestination = MutableLiveData<Event<Pair<PersonalHistoryEntity, Int>>>()
    val personDetailDestination: LiveData<Event<Pair<PersonalHistoryEntity, Int>>> = _personDetailDestination
    fun navigateToPersonDetailPage(history: PersonalHistoryEntity, position: Int) {
        _personDetailDestination.value = Event(history to position)
    }

    private val _bbsDetailDestination = MutableLiveData<Event<Pair<Int, AnswerEntity>>>()
    val bbsDetailDestination: LiveData<Event<Pair<Int, AnswerEntity>>> = _bbsDetailDestination
    fun navigateToBbsDetailPage(position: Int, answer: AnswerEntity) {
        _bbsDetailDestination.value = Event(position to answer)
    }

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


}