package com.wd.workoffice.presenter;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.bean.OrderBean;
import com.wd.workoffice.contract.SaleSaleContract;
import com.wd.workoffice.model.SaleSaleModel;
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 SaleSalePresenter extends BasePresenter<SaleSaleContract.View, SaleSaleContract.Model> implements SaleSaleContract.Presenter {
    @Override
    public void getOrder(Map<String, Object> param) {
        mView.showLoading();
        mModel.getOrder(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.saleList(JSON.parseArray(JSON.parseObject(data.getData().toString()).getString("records"), OrderBean.class));
            }
        });
    }

    @Override
    public void getOutOrder(Map<String, Object> param) {
        mView.showLoading();
        mModel.getOutOrder(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.outList(JSON.parseArray(JSON.parseObject(data.getData().toString()).getString("records"), OrderBean.class));
            }
        });
    }

    @Override
    public void getInsideOrder(Map<String, Object> param) {
        mView.showLoading();
        mModel.getInsideOrder(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.insideList(JSON.parseArray(JSON.parseObject(data.getData().toString()).getString("records"), OrderBean.class));
            }
        });
    }

    @Override
    public void getSupplierOrder(Map<String, Object> param) {
        mView.showLoading();
        mModel.getSupplierOrder(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.supplierList(JSON.parseArray(JSON.parseObject(data.getData().toString()).getString("records"), OrderBean.class));
            }
        });
    }

    @Override
    public SaleSaleContract.Model bindModel() {
        return SaleSaleModel.newInstance();
    }
}
