package com.yyrh.core.network;

import android.content.Context;
import android.text.TextUtils;

import com.yyrh.constant.RoleInfo;
import com.yyrh.core.SDKDrive;
import com.yyrh.core.entity.ResultInfo;
import com.yyrh.core.entity.SdkBackLoginInfo;
import com.yyrh.core.internal.IRequestCallback;
import com.yyrh.utils.ManifestUtils;
import com.yyrh.utils.YYLog;
import com.yythird.sdk.ChannelSDK;

import org.json.JSONObject;

import java.util.HashMap;

import cn.yyxx.support.AppUtils;

/**
 * @author #Suyghur.
 * Created on 2021/10/15
 */
public final class SdkRequest {

    private SdkRequest() {

    }

    public static void initSdk(Context context, IRequestCallback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("route_path", Host.BASIC_ROUTE_INIT_SDK);
        } catch (Exception e) {
            e.printStackTrace();
        }
        VolleyRequest.post(context, jsonObject, callback);
    }

    public static void accountRegister(Context context, String userName, String pwd, IRequestCallback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("uname_pwd", SDKDrive.getInstance().RsaEncrypt(userName + "|" + pwd));
            jsonObject.put("route_path", Host.BASIC_ROUTE_FAST_REGISTER);
        } catch (Exception e) {
            e.printStackTrace();
        }
        VolleyRequest.post(context, jsonObject, callback);
    }

    public static void accountLauncher(Context context, String userName, String pwd, IRequestCallback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("uname_pwd", SDKDrive.getInstance().RsaEncrypt(userName + "|" + pwd));
            jsonObject.put("route_path", Host.BASIC_ROUTE_USER_LOGIN);
        } catch (Exception e) {
            e.printStackTrace();
        }
        VolleyRequest.post(context, jsonObject, callback);
    }

    public static void phoneRegister(Context context, String phone, String pwd, String captcha, IRequestCallback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("uname_pwd", SDKDrive.getInstance().RsaEncrypt(phone + "|" + pwd));
            jsonObject.put("sms", captcha);
            jsonObject.put("route_path", Host.BASIC_ROUTE_PHONE_REGISTER);
        } catch (Exception e) {
            e.printStackTrace();
        }
        VolleyRequest.post(context, jsonObject, callback);
    }

    public static void getCaptcha(Context context, String action, String phone, IRequestCallback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("action", action);
            jsonObject.put("phone", phone);
            jsonObject.put("route_path", Host.BASIC_ROUTE_PHONE_CAPTCHA);
        } catch (Exception e) {
            e.printStackTrace();
        }
        VolleyRequest.post(context, jsonObject, callback);
    }


    public static void submitRoleInfo(Context context, String type, RoleInfo roleInfo, IRequestCallback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("report_role_type", type);
            jsonObject.put("uname", SdkBackLoginInfo.getInstance().userName);
            jsonObject.put("channel_uid", SdkBackLoginInfo.getInstance().userName);
            jsonObject.put("role_id", roleInfo.getRoleId());
            jsonObject.put("role_name", roleInfo.getRoleName());
            jsonObject.put("role_level", roleInfo.getRoleLevel());
            jsonObject.put("role_power", roleInfo.getRolePower());
            jsonObject.put("role_gold", roleInfo.getHasGold());
            jsonObject.put("server_id", roleInfo.getServerId());
            jsonObject.put("server_name", roleInfo.getServerName());
            jsonObject.put("party_id", roleInfo.getPartyId());
            jsonObject.put("party_name", roleInfo.getPartyName());
            jsonObject.put("vip_level", roleInfo.getVipLevel());
            jsonObject.put("role_charge", roleInfo.getCharge());
            jsonObject.put("role_ctime", roleInfo.getRoleCreateTime());
            jsonObject.put("route_path", Host.BASIC_ROUTE_REPORT_ROLE_INFO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        VolleyRequest.post(context, jsonObject, callback);
    }

    public static void createOrder(Context context, HashMap<String, Object> orderInfo, IRequestCallback callback) {
        JSONObject jsonObject = new JSONObject();

        String cpOrderId = (String) orderInfo.get("game_no");
        String money = (String) orderInfo.get("pay_money");
        String serverId = (String) orderInfo.get("server_id");
        String serverName = (String) orderInfo.get("server_name");
        String roleId = (String) orderInfo.get("role_id");
        String roleLevel = (String) orderInfo.get("role_level");
        String roleName = (String) orderInfo.get("role_name");
        String ext = (String) orderInfo.get("ext");
        String orderName = (String) orderInfo.get("order_name");
        String productId = (String) orderInfo.get("productId");
        String roleCTime = (String) orderInfo.get("role_ctime");

        if (TextUtils.isEmpty(cpOrderId) || TextUtils.isEmpty(money) || TextUtils.isEmpty(serverId) || TextUtils.isEmpty(serverName)
                || TextUtils.isEmpty(roleId) || TextUtils.isEmpty(roleName) || TextUtils.isEmpty(ext) || TextUtils.isEmpty(orderName)
                || TextUtils.isEmpty(productId) || TextUtils.isEmpty(roleCTime) || TextUtils.isEmpty(roleLevel)) {
            YYLog.e("下单参数有空值，请检查");
            callback.onResponse(new ResultInfo());
            return;
        }
        try {
            String channelOrderExt = ChannelSDK.getInstance().SDKGetOrderExt();
            if (!TextUtils.isEmpty(channelOrderExt)) {
                jsonObject.put("sdk_params", channelOrderExt);
            }
            String orderExt = ManifestUtils.getMetaDataValue(context, "yyrh_order_ext");
            if (!TextUtils.isEmpty(orderExt)) {
                jsonObject.put("order_ext", orderExt);
            }
            jsonObject.put("uname", SdkBackLoginInfo.getInstance().userName);
            jsonObject.put("channel_uid", SdkBackLoginInfo.getInstance().userId);
            jsonObject.put("cp_order_id", cpOrderId);
            jsonObject.put("money", money);
            jsonObject.put("product_id", productId);
            jsonObject.put("order_name", orderName);
            jsonObject.put("server_id", serverId);
            jsonObject.put("server_name", serverName);
            jsonObject.put("role_id",roleId);
            jsonObject.put("role_name",roleName);
            jsonObject.put("role_level",roleLevel);
            jsonObject.put("role_ctime",roleCTime);
            jsonObject.put("ext",ext);
            jsonObject.put("app_name", AppUtils.getAppName(context));
            jsonObject.put("route_path",Host.BASIC_ROUTE_RECHARGE_INIT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        VolleyRequest.post(context,jsonObject,callback);
    }
}
