package com.philisense.rdgo.viewModel;

import android.content.Context;

import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.bean.request.RemoveRequestBean;
import com.philisense.rdgo.bean.response.BaseBean;
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/9.
 */

public class RemoveViewModel {
    private Context context;

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

    public void removeAction(String boxId, RequestImpl<String> listener) {
        UserDb userDb = DBFactory.getUserInfo(context);


        String token = ACache.get(context).getAsString(Constant.ACCESSTOKEN);
        RemoveRequestBean bean = new RemoveRequestBean();
        bean.setAccessToken(token);
        bean.setBoxId(boxId);
        bean.setWarehouse(userDb.getWarehouse());
        bean.setWarehouseCode(userDb.getWarehouseCode());
        bean.setAccount(userDb.getAccount());
        bean.setName(userDb.getName());

        HttpClient.Builder.getBaseService()
                .removeAction(bean)
                .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(value.getMessage());
                        }else {
                            listener.loadFailed(new OError(value.getSuccess(), value.getMessage()));
                        }
                    }

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

                    @Override
                    public void onComplete() {

                    }
                });
    }

    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 inRemoveAction(String boxId, RequestImpl<String> listener) {

        UserDb userDb = DBFactory.getUserInfo(context);
        String token = ACache.get(context).getAsString(Constant.ACCESSTOKEN);
        RemoveRequestBean bean = new RemoveRequestBean();
        bean.setAccessToken(token);
        bean.setBoxId(boxId);
        bean.setWarehouse(userDb.getWarehouse());
        bean.setWarehouseCode(userDb.getWarehouseCode());
        bean.setAccount(userDb.getAccount());
        bean.setName(userDb.getName());

        HttpClient.Builder.getBaseService()
                .inRemoveAction(bean)
                .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(value.getMessage());
                        }else {
                            listener.loadFailed(new OError(value.getSuccess(), value.getMessage()));
                        }
                    }

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

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
