package com.zxbw.modulemain.presenter

import com.zxbw.modulemain.contract.FlashSaleContract
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.SkillEntity
import com.zxhl.cms.net.model.qy.SkillGoodsEntity
import com.zxhl.cms.net.model.qy.ToSkillInfoEntity

class FlashSalePresenter : FlashSaleContract.Presenter {
    private val mView: FlashSaleContract.View

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

    override fun getFlashSaleData() {
        ApiClient.homeApi.getskill().compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<SkillEntity>>() {
                override fun onSuccess(result: List<SkillEntity>?) {
                    mView.setData(result)
                }

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

                }
            })
    }

    override fun getSkillGoodsList(string: String?) {
        ApiClient.homeApi.getskillGoodsList(string).compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<SkillGoodsEntity>>() {
                override fun onSuccess(result: List<SkillGoodsEntity>?) {
                    mView.setSkillListResult(result)
                }

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

                }
            })
    }

    //开始秒杀
    override fun beginSkill(durationId: String?, id: String?) {
        ApiClient.homeApi.beginskill(durationId, id).compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<ToSkillInfoEntity>() {
                override fun onSuccess(result: ToSkillInfoEntity?) {
                    mView?.toskill(true, result)
                }

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

    override fun getMoreList(durationId: String?) {
        ApiClient.homeApi.getskillGoodsList(durationId).compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<SkillGoodsEntity>>() {
                override fun onSuccess(result: List<SkillGoodsEntity>?) {
                    mView.setMoreResult(result)
                }

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

                }
            })
    }
}