package com.zxbw.modulemain.box.presenter


import com.zxbw.modulemain.box.contract.ReceiveAddressContract
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.AddressEntity

class ReceiveAddressPresenter : ReceiveAddressContract.Presenter {
    private val mView: ReceiveAddressContract.View

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

    override fun getAddress(isDefault: String?) {
        ApiClient.homeApi.getAddress(isDefault ?: "")
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<AddressEntity>>() {
                override fun onSuccess(result: List<AddressEntity>?) {
                    if (result != null) {
                        mView.setAddressData(result)
                    }
                }

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

                }
            })
    }

    override fun delAddress(entity: AddressEntity?) {
        ApiClient.homeApi.delAddress(entity)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    if (result != null) {
                        mView.delSucess()
                    }
                }

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

                }
            })
    }
}