package com.wd.workoffice.presenter;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.bean.DepBean;
import com.wd.workoffice.bean.ProcessBean;
import com.wd.workoffice.bean.StockBean;
import com.wd.workoffice.contract.BatSaleContract;
import com.wd.workoffice.model.BatSaleModel;
import com.wd.workoffice.retrofit.WorkObserver;
import java.util.Map;
import flexible.xd.android_base.mvpBase.BasePresenter;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;

/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class BatSalePresenter extends BasePresenter<BatSaleContract.View, BatSaleContract.Model> implements BatSaleContract.Presenter {

    @Override
    public BatSaleContract.Model bindModel() {
        return BatSaleModel.newInstance();
    }


    @Override
    public void getProcess(String processCode, Integer transitPartId) {
        mView.showLoading();
        mModel.getProcess(processCode, transitPartId).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
                mDisposable.add(d);
            }

            @Override
            public void onFail(String errorMsg) {
                mView.hideLoading();
                mView.onError(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                mView.hideLoading();
                if (data.getCode() != 0) {
                    mView.onError(data.getMessage());
                    return;
                }
                mView.processList(JSON.parseArray(data.getData().toString(), ProcessBean.class));
            }
        });
    }


    @Override
    public void getDep(String permission) {
        mView.showLoading();
        mModel.getDep(permission).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
                mDisposable.add(d);
            }

            @Override
            public void onFail(String errorMsg) {
                mView.hideLoading();
                mView.onError(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                mView.hideLoading();
                if (data.getCode() != 0) {
                    mView.onError(data.getMessage());
                    return;
                }
                mView.depList(JSON.parseArray(data.getData().toString(), DepBean.class));
            }
        });
    }

    @Override
    public void getStock(Map<String, Object> param) {
        mView.showLoading();
        mModel.getStock(param).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
                mDisposable.add(d);
            }

            @Override
            public void onFail(String errorMsg) {
                mView.hideLoading();
                mView.onError(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                mView.hideLoading();
                if (data.getCode() != 0) {
                    mView.onError(data.getMessage());
                    return;
                }
                mView.getStock(JSON.parseArray(data.getData().toString(), StockBean.class));
            }
        });
    }
}
