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.box.HomeBoxListEntity
import com.zxhl.main.page.contract.HomeContract

class HomePresenter : HomeContract.Presenter {
    private val mView: HomeContract.View


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

    private var page: Int = 1
    private val size: Int = 10
    override fun getHomeBoxList(category: String?, ifRefresh: Boolean) {
        if (ifRefresh) {
            clearPage()
        }
        ApiClient.homeApi.getHomelist(page, size, category ?: "")
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<HomeBoxListEntity>>() {
                override fun onSuccess(result: List<HomeBoxListEntity>?) {
                    if ((result?.size ?: 0) < size) {
                        mView.setLoadMore(false)
                    } else {
                        mView.setLoadMore(true)
                        page++
                    }
                    if (result != null) {
                        mView.setHomeData(ifRefresh, result)
                    } else {
                        mView.setHomeData(false, null)
                    }
                }

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

                }
            })

    }


    override fun clearPage() {
        page = 1
    }



}