package com.gh.gamecenter.message

import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.entity.MessageEntity
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import com.lightgame.utils.Utils
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.MediaType
import okhttp3.RequestBody
import okhttp3.ResponseBody
import org.json.JSONObject

class MessageNormalViewModel(
    application: Application,
    val messageType: String
) : ListViewModel<MessageEntity, MessageEntity>(application) {

    override fun provideDataObservable(page: Int): Observable<MutableList<MessageEntity>>? {
        return RetrofitManager.getInstance().api.getMessage(
            UserManager.getInstance().userId,
            messageType,
            Utils.getTime(getApplication()),
            page
        )
    }

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


    fun deleteMessage(messageId: String) {
        RetrofitManager.getInstance().api
            .deleteMessage(UserManager.getInstance().userId, messageId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    val listData = mListLiveData.value

                    listData?.let {
                        val iterator = it.iterator()
                        while (iterator.hasNext()) {
                            val data = iterator.next()
                            if (data.id == messageId) {
                                it.remove(data)
                                mListLiveData.postValue(listData)
                                break
                            }
                        }
                    }
                }
            })
    }

    fun postMessageRead(messageId: String, type: String) {

        // 更新本地数据以及页面
        val listData = mListLiveData.value
        if (listData != null) {
            for (entity in listData) {
                if (messageId == entity.id) {
                    entity.read = true
                    mListLiveData.postValue(listData)
                    break
                }
            }
        }

        // 后端同步
        val jsonObject = JSONObject()
        jsonObject.put("type", type)
        val body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString())
        RetrofitManager.getInstance().api.postMessageRead(UserManager.getInstance().userId, messageId, body)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
            })
    }

    class Factory(
        private val mApplication: Application,
        private val mMessageType: String
    ) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return MessageNormalViewModel(mApplication, mMessageType) as T
        }
    }
}