package com.zxbw.modulemain.presenter

import com.zxbw.modulemain.contract.ShopOrderListContract
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.ShopOrderEntity


class ShopOrderListPresenter : ShopOrderListContract.Presenter {
    private val mView: ShopOrderListContract.View


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

    private var page: Int = 1
    private val size: Int = 10
    private var isMore = false
    override fun getShopOrderList(ifRefresh: Boolean) {
        if (ifRefresh) {
            page = 1
            isMore = false
        } else if (isMore) {
            return
        }
        isMore = true
        ApiClient.homeApi.getBoxOrderShop(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.setShopOrderListData(ifRefresh, result)
                        }else{
                            if(page==1){
                                mView.setEmptyView(true)
                            }
                        }
                    }else{
                        mView.setEmptyView(true)
                    }

                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
//                    Log.d("wxw", "goodsListError " + code + " errorMsg " + errorMsg)
                  //  mView.setEmptyView(true)
                }
            })

    }

    override fun clearPage() {
        page = 1
    }


}