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.*
import com.zxhl.main.page.contract.LotteryContract

class LotteryPresenter : LotteryContract.Presenter {
    private val mView: LotteryContract.View

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

    override fun testLottery(boxid: String?) {
        ApiClient.homeApi.testLottery(boxid ?: "")
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<LotteryEntity>() {
                override fun onSuccess(result: LotteryEntity?) {
                    if (result != null) {
                        mView.setTestLotterResult(result)
                    }
                }

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

                }
            })
    }

    override fun oneLottery(boxid: String?) {
        ApiClient.homeApi.oneLottery(boxid ?: "")
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<OneLotteryEntity>() {
                override fun onSuccess(result: OneLotteryEntity?) {
                    if (result != null) {
                        mView.setOneLotterResult(result)
                        getPropsCardList(boxid, result.card)
                    }
                }

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

                }
            })
    }

    override fun fiveLottery(boxid: String?) {
        ApiClient.homeApi.fiveLottery(boxid ?: "")
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<FiveLotteryEntiy>() {
                override fun onSuccess(result: FiveLotteryEntiy?) {
                    if (result != null) {
                        mView.setFiveLotterResult(result)
                        getPropsCardList(boxid, result.card)
                    }
                }

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

                }
            })
    }

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

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

    override fun rePumpingOneLottery(boxid: String?, depotIds: String) {
        ApiClient.homeApi.rePumpingOneLottery(boxid ?: "", depotIds)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<OneLotteryEntity>() {
                override fun onSuccess(result: OneLotteryEntity?) {
                    if (result != null) {
                        mView.setOneLotterResult(result)
                        // getPropsCardList(boxid, null)
                    }
                }

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

                }
            })
    }

    override fun rePumpingFiveLottery(boxid: String?, depotIds: String) {
        ApiClient.homeApi.rePumpingFiveLottery(boxid ?: "", depotIds)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<FiveLotteryEntiy>() {
                override fun onSuccess(result: FiveLotteryEntiy?) {
                    if (result != null) {
                        mView.setFiveLotterResult(result)
                        // getPropsCardList(boxid, null)
                    }
                }

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

                }
            })
    }
}