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.BoxInfoEntity
import com.zxhl.cms.net.model.box.HomeBoxListEntity
import com.zxhl.cms.net.model.box.MarqueeEntity
import com.zxhl.cms.net.model.box.PropsCardList
import com.zxhl.main.page.contract.GoodsDetailContract


class GoodsDetailPresenter : GoodsDetailContract.Presenter {

    private val mView: GoodsDetailContract.View


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

    override fun getBoxInfo(boxId: String?) {
        ApiClient.homeApi.getBoxInfo(boxId ?: "")
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<BoxInfoEntity>() {
                override fun onSuccess(result: BoxInfoEntity?) {
                    if (result != null) {
                        mView.setBoxInfoData(result)
                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    Log.e("MXL","ERROR"+code+errorMsg+e)
                }
            })
    }

    override fun getBoxMarquee(boxId: String?) {
        ApiClient.homeApi.getBoxMarquee(boxId ?: "",6)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<MarqueeEntity>>() {
                override fun onSuccess(result: List<MarqueeEntity>?) {
                    if (result != null) {
                        mView.setMarqueeData(result)
                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    Log.e("MXL","error"+errorMsg+"code"+code+e)
                }
            })
    }

    override fun getPropsCardList(boxId: String?) {
        ApiClient.homeApi.getPropsCardList(boxId ?: "")
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<PropsCardList?>() {
                override fun onSuccess(result: PropsCardList?) {
                    mView.setPropsCardList(result)
                }

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