package com.wd.workoffice.presenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.bean.ProcessBean;
import com.wd.workoffice.bean.ProductBean;
import com.wd.workoffice.bean.StockBean;
import com.wd.workoffice.bean.TradingBean;
import com.wd.workoffice.contract.BatChooseInfoContract;
import com.wd.workoffice.model.BatChooseInfoModel;
import com.wd.workoffice.retrofit.WorkObserver;

import java.util.ArrayList;
import java.util.List;
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 BatChooseInfoPresenter extends BasePresenter<BatChooseInfoContract.View, BatChooseInfoContract.Model>
        implements BatChooseInfoContract.Presenter {
    @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 getPro(Map<String, Object> param) {
        mView.showLoading();
        mModel.getPro(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;
                }
                List<ProductBean.RecordsBean> dataList;
                ProductBean jsonData = JSON.parseObject(data.getData().toString(), ProductBean.class);
                if (jsonData != null) {
                    dataList = jsonData.getRecords();
                } else {
                    dataList = new ArrayList<>();
                }
                mView.getPro(dataList);
            }
        });
    }

    @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));
            }
        });
    }

    @Override
    public void getTradingPro(Map<String, Object> param) {
        mView.showLoading();
        mModel.getTradingPro(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;
                }
                ProductBean jsonData = JSON.parseObject(data.getData().toString(), ProductBean.class);
                mView.getTradingPro(jsonData.getRecords());
            }
        });
    }

    @Override
    public void getTradingProList(Map<String, Object> param) {
        mView.showLoading();
        mModel.getTradingProList(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;
                }
                List<TradingBean.RecordsBean> dataList;
                TradingBean jsonData = JSON.parseObject(data.getData().toString(), TradingBean.class);
                if (jsonData != null) {
                    dataList = jsonData.getRecords();
                } else {
                    dataList = new ArrayList<>();
                }
                mView.getTradingProList(dataList);
            }
        });
    }

    @Override
    public BatChooseInfoContract.Model bindModel() {
        return BatChooseInfoModel.newInstance();
    }
}
