package com.gh.gamecenter.collection

import android.annotation.SuppressLint
import android.app.Application
import com.gh.gamecenter.core.AppExecutor
import com.gh.common.history.HistoryDatabase
import com.gh.common.history.HistoryHelper
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.common.baselist.LoadType
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.qa.entity.AnswerEntity
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.ResponseBody

class AnswerViewModel(application: Application) : ListViewModel<AnswerEntity, AnswerEntity>(application) {

    private var mApi = RetrofitManager.getInstance().api

    var type: String = AnswerFragment.COLLECTION

    override fun provideDataObservable(page: Int): Observable<MutableList<AnswerEntity>>? {
        return null
    }

    override fun provideDataSingle(page: Int): Single<List<AnswerEntity>> {
        return if (type == AnswerFragment.COLLECTION) {
            Single.fromObservable(mApi.getCollectionAnswer(UserManager.getInstance().userId, page))
        } else {
            if (page > 5) {
                Single.create { it.onSuccess(arrayListOf()) }
            } else {
                HistoryDatabase.instance.answerDao().getAnswersWithOffset(20, (page - 1) * 20)
            }
        }
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource<List<AnswerEntity>>(mListLiveData) { mResultLiveData.postValue(it) }
    }

    @SuppressLint("CheckResult")
    fun postCollectionAnswerRead(answerId: String) {
        mApi
            .postCollectionAnswerRead(UserManager.getInstance().userId, answerId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<ResponseBody>() {
                override fun onSuccess(data: ResponseBody) {
                }
            })
    }

    fun removeHistory(answerEntity: AnswerEntity) {
        mResultLiveData.value?.let {
            HistoryHelper.deleteAnswerEntity(answerEntity.id!!)
            it.remove(answerEntity)

            if (it.size == 0) {
                AppExecutor.uiExecutor.executeWithDelay(Runnable { load(LoadType.REFRESH) }, 100)
            } else {
                mResultLiveData.postValue(it)
            }
        }
    }

    fun removeHistories(ids: List<String>) {
        mResultLiveData.value?.let { datas ->
            ids.forEach { id ->
                HistoryHelper.deleteAnswerEntity(id)
                val entity = datas.find { it.id == id }
                datas.remove(entity)
            }

            if (datas.size == 0) {
                AppExecutor.uiExecutor.executeWithDelay(Runnable { load(LoadType.REFRESH) }, 100)
            } else {
                mResultLiveData.postValue(datas)
            }
        }
    }
}