package com.rdiot.yx485.message

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.rdiot.yx485.base.BaseViewModel
import com.rdiot.yx485.bean.MessageListBean
import com.rdiot.yx485.bean.MessageNumBean
import com.rdiot.yx485.http.Url
import com.rdiot.yx485.http.Url.MESSAGEREAD
import com.rdiot.yx485.http.Url.MESSAGEREADALL
import com.rdiot.yx485.http.entity.BaseReplyBean
import com.rdiot.yx485.util.LocalCache
import com.zcolin.frame.http.ZHttp
import com.zcolin.frame.http.response.ZResponse
import okhttp3.Response

class MessageViewModel : BaseViewModel() {
    private val _messageCount = MutableLiveData<MessageNumBean.DataBean.ListBean?>()
    val messageCount: LiveData<MessageNumBean.DataBean.ListBean?> = _messageCount

    private val _messageList = MutableLiveData<List<MessageListBean.DataBean.MessageInfo>>()
    val messageList: LiveData<List<MessageListBean.DataBean.MessageInfo>> = _messageList

    private val _readResult = MutableLiveData<Boolean>()
    val readResult: LiveData<Boolean> = _readResult

    private val _readAllResult = MutableLiveData<Boolean>()
    val readAllResult: LiveData<Boolean> = _readAllResult

    /**
     * 获取消息列表
     */
    fun loadMessageList(type: String) {
        val map = HashMap<String, String>()
        map["q"] = "list"
        map["type"] = type
        map["home_id"] = LocalCache.getDefaultHomeId()

        ZHttp.getWithHeader<MessageListBean>(
            Url.MESSAGE,
            getHeaders(),
            map,
            object : ZResponse<MessageListBean>() {
                override fun onSuccess(response: Response?, resObj: MessageListBean?) {
                    resObj?.let {
                        if (it.isSuccess) {
                            _messageList.value = it.data.list
                            return
                        }
                        mErrorMessage.value = it.message
                    }
                }

                override fun onError(code: Int, error: String?) {
                    super.onError(code, error)
                    mErrorMessage.value = error
                }
            })
    }

    /**
     * 获取消息数量
     */
    fun loadMessageCount() {
        val map = HashMap<String, String>()
        map["q"] = "unread"
        map["home_id"] = LocalCache.getDefaultHomeId()

        ZHttp.getWithHeader<MessageNumBean>(
            Url.MESSAGE,
            getHeaders(),
            map,
            object : ZResponse<MessageNumBean>() {
                override fun onSuccess(response: Response?, resObj: MessageNumBean?) {
                    resObj?.let {
                        if (it.isSuccess) {
                            _messageCount.value = it.data.list
                            return
                        }
                        mErrorMessage.value = it.message
                    }
                }

                override fun onError(code: Int, error: String?) {
                    super.onError(code, error)
                    mErrorMessage.value = error
                }
            })
    }

    /**
     * 标记消息已读
     */
    fun markMessageAsRead(messageId: String) {
        val map = HashMap<String, String>()
        val url = MESSAGEREAD.replace("{id}", messageId)
        ZHttp.patchJsonWithHeader<BaseReplyBean>(
            url,
            getHeaders(),
            Gson().toJson(map),
            object : ZResponse<BaseReplyBean>() {
                override fun onSuccess(response: Response?, resObj: BaseReplyBean?) {
                    resObj?.let {
                        if (it.isSuccess) {
                            _readResult.value = true
                            return
                        }
                        mErrorMessage.value = it.message
                    }
                }

                override fun onError(code: Int, error: String?) {
                    super.onError(code, error)
                    mErrorMessage.value = error
                    _readResult.value = false
                }
            })
    }

    /**
     * 全部
     */
    fun markAllRead(type: String) {
        val map = HashMap<String, String>()
        map["type"] = type
        val url = MESSAGEREADALL.replace("{home_id}", LocalCache.getDefaultHomeId())

        ZHttp.patchJsonWithHeader<BaseReplyBean>(
            url,
            getHeaders(),
            Gson().toJson(map),
            object : ZResponse<BaseReplyBean>() {
                override fun onSuccess(response: Response?, resObj: BaseReplyBean?) {
                    resObj?.let {
                        if (it.isSuccess) {
                            _readAllResult.value = true
                            return
                        }
                        mErrorMessage.value = it.message
                    }
                }

                override fun onError(code: Int, error: String?) {
                    super.onError(code, error)
                    mErrorMessage.value = error
                }
            })
    }
} 