package com.mints.goodmoney.mvp.presenters;

import com.google.gson.JsonObject;
import com.mints.goodmoney.manager.AppHttpManager;
import com.mints.goodmoney.mvp.model.BaseResponse;
import com.mints.goodmoney.mvp.model.WaterBean;
import com.mints.goodmoney.mvp.views.WaterView;
import com.mints.library.net.neterror.BaseSubscriber;
import com.mints.library.net.neterror.Throwable;

public class WaterPresenter extends BasePresenter<WaterView> {

    /**
     * 获取首页喝水信息
     */
    public void getWaterMsg() {
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.getWaterMsg(),
                        new BaseSubscriber<BaseResponse<WaterBean>>() {
                            @Override
                            public void onCompleted() {
                                if (isLinkView()) return;

                                view.hideLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
                                if (isLinkView()) return;

                                view.hideLoading();
                                view.showToast(e.getMessage());
                            }

                            @Override
                            public void onNext(BaseResponse<WaterBean> baseResponse) {
                                if (isLinkView()) return;

                                int code = baseResponse.getStatus();
                                String message = baseResponse.getMessage();
                                switch (code) {
                                    case 200://成功
                                        view.getWaterMsgHomeSuc(baseResponse.getData());
                                        break;
                                    default:
                                        view.showToast(message);
                                        break;
                                }
                            }
                        });
    }

    /**
     * 首页喝水
     */
    public void clickForWater() {
        view.showLoading("加载中...");

        AppHttpManager.getInstance(loanApplication)
                .call(loanService.clickForWater(),
                        new BaseSubscriber<BaseResponse<JsonObject>>() {
                            @Override
                            public void onCompleted() {
                                if (isLinkView()) return;

                                view.hideLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
                                if (isLinkView()) return;

                                view.hideLoading();
                                view.showToast(e.getMessage());
                            }

                            @Override
                            public void onNext(BaseResponse<JsonObject> baseResponse) {
                                if (isLinkView()) return;

                                int code = baseResponse.getStatus();
                                String message = baseResponse.getMessage();
                                switch (code) {
                                    case 200://成功
                                        JsonObject data = baseResponse.getData();
                                        if (data != null) {
                                            int coin = data.get("coin").getAsInt();
                                            view.clickForWaterSuc(coin);
                                        } else {
                                            view.clickForWaterSuc(0);
                                        }
                                        break;
                                    default:
                                        view.showToast(message);
                                        break;
                                }
                            }
                        });
    }

}
