package com.ym.chat.presenter

import android.util.Log
import com.ym.chat.contract.VisitorContract
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.VisitorEntity

class VisitorPresenter : VisitorContract.Presenter {
    private val mView: VisitorContract.View


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

    private var page = 1
    private val size = 20
    private var isMore = false
    override fun getVisitorList(ifRefresh: Boolean,free:Int) {
        if (ifRefresh) {
            page = 1
            isMore = false
        } else if (isMore) {
            return
        }
      isMore = true
        ApiClient.homeApi.getVisitor(page, size,free)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<VisitorEntity>>() {
                override fun onSuccess(result: List<VisitorEntity>?) {
                    if (result != null) {
                        if (result.isNullOrEmpty()) {
                            if(page==1){
                                mView.setEmptyView()
                            }
                        } else {
                            if (result.size <= size) {
                                page++
                                isMore = false
                            } else {
                                isMore = true
                            }
                            mView.setData(ifRefresh, result)
                        }
                    } else {

                            mView.setEmptyView()

                    }
                }

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

                }
            })
    }
}