package com.yyrh.networking;

import android.app.Activity;

import android.os.Message;

import android.widget.Toast;


import com.yyrh.constant.InitConfig;
import com.yyrh.constant.LoginData;
import com.yyrh.pay.PaymentNotify;
import com.yyrh.sdk.SDKEntry;
import com.yyrh.sdk.callback.ShowPolicyCallback;

import com.yyrh.ui.fragment.UserPhoneLoginFragment;
import com.yyrh.ui.fragment.UserPhoneLoginSetpwdFragment;
import com.yyrh.ui.fragment.UserRetrievePwdFragment;
import com.yyrh.utils.ResourceUtil;
import com.yyrh.utils.SignUtils;
import com.yyrh.utils.SwitchUtil;
import com.yyrh.utils.TosUtil;
import com.yyrh.utils.Utils;
import com.yyrh.utils.YYLog;

import com.yyrh.constant.Constants;
import com.yyrh.constant.SDKSettings;
import com.yyrh.networking.urlhttp.CallBackUtil;
import com.yyrh.networking.urlhttp.UrlHttpUtil;
import com.yyrh.sdk.callback.InitCallback;
import com.yyrh.sdk.callback.LoginCallback;
import com.yyrh.sdk.callback.PayOrderCallback;
import com.yyrh.sdk.callback.RegisterCallback;
import com.yyrh.ui.ProgressDialogUtils;
import com.yythird.sdk.ChannelSDK;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


import static com.yyrh.constant.Constants.LOGIN;
import static com.yyrh.constant.SDKSettings.channelTag;

import static com.yyrh.constant.SDKSettings.isThirdLogin;
import static com.yyrh.constant.SDKSettings.isThirdPay;

import static com.yyrh.constant.SDKSettings.loginData;
import static com.yyrh.constant.SDKSettings.order_ext;
import static com.yyrh.constant.URLConstants.AGREEMENTINIT;
import static com.yyrh.constant.URLConstants.BASEURL;
import static com.yyrh.constant.URLConstants.CHANNELLOGIN;
import static com.yyrh.constant.URLConstants.CHANNELZTAG;
import static com.yyrh.constant.URLConstants.FASTREGISTER;
import static com.yyrh.constant.URLConstants.GETCAPTCHA;
import static com.yyrh.constant.URLConstants.INIT;

import static com.yyrh.constant.URLConstants.LOGINWITHSMS;
import static com.yyrh.constant.URLConstants.QUERYORDER;
import static com.yyrh.constant.URLConstants.REALNAMERECORD;
import static com.yyrh.constant.URLConstants.RECHARGEINT;
import static com.yyrh.constant.URLConstants.REGISTERWITHPHONE;
import static com.yyrh.constant.URLConstants.RESETPWDWITHPHONE;
import static com.yyrh.constant.URLConstants.RSAKEY;
import static com.yyrh.constant.URLConstants.UPLOADROLEINFO;
import static com.yyrh.constant.URLConstants.USERLOGIN;


public class NetRequestApi {
    public static void init(final Activity activity, final InitCallback initCallback) {



        HashMap<String, String> map = getCommonParams();

        String time = SignUtils.toURLEncoded(System.currentTimeMillis() / 1000 + "");
        String paramSign = SignUtils.paramSign(map, time);
        map.put("time", time);
        map.put("sign", paramSign);




        if (BASEURL.equals("http://testsdkapi.yyxxgame.com")) {
            Toast.makeText(activity, "正在使用测试环境，请勿上线" + BASEURL, Toast.LENGTH_SHORT).show();
        }

        UrlHttpUtil.post(activity,INIT, map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {
                  YYLog.i(errorMessage);


                initCallback.onInitError(activity.getString(ResourceUtil.getStringId(activity, "hnyy_network_error_tip")));

            }

            @Override
            public void onResponse(String response) {

                try {
                    JSONObject json = new JSONObject(response);
                    int code = json.optInt("code", -1);
                    String msg = json.optString("msg", "");
                    if (code != 1) {
                        initCallback.onInitFailed(msg);
                        return;
                    }
                    JSONObject data = json.getJSONObject("data");

                    SDKSettings.initConfig = InitConfig.toBean(data);

                    if (SDKSettings.initConfig.sdk_login == 1) {
                        if (channelTag.equals(CHANNELZTAG)) {
                            isThirdLogin = false;
                        } else {
                            isThirdLogin = true;
                        }


                    } else {

                        SDKSettings.UserCentUrl = data.optString("h5_web_host", "");

                        isThirdLogin = false;
                    }


                    SwitchUtil.ControlKFSwitch(SDKSettings.initConfig.bswitch);

                    YYLog.i("初始化成功。");
                    initCallback.onInitSuccess("init success");

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

    }

    public static void loginWithYy(boolean isShowPrg, final Activity activity, String account, String password,String login_token, final LoginCallback loginCallback) {


        HashMap<String, String> map = getCommonParams();


        map.put("smt_type", SignUtils.toURLEncoded(SDKSettings.isSimulator));

        if (Utils.judgeStrNull(password)){
            map.put("login_token", login_token);
        }else {
            String uname_pwd = null;

            try {
                uname_pwd = SignUtils.encryptRsaByPublicKey(RSAKEY, account + "|" + password);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            map.put("uname_pwd", uname_pwd);
        }

        map.put("package_name", SDKSettings.package_name);

        String time = SignUtils.toURLEncoded(System.currentTimeMillis() / 1000 + "");
        String paramSign = SignUtils.paramSign(map, time);
        map.put("time", time);
        map.put("sign", paramSign);


        UrlHttpUtil.post(activity,USERLOGIN, map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {

                new TosUtil(activity, TosUtil.TOAST_ERROR, activity.getString(ResourceUtil.getStringId(activity, "hnyy_network_error_tip"))).show();

                loginCallback.onLoginError(activity.getString(ResourceUtil.getStringId(activity, "hnyy_network_error_tip")));
            }

            @Override
            public void onResponse(String response) {


                JSONObject json = null;
                JSONObject data = null;
                try {
                    json = new JSONObject(response);
                    data = json.getJSONObject("data");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                int code = json.optInt("code", -1);
                if (code != 1) {

                    YYLog.i("登录失败:" + json.optString("msg", "登录失败"));
                    loginCallback.onLoginFailed(json.optString("msg", "登录失败"));
                    return;
                }

                SDKSettings.loginData = LoginData.toBean(data);




                SDKSettings.LOGINTAG = LOGIN;

                if (loginData.is_rn == 0) {
                    SDKSettings.isVefityRealName = false;
                } else {
                    SDKSettings.isVefityRealName = true;
                }

                YYLog.i("登录成功。+ token：" +  loginData.token);

                loginCallback.onLoginSuccess(loginData.token);

            }
        });

    }

    public static void registerWithYy(final Activity activity, String account, String password, final RegisterCallback registerCallback) {



        ProgressDialogUtils.show(activity, activity.getString(ResourceUtil.getStringId(activity, "hnyy_registering")));

        HashMap<String, String> map = getCommonParams();


        map.put("smt_type", SignUtils.toURLEncoded(SDKSettings.isSimulator));


        String uname_pwd = null;

        try {
            uname_pwd = SignUtils.encryptRsaByPublicKey(RSAKEY, account + "|" + password);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        map.put("uname_pwd", uname_pwd);
        map.put("package_name", SDKSettings.package_name);

        String time = SignUtils.toURLEncoded(System.currentTimeMillis() / 1000 + "");
        String paramSign = SignUtils.paramSign(map, time);
        map.put("time", time);
        map.put("sign", paramSign);




        UrlHttpUtil.post(activity,FASTREGISTER, map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {

                registerCallback.onRegisterFailed(activity.getString(ResourceUtil.getStringId(activity, "hnyy_network_error_tip")));

            }

            @Override
            public void onResponse(String response) {

                JSONObject json = null;
                JSONObject data = null;
                try {
                    json = new JSONObject(response);
                    data = json.getJSONObject("data");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                int code = json.optInt("code", -1);
                if (code != 1) {
                    YYLog.i("注册账号失败:" + json.optString("msg", "注册失败"));
                    registerCallback.onRegisterFailed(json.optString("msg", "注册失败"));
                    return;
                }

                SDKSettings.loginData = LoginData.toBean(data);





                YYLog.i("注册账号成功。 token：" + loginData.token);
                registerCallback.onRegisterSuccess(loginData.token);

            }
        });

    }

    public static void loginWithChannel(final Activity activity, String channelLoginInfo, final LoginCallback loginCallback) {

          HashMap<String, String> map = getCommonParams();


        map.put("sdk_params", channelLoginInfo);

        map.put("package_name", SDKSettings.package_name);


        String time = SignUtils.toURLEncoded(System.currentTimeMillis() / 1000 + "");
        String paramSign = SignUtils.paramSign(map, time);
        map.put("time", time);
        map.put("sign", paramSign);



        UrlHttpUtil.post(activity,CHANNELLOGIN, map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {


                new TosUtil(activity, TosUtil.TOAST_ERROR, activity.getString(ResourceUtil.getStringId(activity, "hnyy_network_error_tip"))).show();


                loginCallback.onLoginError(activity.getString(ResourceUtil.getStringId(activity, "hnyy_network_error_tip")));
                YYLog.i(errorMessage);
            }

            @Override
            public void onResponse(String response) {



                JSONObject json = null;
                JSONObject data = null;
                String errMsg = null;
                try {
                    json = new JSONObject(response);
                    errMsg = json.optString("msg", "登录验证失败");
                    data = json.getJSONObject("data");
                } catch (JSONException e) {
                    e.printStackTrace();
                    SDKEntry.getSdkInstance().sdkCallback.onLoginChannelFail(errMsg);
                    return;
                }
                int code = json.optInt("code", -1);
                if (code != 1) {
                    YYLog.i("登录验证失败");
                    String msg = json.optString("msg", "登录验证失败");
                    loginCallback.onLoginFailed(msg);
                    SDKEntry.getSdkInstance().sdkCallback.onLoginChannelFail(msg);
                    return;
                }
                SDKSettings.loginData = LoginData.toBean(data);



                SDKEntry.getSdkInstance().sdkCallback.onLoginChannelSuccess(data.toString());
                YYLog.i("渠道登录验证成功。token:" + loginData.token);

                loginCallback.onLoginSuccess(loginData.token);
            }
        });

    }

    public static void upLoadingRoleInfo(String type, Activity activity,
                                         String roleId, String roleName, String roleLevel, String serverId,
                                         String serverName, String hasGold, String vipLevel, String role_power, String partyId, String partyName, String createTime, String charge) {

        HashMap<String, String> map = getCommonParams();


        if (type.equals(Constants.UPLOADTYPE_CREATEROLE)) {
            map.put("report_role_type", "createRole");
        } else if (type.equals(Constants.UPLOADTYPE_ENTERGAME)) {
            map.put("report_role_type", "enterGame");
        } else if (type.equals(Constants.UPLOADTYPE_LEVELUP)) {
            map.put("report_role_type", "roleUpgrade");
            return;
        }
        if (partyName.equals("暂无")) {
            partyName = "";
        }


        if (createTime.length() != 10) {

            new TosUtil(activity, TosUtil.TOAST_ERROR, activity.getString(ResourceUtil.getStringId(activity, "hnyy_ctime_format_error"))).show();

            return;
        }


        map.put("uname", loginData.uname);
        map.put("channel_uid", loginData.channel_uid);
        map.put("party_id", partyId);
        map.put("party_name", partyName);
        map.put("server_id", serverId);
        map.put("server_name", serverName);
        map.put("role_id", roleId);
        map.put("role_name", roleName);
        map.put("role_level", roleLevel);
        map.put("vip_level", vipLevel);
        map.put("role_power", role_power);
        map.put("role_gold", hasGold);
        map.put("role_ctime", createTime);

        map.put("role_charge", charge);


        String time = SignUtils.toURLEncoded(System.currentTimeMillis() / 1000 + "");
        String paramSign = SignUtils.paramSign(map, time);
        map.put("time", time);
        map.put("sign", paramSign);




        UrlHttpUtil.post(activity,UPLOADROLEINFO, map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {

                YYLog.i(errorMessage);
            }

            @Override
            public void onResponse(String response) {


                try {
                    JSONObject json = new JSONObject(response);
                    int code = json.optInt("code", -1);
                    if (code == 1) {
                        YYLog.i("上传角色信息成功。");
                        return;
                    } else {
                        YYLog.i("上传角色信息失败，请检查参数是否为空。");
                    }

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }
        });

    }

    public static void payOrderInit(String url,final HashMap<String, Object> map,
                                    final Activity activity, final PayOrderCallback callback) {



        String cp_order_id = (String) map.get("game_no");
        String money = (String) map.get("pay_money");
        String server_id = (String) map.get("server_id");
        String server_name = (String) map.get("server_name");
        String role_id = (String) map.get("role_id");
        String role_level = (String) map.get("role_level");
        String role_name = (String) map.get("role_name");
        String ext = (String) map.get("ext");
        String order_name = (String) map.get("order_desc");
        String product_id = (String) map.get("productId");


        if (Utils.judgeStrNull(cp_order_id) || Utils.judgeStrNull(money)
                || Utils.judgeStrNull(server_id)
                || Utils.judgeStrNull(server_name)
                || Utils.judgeStrNull(role_id)
                || Utils.judgeStrNull(role_name)
                || Utils.judgeStrNull(ext)
                || Utils.judgeStrNull(order_name)
                || Utils.judgeStrNull(product_id)
        ) {

            YYLog.i("下单参数有空值，请检查: " + map.toString());
            return;

        }

        YYLog.i("下单参数: " + map.toString());

        String app_name = Utils.getApplicationName(activity);

        String role_ctime = (String) map.get("role_ctime");
        if (Utils.judgeStrNull(role_ctime)) {
            new TosUtil(activity, TosUtil.TOAST_ERROR, activity.getString(ResourceUtil.getStringId(activity, "hnyy_pls_upload_roleinfo_frist"))).show();

            return;
        }


        HashMap<String, String> orderMap = getCommonParams();


        orderMap.put("uname", loginData.uname);
        orderMap.put("channel_uid", loginData.channel_uid);
        orderMap.put("cp_order_id", cp_order_id);
        orderMap.put("money", money);
        orderMap.put("server_id", server_id);
        orderMap.put("server_name", server_name);
        orderMap.put("role_id", role_id);
        orderMap.put("role_name", role_name);
        orderMap.put("role_level", role_level);
        orderMap.put("ext", ext);
        orderMap.put("order_name", order_name);
        orderMap.put("app_name", app_name);

        orderMap.put("role_ctime", role_ctime);
        orderMap.put("product_id", product_id);


        String channelOrderExt = ChannelSDK.getInstance().SDKGetOrderExt();

        if (!Utils.judgeStrNull(channelOrderExt)) {
            orderMap.put("sdk_params", channelOrderExt);
        }

        if (!Utils.judgeStrNull(order_ext)) {
            orderMap.put("order_ext", SignUtils.toURLEncoded(SDKSettings.order_ext));
        }




        String time = SignUtils.toURLEncoded(System.currentTimeMillis() / 1000 + "");
        String paramSign = SignUtils.paramSign(orderMap, time);
        orderMap.put("time", time);
        orderMap.put("sign", paramSign);

        UrlHttpUtil.post(activity,url, orderMap, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {

                new TosUtil(activity, TosUtil.TOAST_ERROR, activity.getString(ResourceUtil.getStringId(activity, "hnyy_network_error_tip"))).show();

                callback.onPayFailed(activity.getString(ResourceUtil.getStringId(activity, "hnyy_network_error_tip")));

                YYLog.i(errorMessage);
            }

            @Override
            public void onResponse(String response) {

                try {
                    JSONObject json = new JSONObject(response);
                    int code = json.optInt("code", -1);
                    if (code != 1) {
                        String msg = json.optString("msg");
                        callback.onPayFailed(msg);
                        return;
                    }

                    JSONObject data = json.getJSONObject("data");
                    int state = data.optInt("sdk_pay", 100);

                    if (state == 1) {

                        if (channelTag.equals(CHANNELZTAG)) {
                            map.put("url", data.optString("pay_web_url", ""));
                            isThirdPay = false;
                        } else {
                            isThirdPay = true;
                        }

                    } else {
                        map.put("url", data.optString("pay_web_url", ""));
                        isThirdPay = false;
                    }
                    map.put("order_id", data.optString("order_id", ""));
                    map.put("3rdext", data.optString("3rdext", ""));
                    map.put("query_params", data.optString("query_params", ""));
                    callback.onPaySuccess(map);

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

    }

    public static void getPhoneCaptcha(final Activity activity, String phone, String action, final UserPhoneLoginFragment.CaptchaCallback captchaCallback) {

        ProgressDialogUtils.show(activity, activity.getString(ResourceUtil.getStringId(activity, "hnyy_get_code")));

        HashMap<String, String> map = getCommonParams();


        map.put("phone", SignUtils.toURLEncoded(phone));
        map.put("action", SignUtils.toURLEncoded(action));


        String time = SignUtils.toURLEncoded(System.currentTimeMillis() / 1000 + "");
        String paramSign = SignUtils.paramSign(map, time);
        map.put("time", time);
        map.put("sign", paramSign);



        UrlHttpUtil.post(activity,GETCAPTCHA, map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {
                ProgressDialogUtils.dismiss();
                new TosUtil(activity, TosUtil.TOAST_ERROR, activity.getString(ResourceUtil.getStringId(activity, "hnyy_network_error_tip"))).show();

                captchaCallback.onGetFailed(activity.getString(ResourceUtil.getStringId(activity, "hnyy_network_error_tip")));

                YYLog.i(errorMessage);
            }

            @Override
            public void onResponse(String response) {

                ProgressDialogUtils.dismiss();


                JSONObject json = null;

                try {
                    json = new JSONObject(response);

                } catch (JSONException e) {
                    e.printStackTrace();
                    return;
                }
                int code = json.optInt("code", -1);
                if (code == 1) {
                    captchaCallback.onGetSuccess(json.optString("msg", "验证码发送成功"));
                } else {
                    captchaCallback.onGetFailed(json.optString("msg", "验证码发送失败"));
                }

            }
        });

    }

    public static void phoneLoginWithSms(final Activity activity, String phone,String code,final UserPhoneLoginFragment.PhoneLoginCallback phoneLoginCallback) {
        if (ProgressDialogUtils.isShowing()) {
            return;
        }

        HashMap<String, String> map = getCommonParams();



        map.put("sms", SignUtils.toURLEncoded(code));
        map.put("phone", SignUtils.toURLEncoded(phone));


        String time = SignUtils.toURLEncoded(System.currentTimeMillis() / 1000 + "");
        String paramSign = SignUtils.paramSign(map, time);
        map.put("time", time);
        map.put("sign", paramSign);



        UrlHttpUtil.post(activity,LOGINWITHSMS, map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {

                phoneLoginCallback.onPhoneLoginFailed(activity.getString(ResourceUtil.getStringId(activity, "hnyy_network_error_tip")));

                YYLog.i(errorMessage);
            }

            @Override
            public void onResponse(String response) {


                JSONObject json = null;
                JSONObject data = null;
                try {
                    json = new JSONObject(response);
                    data = json.getJSONObject("data");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                int code = json.optInt("code", -1);
                if (code != 1) {

                    YYLog.i("手机登录失败:" + json.optString("msg", "手机登录失败"));
                    phoneLoginCallback.onPhoneLoginFailed(json.optString("msg", "手机登录失败"));

                    return;
                }
                if (data.optInt("new_user") == 1){
                    phoneLoginCallback.onPhoneNewLogin();
                }else{
                    SDKSettings.loginData = LoginData.toBean(data);
                    YYLog.i("手机注册成功，正在登录游戏。+ token：" + loginData.token);
                    phoneLoginCallback.onPhoneLoginSuccess(loginData.token);

                }

            }
        });
    }



    public static void registerWithPhone(final Activity activity, String code, String userInfo, final UserPhoneLoginSetpwdFragment.PhoneRegisterCallback phoneRegisterCallback) {

        HashMap<String, String> map = getCommonParams();


        map.put("uname_pwd", userInfo);
        map.put("sms", SignUtils.toURLEncoded(code));
        map.put("smt_type", SignUtils.toURLEncoded(SDKSettings.isSimulator));


        String time = SignUtils.toURLEncoded(System.currentTimeMillis() / 1000 + "");
        String paramSign = SignUtils.paramSign(map, time);
        map.put("time", time);
        map.put("sign", paramSign);



        UrlHttpUtil.post(activity,REGISTERWITHPHONE, map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {

                phoneRegisterCallback.onRegisterFailed(activity.getString(ResourceUtil.getStringId(activity, "hnyy_network_error_tip")));
                YYLog.i(errorMessage);
            }

            @Override
            public void onResponse(String response) {



                JSONObject json = null;
                JSONObject data = null;
                try {
                    json = new JSONObject(response);
                    data = json.getJSONObject("data");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                int code = json.optInt("code", -1);
                if (code != 1) {

                    YYLog.i("手机注册失败:" + json.optString("msg", "手机注册失败"));
                    phoneRegisterCallback.onRegisterFailed(json.optString("msg", "手机注册失败"));
                    return;
                }

                SDKSettings.loginData = LoginData.toBean(data);


                YYLog.i("手机注册成功，正在登录游戏。+ token：" + loginData.token);
                phoneRegisterCallback.onRegisterSuccess(loginData.token);
            }
        });
    }

    /**
     * 支付回调通用扩展，目前仅应用宝需要用到
     */
    public static void payChannelNotify(final HashMap<String, String> payInfo, final String notifyUrl) {

        final HashMap<String, String> ntymap = new HashMap();

        for (Map.Entry<String, String> entry : payInfo.entrySet()) {


            ntymap.put(entry.getKey(), SignUtils.toURLEncoded(entry.getValue()));

        }
        String url = notifyUrl + "?" + SignUtils.encryptParam(ntymap);
        Message payMsg = new Message();
        payMsg.what = 1;
        HashMap<String, String> paydata = new HashMap<>();
        paydata.put("url", url);
        payMsg.obj = paydata;
        new PaymentNotify().sendMessage(payMsg);

    }

    public static void resetPwdWithPhone(Activity activity, String phone, String code, String pwd, final UserRetrievePwdFragment.ResetPwdCallback resetPwdCallback) {


        HashMap<String, String> map = getCommonParams();


        String uname_pwd = null;
        try {
            uname_pwd = SignUtils.encryptRsaByPublicKey(RSAKEY, phone + "|" + pwd + "|" + code);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        map.put("phone_pwd", uname_pwd);

        String time = SignUtils.toURLEncoded(System.currentTimeMillis() / 1000 + "");
        String paramSign = SignUtils.paramSign(map, time);
        map.put("time", time);
        map.put("sign", paramSign);



        UrlHttpUtil.post(activity,RESETPWDWITHPHONE, map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {


                YYLog.i(errorMessage);
            }

            @Override
            public void onResponse(String response) {



                JSONObject json = null;
                String msg = "";
                try {
                    json = new JSONObject(response);
                    msg = json.optString("msg");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                int code = json.optInt("code", -1);
                if (code != 1) {

                    resetPwdCallback.onResetFailed(msg);

                    return;
                }

                resetPwdCallback.onResetSuccess(msg);


            }
        });


    }

    public static void upLoadUserRealNameInfo(String report_type, String channelRealNameInfo) {
        HashMap<String, String> map = getCommonParams();


        map.put("channel_uid", SignUtils.toURLEncoded(loginData.channel_uid));
        map.put("uname", SignUtils.toURLEncoded(loginData.uname));
        map.put("report_type", report_type);
        map.put("sdk_params", channelRealNameInfo);


        String time = SignUtils.toURLEncoded(System.currentTimeMillis() / 1000 + "");
        String paramSign = SignUtils.paramSign(map, time);
        map.put("time", time);
        map.put("sign", paramSign);


        UrlHttpUtil.post(REALNAMERECORD, map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {


                YYLog.i(errorMessage);
            }

            @Override
            public void onResponse(String response) {


                ProgressDialogUtils.dismiss();

                JSONObject json = null;

                try {
                    json = new JSONObject(response);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                int code = json.optInt("code", -1);

                if (code == 1) {
                    String rn_bd = "";
                    try {
                        JSONObject data = json.getJSONObject("data");
                        rn_bd = data.optString("rn_bd", "");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    loginData.rn_bd = rn_bd;

                }


            }
        });

    }

    public static void recordUserRealNameInfo(Activity activity, String report_type, String channelRealNameInfo) {

        HashMap<String, String> map = getCommonParams();


        map.put("channel_uid", SignUtils.toURLEncoded(loginData.channel_uid));
        map.put("uname", SignUtils.toURLEncoded(loginData.uname));
        map.put("report_type", report_type);
        map.put("sdk_params", channelRealNameInfo);


        String time = SignUtils.toURLEncoded(System.currentTimeMillis() / 1000 + "");
        String paramSign = SignUtils.paramSign(map, time);
        map.put("time", time);
        map.put("sign", paramSign);



        UrlHttpUtil.post(REALNAMERECORD, map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {
                ProgressDialogUtils.dismiss();


                YYLog.i(errorMessage);
            }

            @Override
            public void onResponse(String response) {


                ProgressDialogUtils.dismiss();

                JSONObject json = null;
                String msg = "";
                try {
                    json = new JSONObject(response);
                    msg = json.optString("msg");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                int code = json.optInt("code", -1);
                if (code != 1) {

                    SDKEntry.getSdkInstance().sdkCallback.realNameMsgCallback(false, "");
                } else {
                    String rn_bd = "";
                    try {
                        JSONObject data = json.getJSONObject("data");
                        rn_bd = data.optString("rn_bd", "");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    loginData.rn_bd = rn_bd;
                    SDKEntry.getSdkInstance().sdkCallback.realNameMsgCallback(true, rn_bd);
                }


            }
        });


    }

    public static void queryOrder(String order_id, final HashMap<String, Object> payMap) {
        HashMap<String, String> map = getCommonParams();


        String query_params = (String) payMap.get("query_params");

        YYLog.i("begin query order,order_id:" + order_id + "----  and query_params:" + query_params);
        if (Utils.judgeStrNull(query_params)) {
            return;
        }


        map.put("order_id", SignUtils.toURLEncoded(order_id));
        map.put("package_name", SDKSettings.package_name);


        map.put("query_params", query_params);

        String time = SignUtils.toURLEncoded(System.currentTimeMillis() / 1000 + "");
        String paramSign = SignUtils.paramSign(map, time);
        map.put("time", time);
        map.put("sign", paramSign);



        UrlHttpUtil.post(QUERYORDER, map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {
                YYLog.i(errorMessage);
            }

            @Override
            public void onResponse(String response) {

                JSONObject json = null;
                JSONObject data = null;
                try {
                    json = new JSONObject(response);
                    data = json.getJSONObject("data");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                int code = json.optInt("code", -1);

                YYLog.i(response);
                if (code == 1) {

                    int orderStatus = data.optInt("order_status");

                    if (orderStatus == 1) {
                        SDKEntry.getSdkInstance().adSdkCallback.onPaySuccess(payMap);
                    }
                }


            }
        });

    }

    public static void initAgreement(Activity activity, final ShowPolicyCallback showPolicyCallback) {


        HashMap<String, String> map = new HashMap();

        String game_code = Utils.GetApplicationMetaData(activity, "yyrh_game_code");

        String gcp_code = Utils.getParamCnfValuebyKey(activity, "YyrhParam.cnf",
                "GCP_CODE");

        map.put("game_code", game_code);
        map.put("gcp_code", gcp_code);


        String time = SignUtils.toURLEncoded(System.currentTimeMillis() / 1000 + "");
        String paramSign = SignUtils.paramSign(map, time);

        map.put("time", time);
        map.put("sign", paramSign);



        UrlHttpUtil.post(AGREEMENTINIT, map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {

                showPolicyCallback.onError();
            }

            @Override
            public void onResponse(String response) {
                ProgressDialogUtils.dismiss();

                JSONObject json = null;
                JSONObject data = null;
                try {
                    json = new JSONObject(response);
                    data = json.getJSONObject("data");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                int code = json.optInt("code", -1);

                YYLog.i(response);
                if (code == 1) {

                    String agreement_url = data.optString("agreement_url");
                    if (Utils.judgeStrNull(agreement_url)) {
                        showPolicyCallback.onHide();
                    } else {
                        showPolicyCallback.onShow(agreement_url);
                    }


                }

            }
        });
    }

    private static HashMap<String, String> getCommonParams() {
        HashMap<String, String> map = new HashMap();

        map.put("game_code", SignUtils.toURLEncoded(SDKSettings.game_code));
        map.put("gcp_code", SignUtils.toURLEncoded(SDKSettings.gcp_code));
        map.put("cs_ver", SignUtils.toURLEncoded(SDKSettings.SDK_VERSION));
        map.put("system", SignUtils.toURLEncoded(SDKSettings.system));
        map.put("ifa", SignUtils.toURLEncoded(SDKSettings.imei));
        map.put("device_ver", SignUtils.toURLEncoded(SDKSettings.version));
        map.put("sp_code", SignUtils.toURLEncoded(SDKSettings.sp_code));

        map.put("device_model", SignUtils.toURLEncoded(SDKSettings.device_model));
        map.put("package_name", SDKSettings.package_name);
        map.put("ifa_type", SDKSettings.ifa_type);
        map.put("android_id", SDKSettings.android_id);

        if (!Utils.judgeStrNull(SDKSettings.o_cfg)) {
            map.put("o_cfg", SDKSettings.o_cfg);
            YYLog.i("o_cfg : " + SDKSettings.o_cfg);
        } else {
            YYLog.i("o_cfg is null");
        }

        return map;
    }

}
