package com.philisense.rdgo.viewModel;

import android.content.Context;

import com.google.gson.Gson;
import com.philisense.rdgo.bean.LineBean;
import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.bean.request.PeopleRequestBean;
import com.philisense.rdgo.bean.request.ProductRequestBean;
import com.philisense.rdgo.bean.request.PutInRequestBean;
import com.philisense.rdgo.bean.request.WhRequestBean;
import com.philisense.rdgo.bean.request.addRequestBean;
import com.philisense.rdgo.bean.response.BaseBean;
import com.philisense.rdgo.bean.response.CommonBean;

import com.philisense.rdgo.bean.response.PeopleBean;
import com.philisense.rdgo.bean.response.ProductBean;
import com.philisense.rdgo.bean.response.TypeBean;
import com.philisense.rdgo.bean.response.WhBean;
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 2017/12/13.
 */

public class HQTViewModel {
    private Context context;
    public String brankProduct = "";
    public String geneProduct = "";
    public String levelProduct = "";
    public String capacityProduct = "";

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

    public void getWh(RequestImpl<List<WhBean.ListBean>> listener) {
        WhRequestBean bean = new WhRequestBean();
        ACache aCache = ACache.get(context);
        String accessToken = aCache.getAsString(Constant.ACCESSTOKEN);
        UserDb userDb = DBFactory.getUserInfo(context);
        bean.setAccessToken(accessToken);
        bean.setNum(100);
        bean.setPage(1);
        bean.setCode("");
        bean.setAccount(userDb.getAccount() == null ? "" : userDb.getAccount());
        bean.setName("");
        bean.setRegion("");
        bean.setAddress("");
        bean.setStartPoint("");
        bean.setCharger("");
        bean.setChargerPhone("");
        HttpClient.Builder.getBaseService()
                .getWhSearch(bean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WhBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

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

                        }


                    }

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

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public void getLins(RequestImpl<List<LineBean.ListBean>> listener) {
        ACache aCache = ACache.get(context);
        HttpClient.Builder.getBaseService()
                .getlins(aCache.getAsString(Constant.ACCESSTOKEN))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LineBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LineBean value) {
                        if (value.getSuccess() == 0) {
                            listener.loadSuccess(value.getList());
                        }

                    }

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

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public void getPeople(RequestImpl<List<PeopleBean.ListBean>> listener) {
        PeopleRequestBean bean = new PeopleRequestBean();
        ACache aCache = ACache.get(context);
        UserDb userDb = DBFactory.getUserInfo(context);
        String accessToken = aCache.getAsString(Constant.ACCESSTOKEN);
        bean.setAccessToken(accessToken);
        bean.setAccount(userDb.getAccount() == null ? "" : userDb.getAccount());
        bean.setFactory("");
        bean.setNum(150);
        bean.setPage(1);
        bean.setPersonCode("");
        bean.setPersonName("");
        bean.setPersonPhone("");

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

                    }

                    @Override
                    public void onNext(PeopleBean value) {
                        if (value.getSuccess() == 0) {
                            listener.loadSuccess(value.getList());
                        }

                    }

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

                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    public void getType(RequestImpl<TypeBean> listener) {
        ACache aCache = ACache.get(context);
        HttpClient.Builder.getBaseService()
                .gettype(aCache.getAsString(Constant.ACCESSTOKEN))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<TypeBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(TypeBean value) {
                        if (value.getSuccess() == 0 && value.getData() != null) {
                            listener.loadSuccess(value);
                        }

                    }

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

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getproduct(RequestImpl<List<ProductBean.ListBean>> listener) {

        ProductRequestBean bean = new ProductRequestBean();
        ACache aCache = ACache.get(context);
        String accessToken = aCache.getAsString(Constant.ACCESSTOKEN);
        UserDb userDb = DBFactory.getUserInfo(context);
        bean.setAccessToken(accessToken);
        bean.setNum(100);
        bean.setPage(1);
        bean.setTypeCode("");
        bean.setAccount(userDb.getAccount() == null ? "" : userDb.getAccount());
        bean.setTypeName("");
        bean.setBrand(brankProduct);
        bean.setGene(geneProduct);
        bean.setLevel(levelProduct);
        bean.setCapacity(capacityProduct);
        bean.setSources("");
        bean.setBottleNum("");
        Gson g = new Gson();
        System.out.println(g.toJson(bean) + "pppppp");
        HttpClient.Builder.getBaseService()
                .getTypeSearch(bean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ProductBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

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

                        }


                    }

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

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void addpile(addRequestBean bean, RequestImpl<String> listener) {
        Gson g = new Gson();
        System.out.println(g.toJson(bean) + "pppppp");
        HttpClient.Builder.getBaseService()
                .getpilewait(bean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CommonBean value) {
                        if (value.getSuccess() == 0) {
                            listener.loadSuccess(value.getMessage());
                        }

                    }

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

                    }
                });

    }

}
