package com.danding.yang800.Presenter;

import android.content.Context;
import android.content.SharedPreferences;

import com.danding.yang800.BindPhoneActivity;
import com.danding.yang800.BuildConfig;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.Util.VolleyUtil;
import com.danding.yang800.Vo.BuyGiftFlagVO;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.HashMap;

public class BindActivityPresenter {
    private BindPhoneActivity context;
    SharedPreferences sp;

    public BindActivityPresenter(BindPhoneActivity context) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
    }


    public void login(final String mobile, final String code, final String epCode) {
        if (mobile.equals("") || code.equals("")) {
            ToastUtil.show(context, "手机号或验证码不能为空！");
            return;
        }
        context.initDialog();
        HashMap<String, String> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("code", code);
        map.put("epCode", epCode);
        new VolleyUtil(context).request(context, "login.wxBindPhone", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONObject jsonData = json.getJSONObject("result");
                    JSONObject jsonUser = jsonData.getJSONObject("user");
//                    String status = jsonUser.getString("status");
                    int userId = jsonUser.getInt("userId");
                    sp.edit().putInt("userId", userId).commit();
                    String token = jsonUser.getString("token");
                    sp.edit().putString("token", token).commit();
                    JSONObject jsonStore = jsonData.getJSONObject("store");
                    String storeId = jsonStore.getString("id");
                    sp.edit().putString("storeId", storeId).commit();
                    String referralCode = "";
                    boolean shopGiftBagFlag = true;
                    try {
                        referralCode = StringUtil.toString(jsonStore.getString("referralCode"));
                        shopGiftBagFlag = jsonUser.getBoolean("shopGiftBagFlag");
                    } catch (Exception e) {

                    }
                    sp.edit().putString("referralCode", referralCode).commit();
                    if (!shopGiftBagFlag) {
                        buyCheck();
                    } else {
                        context.loginSuccess();
                    }
                    context.dismissDialog();
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();

                }
            }


            @Override
            public void onerrSuccess() {
                context.dismissDialog();
            }

            @Override
            public void onCatch() {
                context.dismissDialog();
            }

            @Override
            public void onFail() {
                context.dismissDialog();
            }
        });
    }

    public void smsSend(final String mobile) {
        if (mobile.equals("")) {
            ToastUtil.show(context, "手机号不能为空！");
            return;
        }
        context.initDialog();
        HashMap<String, String> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("type", "wxBindPhone");
        new VolleyUtil(context).request(context, "sms.send", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    context.sendSuccess();
                    context.dismissDialog();
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();

                }
            }


            @Override
            public void onerrSuccess() {
                context.dismissDialog();
            }

            @Override
            public void onCatch() {
                context.dismissDialog();
            }

            @Override
            public void onFail() {
                context.dismissDialog();
            }
        });
    }
    public void buyCheck() {
        HashMap<String, String> map = new HashMap<>();
        new VolleyUtil(context).request(context, "goods.giftBag.buyCheck", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    Gson gson = new Gson();
                    BuyGiftFlagVO bean = gson.fromJson(json.toString(), BuyGiftFlagVO.class);
                    BuyGiftFlagVO.ResultBean result = bean.getResult();
                    String buyGiftBagFlag = result.getBuyGiftBagFlag();
                    boolean singleFlag = result.isSingleFlag();
                    String storeSkuSn = result.getStoreSkuSn();
                    if (buyGiftBagFlag.equals("false")) {
                        context.toMain();
                    } else if (singleFlag) {
                        context.toGifitInfo(storeSkuSn);
                    } else {
                        context.toGiftList();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                }
            }

            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {
            }

            @Override
            public void onFail() {
            }
        });

    }

}
