package com.wd.workoffice.presenter;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.bean.ProductBean;
import com.wd.workoffice.contract.WorkProContract;
import com.wd.workoffice.model.WorkProModel;
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 WorkProPresenter extends BasePresenter<WorkProContract.View, WorkProContract.Model> implements WorkProContract.Presenter {
    @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;
                }
                mView.getPro(JSON.parseArray(JSON.parseObject(data.getData().toString()).getString("records"), ProductBean.RecordsBean.class));
            }
        });
    }

    @Override
    public void modify(Integer id, String internalPrice, String remark) {
        mView.showLoading();
        mModel.modify(id, internalPrice, remark).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.modify(data.getMessage());
            }
        });
    }

    @Override
    public void onSale(Integer id, String shelfAction) {
        mView.showLoading();
        mModel.onSale(id, shelfAction).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.onSale(data.getMessage());
            }
        });
    }

    @Override
    public WorkProContract.Model bindModel() {
        return WorkProModel.newInstance();
    }
}
