package com.zxbw.modulemain.box.presenter


import android.util.Log
import com.zxbw.modulemain.box.contract.WareHouseContract
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.AppInEntity
import com.zxhl.cms.net.model.box.DepotEntity
import com.zxhl.cms.net.model.box.NoticeEntity

class WareHousePresenter : WareHouseContract.Presenter {
    private val mView: WareHouseContract.View


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

    private var page: Int = 1
    private val size: Int = 10
    override fun getDepot(status: Int?, type: Int?, ifRefresh: Boolean) {
        if (ifRefresh) {
            clearPage()
        }
        ApiClient.homeApi.getDepot(page, size, status ?: 0, type ?: 0)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<DepotEntity>>() {
                override fun onSuccess(result: List<DepotEntity>?) {
                    if ((result?.size ?: 0) < size) {
                        mView.setLoadMore(false)
                    } else {
                        mView.setLoadMore(true)
                        page++
                    }
                    if (result != null) {
                        mView.setWareHouseData(ifRefresh, result)
                    } else {
                        mView.setWareHouseData(false, null)
                    }
                }

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

    }


    override fun clearPage() {
        page = 1
    }

    override fun recoverGoods(depotIds: String, recover_type: String) {
        ApiClient.homeApi.recoverGoods(depotIds, recover_type)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    mView.recoverSuccess()
                }

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

    }

    override fun getAppInfo() {
        ApiClient.homeApi.getAppInfo()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<AppInEntity>() {
                override fun onSuccess(result: AppInEntity?) {
                    if (result != null) {
                        mView.setAppInfo(result)
                    }
                }

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

                }
            })
    }

    override fun getNotice() {
        ApiClient.homeApi.getNotice()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<NoticeEntity>() {
                override fun onSuccess(result: NoticeEntity?) {

                    mView.setNotice(result)

                }

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

                }
            })
    }
}