package com.gh.gamecenter.video.data

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.entity.VideoDataItem
import com.gh.gamecenter.entity.VideoDataOverViewEntity
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.common.mvvm.Resource
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import retrofit2.HttpException
import java.util.LinkedHashMap

class VideoDataViewModel(application: Application) : AndroidViewModel(application) {

    private val api = RetrofitManager.getInstance().api

    private var videoDataList = ArrayList<VideoDataItem>()

    var videoDataLiveData = MutableLiveData<Resource<ArrayList<VideoDataItem>>>()

    fun loadData() {
        getVideoOverView()
    }

    private fun getVideoOverView() {
        api.getVideoData(UserManager.getInstance().userId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<VideoDataOverViewEntity>() {
                override fun onResponse(response: VideoDataOverViewEntity?) {
                    super.onResponse(response)
                    if (response != null) {
                        videoDataList.clear()
                        if (response.date != TimeUtils.getToday()) {
                            response.yesterdayFan = 0
                            response.yesterdayPlay = 0
                            response.yesterdayVote = 0
                            response.yesterdayComment = 0
                            response.yesterdayShare = 0
                            response.yesterdayFavorite = 0
                        }
                        videoDataList.add(VideoDataItem(videoDataOverView = response))
                        videoDataLiveData.postValue(Resource.success(videoDataList))
                        getVideoFansData()
                    }
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    videoDataLiveData.postValue(Resource.error(e))
                }
            })
    }

    private fun getVideoFansData() {
        api.getVideoFansData(UserManager.getInstance().userId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<LinkedHashMap<String, Int>>() {
                override fun onResponse(response: LinkedHashMap<String, Int>?) {
                    super.onResponse(response)
                    if (!response.isNullOrEmpty()) {
                        videoDataList.add(VideoDataItem(videoFans = response))
                        videoDataLiveData.postValue(Resource.success(videoDataList))
                    }
                    getVideoPlayData()
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    videoDataLiveData.postValue(Resource.error(e))
                }
            })
    }

    private fun getVideoPlayData() {
        api.getVideoPlayData(UserManager.getInstance().userId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<LinkedHashMap<String, Int>>() {
                override fun onResponse(response: LinkedHashMap<String, Int>?) {
                    super.onResponse(response)
                    if (!response.isNullOrEmpty()) {
                        videoDataList.add(VideoDataItem(videoPlay = response))
                        videoDataLiveData.postValue(Resource.success(videoDataList))
                    }
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    videoDataLiveData.postValue(Resource.error(e))
                }
            })
    }
}