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.WalkBean;
import com.mints.goodmoney.mvp.views.WalkView;
import com.mints.goodmoney.mvp.views.WaterView;
import com.mints.library.net.neterror.BaseSubscriber;
import com.mints.library.net.neterror.Throwable;

public class WalkPresenter extends BasePresenter<WalkView> {

    public void getWalkMsg() {
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.getWalkMsg(),
                        new BaseSubscriber<BaseResponse<WalkBean>>() {
                            @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<WalkBean> baseResponse) {
                                if (isLinkView()) return;

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

    public void clickForWalk() {
        view.showLoading("加载中...");

        AppHttpManager.getInstance(loanApplication)
                .call(loanService.clickForWalk(),
                        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.clickForWalkSuc(coin);
                                        } else {
                                            view.clickForWalkSuc(0);
                                        }
                                        break;
                                    default:
                                        view.showToast(message);
                                        break;
                                }
                            }
                        });
    }

}
