package com.philisense.rdgo.viewModel;

import android.content.Context;
import android.widget.Toast;

import com.google.gson.Gson;
import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.bean.request.BoxIdRequestBean;
import com.philisense.rdgo.bean.request.UserOutBean;
import com.philisense.rdgo.bean.response.BoxBean;
import com.philisense.rdgo.bean.response.CommonBean;
import com.philisense.rdgo.bean.response.UserInfoBean;
import com.philisense.rdgo.db.DBFactory;
import com.philisense.rdgo.db.DBImpl;
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 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/15.
 */

public class MainViewModel {

    private Context context;

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

    public void updateUserInfo() {
        ACache aCache = ACache.get(context);
        String token = aCache.getAsString(Constant.ACCESSTOKEN);

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

                    }

                    @Override
                    public void onNext(UserInfoBean value) {
                        if (value.getSuccess() == 0) {
                            // 更新用户信息
                            DBFactory.UpdateHomeUser(value, new DBImpl() {
                                @Override
                                public void loadSuccess() {
                                }

                                @Override
                                public void loadFailed(String error) {
                                    Toast.makeText(context, error, Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public void getBoxId(String id, RequestImpl<BoxBean> listener) {
        ACache aCache = ACache.get(context);
        UserDb userDb = DBFactory.getUserInfo(context);
        String token = aCache.getAsString(Constant.ACCESSTOKEN);
        BoxIdRequestBean bean = new BoxIdRequestBean();
        bean.setAccessToken(token);
        bean.setBottleId(id);
        bean.setAccount(userDb.getAccount() == null ? "" : userDb.getAccount());
        HttpClient.Builder.getBaseService()
                .boxId(bean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BoxBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BoxBean 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 logout(final RequestImpl<String> listener) {
        ACache aCache = ACache.get(context);
        UserDb userDb = DBFactory.getUserInfo(context);
        String name = userDb.getAccount() == null ? "" : userDb.getAccount();
        UserOutBean bean = new UserOutBean();
        bean.setUsername(name);
        bean.setAccessToken(aCache.getAsString(Constant.ACCESSTOKEN));
        bean.setAccount(name);
//        Gson json=new Gson();
//        System.out.println(json
//        .toJson(bean)+"pppppp");
        HttpClient.Builder.getBaseService()
                .out(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) {
                            aCache.clear();
                            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() {

                    }
                });

    }

}
