package com.danding.yang800.Presenter;

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

import com.danding.yang800.BuildConfig;
import com.danding.yang800.CheckPhoneActivity;
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 CheckPhoneActivityPresenter {
    private CheckPhoneActivity context;
    SharedPreferences sp;

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


    public void getMobileCode(final String userId) {
        context.initDialog();
        HashMap<String, String> map = new HashMap<>();
        map.put("userId", userId);
        map.put("smsType", "3");
        new VolleyUtil(context).request(context,  "user.setting.getMobileCode", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    ToastUtil.show(context, "验证码已成功发送");
                    context.startTime();
                    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 getInfo(final String userId) {
        context.initDialog();
        HashMap<String, String> map = new HashMap<>();
        map.put("userId", userId);
        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);
                    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 messageCode(final String userId, final String verifyCode) {
        if (verifyCode.equals("")) {
            ToastUtil.show(context, "验证码不能为空！");
            return;
        }
        context.initDialog();
        HashMap<String, String> map = new HashMap<>();
        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 {
                    JSONObject jsonObject = json.getJSONObject("result");
                    boolean isWithdrawCashPassword = jsonObject.getBoolean("withdrawCashPassword");
                    context.messageCodeSuccess(isWithdrawCashPassword);
                    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/verify/messageCode",
//                new Response.Listener<String>() {
//                    @Override
//                    public void onResponse(String response) {
//                        Log.d("LoginActivity", response);
//                        Log.d("LoginActivity", sp.getString("Cookie", ""));
//                        //System.out.println(response);
//                        try {
//                            JSONObject json = new JSONObject(response);
//                            String code = json.getString("code");
//                            if (code.equals("success")) {
//                                JSONObject jsonObject = json.getJSONObject("data");
//                                boolean isWithdrawCashPassword = jsonObject.getBoolean("withdrawCashPassword");
//                                context.messageCodeSuccess(isWithdrawCashPassword);
//                            } else {
//                                ToastUtil.show(context, "验证码错误");
//                            }
//                            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("verifyCode", verifyCode);
//                map.put("smsType", "3");
//                return map;
//            }
//
//            protected Response<String> parseNetworkResponse(
//                    NetworkResponse response) {
//                try {
//                    Map<String, String> responseHeaders = response.headers;
//                    String rawCookies = responseHeaders.get("Set-Cookie");
//                    if (sp.getString("Cookie", "").equals("")) {
//                        sp.edit().putString("Cookie", rawCookies).commit();
//                    }
//                    String dataString = new String(response.data, "UTF-8");
//                    return Response.success(dataString,
//                            HttpHeaderParser.parseCacheHeaders(response));
//                } catch (UnsupportedEncodingException e) {
//                    return Response.error(new ParseError(e));
//                }
//            }
//
//            @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);
    }


}
