package com.danding.yang800.Presenter;

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

import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.ProvePhoneActivity;
import com.danding.yang800.RealNameActivity;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.Util.VolleyUtil;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by Administrator on 2017/1/12.
 */
public class RealNameActivityPresenter {
    private RealNameActivity context;
    SharedPreferences sp;

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

    public void getmobile(final String userId) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("userId", userId);
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/user/setting/toUpdateUserInfo", true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "user.setting.toUpdateUserInfo", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONObject jsonObject = json.getJSONObject("result");
                    String mobile = jsonObject.getString("mobile");
                    context.initData(mobile);
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                }

            }


            @Override
            public void onerrSuccess() {
            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {

            }
        });
    }

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

            @Override
            public void onerrSuccess() {
            }

            @Override
            public void onCatch() {
            }

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

    public void updateRealname(final String realname, final String code) {
        if (code.equals("")) {
            ToastUtil.show(context, "验证码不能为空！");
        }
        if (realname.equals("")) {
            ToastUtil.show(context, "姓名不能为空！");
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("realname", realname);
        map.put("code", code);
        new VolleyUtil(context).request(context, "user.setting.updateRealname", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    ToastUtil.show(context, "提交成功");
                    context.finish();
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                }

            }

            @Override
            public void onerrSuccess() {
            }

            @Override
            public void onCatch() {
            }

            @Override
            public void onFail() {

            }
        });
    }

//    public void messageCode(final String userId, final String verifyCode) {
//        if (verifyCode.equals("")) {
//            ToastUtil.show(context, "验证码不能为空！");
//            return;
//        }
//        context.initDialog();
//        HashMap<String, String> map = new HashMap<String, String>();
//        map.put("userId", userId);
//        map.put("verifyCode", verifyCode);
//        map.put("smsType", "3");
//        new VolleyUtil(context).request(context,  "user.setting.verify.messageCode", true, map, new HttpCallBack() {
//            @Override
//            public void onSuccess(JSONObject json) {
//                try {
//                    context.messageCodeSuccess();
//                    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();
//            }
//        });
//    }
//

}
