package com.zxhl.main.page.presenter

import android.util.Log
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.FollowEntity
import com.zxhl.main.page.contract.FansContract

class FansPresenter:FansContract.Presenter {
    private val mView: FansContract.View


    constructor(mView: FansContract.View) {
        this.mView = mView
    }
    private var page = 1
    private val size = 20
    private var isMore = false
    override fun getFansList(ifRefresh: Boolean) {
        if (ifRefresh) {
            page = 1
            isMore = false
        } else if (isMore) {
            return
        }
        isMore = true
        ApiClient.homeApi.getFanList(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?) {

                }
            })
    }

    override fun followUser(uid: String?) {
        ApiClient.homeApi.followUser(uid).compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    mView.followSuc()
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    Log.e("MXL", "ERROR" + code + errorMsg)
                }
            })
    }
}