package com.danding.yang800.Presenter;

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

import com.danding.yang800.BuildConfig;
import com.danding.yang800.ChangePhoneActivity;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.Util.VolleyUtil;

import org.json.JSONObject;

import java.util.HashMap;

public class ChangePhoneActivityPresenter {
    private ChangePhoneActivity context;
    SharedPreferences sp;

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


    public void updateMobile(final String userId, final String newMobile, final String verifyNewMobile) {
        if (newMobile.equals("") || verifyNewMobile.equals("")) {
            ToastUtil.show(context, "手机号不能为空！");
            return;
        }
        context.initDialog();
        HashMap<String, String> map = new HashMap<>();
        map.put("userId", userId);
        map.put("newMobile", newMobile);
        map.put("verifyNewMobile", verifyNewMobile);
        new VolleyUtil(context).request(context, "user.setting.updateMobile", true,map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    context.updateMobileSuccess();
                    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();
            }
        });
//        StringRequest stringRequest = new StringRequest(Request.Method.POST,
//                "http://" + BuildConfig.HOSTURL + "/xhr/user/setting/updateMobile",
//                new Response.Listener<String>() {
//                    @Override
//                    public void onResponse(String response) {
//                        Log.d("LoginActivity", response);
//                        //System.out.println(response);
//                        try {
//                            JSONObject json = new JSONObject(response);
//                            String code = json.getString("code");
//                            if (code.equals("success")) {
//                                context.updateMobileSuccess();
//                            } else {
//                                String errorMsg = json.getString("errorMsg");
//                                ToastUtil.show(context, errorMsg);
//                            }
//                            context.dismissDialog();
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                            context.dismissDialog();
//                        }
//
//                    }
//                }, new Response.ErrorListener() {
//
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                context.dismissDialog();
//            }
//        }) {
//            @Override
//            protected HashMap<String, String> getParams() { //
//                HashMap<String, String> map = new HashMap<String, String>();
//                map.put("userId", userId);
//                map.put("newMobile", newMobile);
//                map.put("verifyNewMobile", verifyNewMobile);
//                return map;
//            }
//
//            @Override
//            public Map<String, String> getHeaders() throws AuthFailureError {
//                if (!sp.getString("Cookie", "").equals("")) {
//                    HashMap<String, String> headers = new HashMap<String, String>();
//                    headers.put("Cookie", sp.getString("Cookie", ""));
//                    return headers;
//                } else {
//                    return super.getHeaders();
//                }
//            }
//        };
//        stringRequest.setTag("POST_TAG");
//        requestQueue.add(stringRequest);
    }


}
