package com.philisense.rdgo.viewModel;

import android.content.Context;

import com.google.gson.Gson;
import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.bean.request.PileInfoRequestBean;
import com.philisense.rdgo.bean.request.ResetRequestBean;
import com.philisense.rdgo.bean.response.CommonBean;
import com.philisense.rdgo.bean.response.PileInfoBean;
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 com.philisense.rdgo.util.MD5Util;

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 ResetViewModel {
    private Context context;

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

    /**
     * 获取跺信息
     *
     * @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());
        Gson g = new Gson();
        System.out.println(g.toJson(requestBean) + "pppppp");
        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() {

                    }
                });
    }

    /**
     * 获取跺信息
     *
     * @param listener 返回接口
     */
    public void reset(String username, String password, String boxId, RequestImpl<String> listener) {
        UserDb userDb = DBFactory.getUserInfo(context);
        ResetRequestBean requestBean = new ResetRequestBean();
        requestBean.setAccessToken(ACache.get(context).getAsString(Constant.ACCESSTOKEN));
        requestBean.setBoxId(boxId);
        requestBean.setAccount(userDb.getAccount() == null ? "" : userDb.getAccount());
        requestBean.setUsername(username);
        requestBean.setPassword(MD5Util.getMD5(password));

        HttpClient.Builder.getBaseService()
                .reset(requestBean)
                .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());
                        } 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() {

                    }
                });
    }

}
