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.ChatMessageChargeOrNotEntity
import com.zxhl.cms.net.model.chat.FollowEntity
import com.zxhl.main.page.contract.MyLikesContract

class MyLikesPresenter : MyLikesContract.Presenter {

    private val mView: MyLikesContract.View


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

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

                    } else {
                        mView.setEmptyView()
                    }
                }

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

                }
            })
    }
}