package com.zxhl.main.page.presenter

import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.chat.ChatRoomEntity
import com.zxhl.cms.net.model.chat.RoomStatusEntity
import com.zxhl.main.page.contract.RoomContract

class RoomPresenter : RoomContract.Presenter {
    private val mView: RoomContract.View


    constructor(mView: RoomContract.View) {
        this.mView = mView
    }

    private var page = 1
    private val size = 20


    override fun getRoomList(ifRefresh: Boolean, category: String) {
        if (ifRefresh) {
            clearPage()
        }
        ApiClient.chatApi.getRoomList(page, size, category)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<ChatRoomEntity>>() {
                override fun onSuccess(result: List<ChatRoomEntity>?) {
                    if (result != null) {
                        if ((result.size) < size) {
                            mView.setLoadMore(false)
                        } else {
                            mView.setLoadMore(true)
                            page++
                        }
                        mView.setRoomList(ifRefresh, result)
                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    mView.getFail()
                }
            })
    }

    override fun inRoom(roomID: String?) {
        ApiClient.chatApi.inRoom(roomID)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<RoomStatusEntity>() {
                override fun onSuccess(result: RoomStatusEntity?) {

                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {

                }
            })
    }

    override fun clearPage() {
        page = 1
    }
}