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.box.HomeBoxListEntity
import com.zxhl.cms.net.model.shop.ShopEntity
import com.zxhl.cms.net.model.shop.ShopListEntity
import com.zxhl.main.page.contract.HomeContract
import com.zxhl.main.page.contract.ShopListContract

class ShopListPresenter : ShopListContract.Presenter {
    private val mView: ShopListContract.View


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

    private var page: Int = 1
    private val size: Int = 10

    override fun getShopList(category: String?, ifRefresh: Boolean) {
        if (ifRefresh) {
            clearPage()
        }
        ApiClient.shopApi.getGoodsList(page, size, category ?: "")
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<ShopEntity>() {
                override fun onSuccess(result: ShopEntity?) {
                    if ((result?.list?.size ?: 0) < size) {
                        mView.setLoadMore(false)
                    } else {
                        mView.setLoadMore(true)
                        page++
                    }
                    if (result?.list != null) {
                        mView.setShopListData(ifRefresh, result.list)
                    } else {
                        mView.setShopListData(false, null)
                    }
                }

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

    }


    override fun clearPage() {
        page = 1
    }


}