package com.mints.goodmoney.mvp.presenters;

import android.text.TextUtils;

import com.mints.goodmoney.MintsApplication;
import com.mints.goodmoney.common.DeviceInfo;
import com.mints.goodmoney.manager.AppHttpManager;
import com.mints.goodmoney.manager.ShumeiManager;
import com.mints.goodmoney.manager.UserManager;
import com.mints.goodmoney.mvp.model.BaseResponse;
import com.mints.goodmoney.mvp.model.DrawcashBean;
import com.mints.goodmoney.mvp.model.UserBean;
import com.mints.goodmoney.mvp.views.DrawcashView;
import com.mints.goodmoney.utils.DeviceUuidFactory;
import com.mints.library.net.neterror.BaseSubscriber;
import com.mints.library.net.neterror.Throwable;

import java.util.HashMap;

public class DrawcashPresenter extends BasePresenter<DrawcashView> {

    /**
     * 获取用户配置信息
     */
    public void getUserTaskMsg() {
        view.showLoading("加载中...");

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

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

    /**
     * 修改用户信息
     */
    public void editUserMsg(String wxInfo) {
        HashMap<String, Object> vo = new HashMap<>();
        vo.put("type", "OPENID");
        vo.put("value", wxInfo);

        AppHttpManager.getInstance(loanApplication)
                .call(loanService.bindingWechat(vo),
                        new BaseSubscriber<BaseResponse<UserBean>>() {
                            @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<UserBean> baseResponse) {
                                if (isLinkView()) return;

                                int code = baseResponse.getStatus();
                                String message = baseResponse.getMessage();
                                switch (code) {
                                    case 200://成功
                                        UserBean data = baseResponse.getData();
                                        if (data != null) {
                                            UserManager.getInstance().saveUserInfo(data);

                                            view.editUserMsgSuc();
                                        }
                                        break;
                                    default:
                                        view.showToast(message);
                                        break;
                                }
                            }
                        });
    }

    /**
     * 提交设备信息
     */
    public void saveTerminalInfo( double curCash) {
        view.showLoading("加载中...");

        HashMap<String, Object> vo = new HashMap<>();
        DeviceInfo deviceInfo = DeviceInfo.Companion.getInstance();
        String macAddress = deviceInfo.getMacAddress();
        if (!TextUtils.isEmpty(macAddress)) {
            String mac = macAddress.replace(":", "");
            vo.put("mac", mac);
            vo.put("mac1", macAddress);

        }
        vo.put("imei", deviceInfo.getIMEI());
        vo.put("androidid", deviceInfo.getAndroidId(null));
        vo.put("os", "android");
        vo.put("shumeiId", ShumeiManager.getInstance().getShumeiDeviceId());
        vo.put("model", deviceInfo.getBrand() + " " + deviceInfo.getMobileModel());
        vo.put("uuid", new DeviceUuidFactory().getDeviceUuid());
        vo.put("osversion", deviceInfo.getOSVersion());
        vo.put("appversion", deviceInfo.getVersionName());
        vo.put("oaid", MintsApplication.OAID);

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

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

                                view.hideLoading();
                            }

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

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

    /**
     * 金币/积分兑换
     */
    public void addCashoutReq(double cash) {

        HashMap<String, Object> vo = new HashMap<>();
        vo.put("cash", cash);
        vo.put("payChannel", "WEIXIN");
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.addCashoutReq(vo),
                        new BaseSubscriber<BaseResponse<Object>>() {
                            @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<Object> baseResponse) {
                                if (isLinkView()) return;

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