package com.zxbw.modulemain.presenter

import com.zxbw.modulemain.contract.ShopRecommendContract
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.TabEntity
import com.zxhl.cms.net.model.qy.JgqQyEntity
import com.zxhl.cms.net.model.qy.TbGoodsEntity

/**
 * 商城分类
 */
class ShopRecommendPresenter : ShopRecommendContract.Presenter {

    private val mView: ShopRecommendContract.View

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

    override fun getCategoryList(tabName: String) {
        ApiClient.homeApi.getQyJgqCardList().compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<JgqQyEntity>>() {
                override fun onSuccess(result: List<JgqQyEntity>?) {
                    mView.setCategoryList(result)
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    mView.setCategoryList(null)
                }
            })
    }
    private var searchPage = 1;
    private var pageSize = 20;
    override fun getShopGoodsList(isRefresh: Boolean, tabEntity: TabEntity) {
        if (isRefresh && tabEntity.name == "推荐") {
            searchPage = 1;
        }
        var cate = ""
        var keyword = ""
        if (tabEntity.use == "cate") {
            cate = tabEntity.value
        } else {
            keyword = tabEntity.name
        }
        ApiClient.homeApi.getGoodsListSearch(cate, keyword, searchPage, pageSize)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<TbGoodsEntity>() {
                override fun onSuccess(result: TbGoodsEntity?) {
                    if (result != null) {
                        searchPage++;
                        mView.setShopGoodsList(isRefresh, result.result_list)
                    } else {
                        mView.setShopGoodsList(isRefresh, null)
                    }
                }

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

}