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.NoticeEntity
import com.zxhl.cms.net.model.box.ShopOrderEntity
import com.zxhl.main.page.contract.BoxOrderContract

class BoxOrderPresenter : BoxOrderContract.Presenter {
    private val mView: BoxOrderContract.View


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

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

                }

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

    }

    override fun getNotice() {
        ApiClient.homeApi.getNotice()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<NoticeEntity>() {
                override fun onSuccess(result: NoticeEntity?) {

                    mView.setNotice(result)

                }

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

                }
            })
    }


}