package com.philisense.rdgo.viewModel;

import android.content.Context;

import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.bean.request.OrderDetailRequestBean;
import com.philisense.rdgo.bean.request.PutInRequestBean;
import com.philisense.rdgo.bean.response.BaseBean;
import com.philisense.rdgo.bean.response.OrderDetailBean;
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.ui.putIn.PutInOrderActivity;
import com.philisense.rdgo.util.Constant;
import com.philisense.rdgo.util.TreeNodeInsertSubscribe;
import com.philisense.rdgo.view.OrderInViewHolder;
import com.unnamed.b.atv.model.TreeNode;

import java.util.List;

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

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

public class PutInOrderViewModel {
    private Context context;
    private TreeNode root;

    public Observable<Boolean> completeAvailable;
    public boolean enableEdit;

    public PutInOrderViewModel(Context context) {
        this.context = context;
        completeAvailable = Observable.combineLatest(new Observable[]{Observable.create(new TreeNodeInsertSubscribe((PutInOrderActivity) context))}, new Function<Object,Boolean>() {
            @Override
            public Boolean apply(Object o) throws Exception {
                for (TreeNode node : root.getChildren()) {
                    OrderInViewHolder.OrderInfo data = (OrderInViewHolder.OrderInfo) node.getValue();
                    if (data.getBoxNum() != 0) {
                        return false;
                    }
                }
                return true;
            }
        });

    }

    public TreeNode getRoot() {
        return root;
    }

    public void setRoot(TreeNode root) {
        this.root = root;
    }

    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() {

                    }
                });
    }

    public void getOrderDetail(String orderId, RequestImpl<OrderDetailBean> listener) {
        OrderDetailRequestBean requestBean = new OrderDetailRequestBean();
        UserDb userDb = DBFactory.getUserInfo(context);
        requestBean.setAccessToken(ACache.get(context).getAsString(Constant.ACCESSTOKEN));
        requestBean.setSendId(orderId);
        requestBean.setAccount(userDb.getAccount() == null ? "" : userDb.getAccount());

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

                    }

                    @Override
                    public void onNext(OrderDetailBean value) {
                        if (value.getSuccess() == 0) {
                            enableEdit = value.getData().getStatus() == 1;
                            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() {

                    }
                });
    }

    /**
     * 入库
     * @param sendId 配送单Id
     * @param listener 监听
     */
    public void putInAction(String sendId, RequestImpl<String> listener) {


        ACache aCache = ACache.get(context);

        UserDb userDb = DBFactory.getUserInfo(context);

        PutInRequestBean putInRequestBean= new PutInRequestBean();
        putInRequestBean.setAccessToken(aCache.getAsString(Constant.ACCESSTOKEN));
        putInRequestBean.setAccount(userDb.getAccount() == null ? "": userDb.getAccount());
        putInRequestBean.setName(userDb.getName() == null ? "" : userDb.getName());
        putInRequestBean.setType(0); // 生产出库
        putInRequestBean.setWarehouse(userDb.getWarehouse());
        putInRequestBean.setWarehouseCode(userDb.getWarehouseCode());
        putInRequestBean.setId(sendId);
        putInRequestBean.setBoxId("");

        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() {

                    }
                });
    }
}
