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.BasePageRequestBean;
import com.philisense.rdgo.bean.request.SeeBean;
import com.philisense.rdgo.bean.response.AllBoxBean;
import com.philisense.rdgo.bean.response.PutOutRecordBean;
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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
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/4.
 */

public class QuareViewModel {
    private Context context;
//    private int putInPage;
    public String startTime;
    public String endTime;
    public int type = 1;
    public String lin="";
    public String senId="";

    public QuareViewModel(Context context) {
        this.context = context;
//        putInPage = 1;
    }

    public void getPutInOrder(boolean isNextPage, boolean isRefresh,int flag, RequestImpl<AllBoxBean> listener) {



        // 如果重新刷新，则将起止时间清空

        if (isRefresh&&flag==0) {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            Date date=new Date(System.currentTimeMillis());
            String s=simpleDateFormat.format(date);
            startTime = s;
            endTime = s;
        }




//        System.out.println(startTime+","+endTime+"ddddeddddd"+type);
        ACache aCache = ACache.get(context);

        SeeBean baseRequestBean = new SeeBean();
        baseRequestBean.setAccessToken(aCache.getAsString(Constant.ACCESSTOKEN));

        if (startTime != null && endTime != null) {
            baseRequestBean.setBeginTime(startTime);
            baseRequestBean.setEndTime(endTime);
        }

        baseRequestBean.setLine(lin);
        UserDb userDb = DBFactory.getUserInfo(context);
        baseRequestBean.setAccount(userDb.getAccount() == null ? "": userDb.getAccount());

//        Gson g = new Gson();
//        System.out.println(g.toJson(baseRequestBean)+"ppppppppppp");

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

                    }

                    @Override
                    public void onNext(AllBoxBean 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() {

                    }
                });
    }



    public void getLins(RequestImpl<LineBean> 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);
                        }

                    }

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

                    }

                    @Override
                    public void onComplete() {

                    }
                });






    }








}
