package com.zxbw.modulemain.presenter

import android.util.Log
import com.zxbw.modulemain.contract.HomeRecommendContract
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 HomeRecommendPresenter : HomeRecommendContract.Presenter {

    private val mView: HomeRecommendContract.View

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

    override fun getQyCardList(tabName: String) {
        if (tabName != "推荐") {
            mView.setQyCardList(null)
            return
        }
        ApiClient.homeApi.getQyJgqCardList().compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<JgqQyEntity>>() {
                override fun onSuccess(result: List<JgqQyEntity>?) {
                    mView.setQyCardList(result)
                }

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

    private var searchPage = 1;
    private var pageSize = 20;
    override fun getTbGoodsList(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.setTbGoodsList(isRefresh, result.result_list)
                    } else {
                        mView.setTbGoodsList(isRefresh, null)
                    }
                }

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

    private var guessLikePage = 1;
    override fun getGuessLikeGoodsList(isRefresh: Boolean) {
//        if (isRefresh) {
//            guessLikePage = 1;
//        }
        ApiClient.homeApi.getGoodsGuessLike(guessLikePage, pageSize)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<TbGoodsEntity>() {
                override fun onSuccess(result: TbGoodsEntity?) {
                    if (result != null) {
                        guessLikePage++;
                        mView.setTbGoodsList(isRefresh, result.data)
                    } else {
                        mView.setTbGoodsList(isRefresh, null)
                    }
                }

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