package com.zxbw.modulemain.presenter

import android.util.Log
import com.zxbw.modulemain.contract.SearchReslutContract
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.qy.TbGoodsEntity

class SearchResultPresenter : SearchReslutContract.Presenter {
    private val mView: SearchReslutContract.View

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

    private var page = 1
    private val size = 20
    private var isMore = false
    override fun getShopSearchList(isRefresh: Boolean, keyword: String?) {
        if (isRefresh) {
            page = 1
            isMore = false
        } else if (isMore) {
            return
        }
        isMore = true

        ApiClient.homeApi.getGoodsListSearch("", keyword.toString(), page, size)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<TbGoodsEntity>() {
                override fun onSuccess(result: TbGoodsEntity?) {
                    // mView?.setData(result)
                    if (result != null) {
                        if (result.result_list.isNullOrEmpty()) {
                            if (page == 1) {
                                mView.setEmptyView()
                            }
                        } else {
                            if (result.result_list!!.size <= size) {
                                page++
                                isMore = false
                            } else {
                                isMore = true
                            }
                            mView.setData(isRefresh, result)
                        }
                    } else {

                        mView.setEmptyView()

                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                  //  mView.setTbGoodsList(isRefresh, null)
                }
            })

    }


}