package com.philisense.rdgo.viewModel;

import android.content.Context;

import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.bean.request.PileInfoRequestBean;
import com.philisense.rdgo.bean.request.PutInRequestBean;
import com.philisense.rdgo.bean.response.BaseBean;
import com.philisense.rdgo.bean.response.PileInfoBean;
import com.philisense.rdgo.bean.response.WarehouseBean;
import com.philisense.rdgo.db.DBFactory;
import com.philisense.rdgo.db.UserDb;
import com.philisense.rdgo.http.HttpClient;
import com.philisense.rdgo.http.RequestImpl;
import com.philisense.rdgo.http.cache.ACache;
import com.philisense.rdgo.util.Constant;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by shaop on 2018/1/5.
 */

public class LineInViewModel {
    private Context context;

    public LineInViewModel(Context context) {
        this.context = context;
    }

    public void getWarehouse(RequestImpl<List<WarehouseBean.ListBean>> listener) {
        ACache aCache = ACache.get(context);

        HttpClient.Builder.getBaseService()
                .getWarehouse(aCache.getAsString(Constant.ACCESSTOKEN))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WarehouseBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(WarehouseBean value) {
                        listener.loadSuccess(value.getList());
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.loadFailed(new OError(Constant.SystemErrorCode, e.getMessage()));
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 入库
     * @param listener 监听
     */
    public void lineInAction(String boxId, String warehouse, String warehouseCode, RequestImpl<String> listener) {


        UserDb userDb = DBFactory.getUserInfo(context);

        PutInRequestBean putInRequestBean= new PutInRequestBean();
        putInRequestBean.setAccount(userDb.getAccount() == null ? "": userDb.getAccount());
        putInRequestBean.setName(userDb.getName() == null ? "" : userDb.getName());
        putInRequestBean.setType(1); // 产线入库
        putInRequestBean.setWarehouse(warehouse);
        putInRequestBean.setWarehouseCode(warehouseCode);
        putInRequestBean.setBoxId(boxId);
        putInRequestBean.setId("");
        putInRequestBean.setAccessToken( ACache.get(context).getAsString(Constant.ACCESSTOKEN));

        HttpClient.Builder.getBaseService()
                .putInStorage(putInRequestBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseBean value) {
                        if (value.getSuccess() == 0) {
                            listener.loadSuccess("成功");
                        }else {
                            listener.loadFailed(new OError(value.getSuccess(), value.getMessage()));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.loadFailed(new OError(Constant.SystemErrorCode, e.getMessage()));
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


    /**
     * 获取跺信息
     * @param listener 返回接口
     */
    public void getPileInfo(String boxId, RequestImpl<PileInfoBean> listener) {
        UserDb userDb = DBFactory.getUserInfo(context);
        PileInfoRequestBean requestBean = new PileInfoRequestBean();
        requestBean.setAccessToken(ACache.get(context).getAsString(Constant.ACCESSTOKEN));
        requestBean.setBoxId(boxId);
        requestBean.setAccount(userDb.getAccount() == null ? "": userDb.getAccount());
        HttpClient.Builder.getBaseService()
                .getPileInfo(requestBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PileInfoBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(PileInfoBean value) {
                        if (value.getSuccess() == 0) {
                            listener.loadSuccess(value);
                        }else {
                            listener.loadFailed(new OError(value.getSuccess(),value.getMessage()));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.loadFailed(new OError(Constant.SystemErrorCode,e.getMessage()));
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}
