package cn.yyxx.commsdk.core.network;

import static cn.yyxx.commsdk.core.network.Hosts.HOST;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

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

import cn.yyxx.commsdk.base.entity.SdkChargeInfo;
import cn.yyxx.commsdk.base.entity.SdkRoleInfo;
import cn.yyxx.commsdk.base.internal.ICallback;
import cn.yyxx.commsdk.base.internal.IRequestCallback;

import cn.yyxx.commsdk.base.utils.Logger;
import cn.yyxx.commsdk.core.SdkDrive;
import cn.yyxx.commsdk.core.common.CommonOperationManager;
import cn.yyxx.commsdk.core.entity.SdkLoginInfo;
import cn.yyxx.commsdk.core.utils.ManifestUtils;
import cn.yyxx.commsdk.core.utils.SignUtils;
import cn.yyxx.support.AppUtils;
import cn.yyxx.support.PropertiesUtils;
import cn.yyxx.support.device.DeviceInfoUtils;
import cn.yyxx.support.encryption.aes.AesUtils;
import cn.yyxx.support.encryption.rsa.RsaUtils;

public class SdkRequest {
    private static volatile SdkRequest mInstance;

    private SdkRequest() {
    }

    public static SdkRequest getInstance() {
        if (mInstance == null) {
            synchronized (SdkRequest.class) {
                mInstance = new SdkRequest();
            }
        }
        return mInstance;
    }

    public void initAgreement(Context context, IRequestCallback callback) {
        String game_code = PropertiesUtils.getValue4Properties(context, "yyxx_cfg.properties", "yyxx_game", "YYXX_GAME_CODE");
        String gcp_code = ManifestUtils.getMetaDataValue(context, "gcp_code");
        JSONObject json = new JSONObject();
        try {
            json.put("game_code", game_code);
            json.put("gcp_code", gcp_code);
            json.put("route_path", Hosts.BASIC_URL_INIT_AGREEMENT);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        String paramText = SdkDrive.getInstance().invokeJobWithoutParams(json.toString());
        VolleyRequest.post(context, HOST, paramText, callback);
    }

    public void initSdk(Context context, IRequestCallback callback) {

        JSONObject ifa_info = CommonOperationManager.getInstance().getIfa(context);

        try {
            ifa_info.put("route_path", Hosts.BASIC_URL_INIT_SDK);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String paramText = SdkDrive.getInstance().invokeJob(ifa_info.toString());
        VolleyRequest.post(context, HOST, paramText, callback);
    }

    public void reigsterAccount(Context context, String account, String password, IRequestCallback callback) {

        JSONObject json = new JSONObject();

        String uname_pwd = SdkDrive.getInstance().invokeEncryptRsa(account + "|" + password);

        try {
            json.put("route_path", Hosts.BASIC_URL_FAST_REGISTER);
            json.put("smt_type", CommonOperationManager.initBean.isEmulator);
            json.put("uname_pwd", uname_pwd);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        String paramText = SdkDrive.getInstance().invokeJob(json.toString());

        VolleyRequest.post(context, HOST, paramText, callback);
    }

    public void accountLogin(Context context, String account, String password, String login_token, IRequestCallback callback) {

        JSONObject json = new JSONObject();

        try {
            json.put("route_path", Hosts.BASIC_URL_USER_LOGIN);
            json.put("smt_type", CommonOperationManager.initBean.isEmulator);

            if (TextUtils.isEmpty(password)) {

                json.put("login_token", login_token);

            } else {

                String uname_pwd = SdkDrive.getInstance().invokeEncryptRsa(account + "|" + password);
                json.put("uname_pwd", uname_pwd);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


        String paramText = SdkDrive.getInstance().invokeJob(json.toString());

        VolleyRequest.post(context, HOST, paramText, callback);
    }

    public void getPhoneCaptcha(Context context, String phoneNum, String action, IRequestCallback callback) {
        JSONObject json = new JSONObject();

        try {
            json.put("route_path", Hosts.BASIC_URL_GET_CAPTCHA);
            json.put("phone", phoneNum);
            json.put("action", action);

        } catch (JSONException e) {
            e.printStackTrace();
        }


        String paramText = SdkDrive.getInstance().invokeJob(json.toString());

        VolleyRequest.post(context, HOST, paramText, callback);
    }


    public void phoneLoginWithSms(Context context, String phoneNum, String code, IRequestCallback callback) {
        JSONObject json = new JSONObject();

        try {
            json.put("route_path", Hosts.BASIC_URL_SMS_LOGIN);
            json.put("phone", phoneNum);
            json.put("sms", code);

        } catch (JSONException e) {
            e.printStackTrace();
        }


        String paramText = SdkDrive.getInstance().invokeJob(json.toString());

        VolleyRequest.post(context, HOST, paramText, callback);
    }

    public void setPassword(Context context, String account, String password, String code, IRequestCallback callback) {

        JSONObject json = new JSONObject();

        String uname_pwd = SdkDrive.getInstance().invokeEncryptRsa(account + "|" + password);

        try {
            json.put("route_path", Hosts.BASIC_URL_PHONE_REGISTER);
            json.put("smt_type", CommonOperationManager.initBean.isEmulator);
            json.put("sms", code);
            json.put("uname_pwd", uname_pwd);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        String paramText = SdkDrive.getInstance().invokeJob(json.toString());

        VolleyRequest.post(context, HOST, paramText, callback);

    }

    public void resetPassword(Context context, String phoneNum, String password, String code, IRequestCallback callback) {

        JSONObject json = new JSONObject();

        String phone_pwd = SdkDrive.getInstance().invokeEncryptRsa(phoneNum + "|" + password + "|" + code);

        try {
            json.put("route_path", Hosts.BASIC_URL_PHONE_RESETPWD);
            json.put("phone_pwd", phone_pwd);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        String paramText = SdkDrive.getInstance().invokeJob(json.toString());

        VolleyRequest.post(context, HOST, paramText, callback);

    }


    public void uploadRoleInfo(Context context, String type, SdkRoleInfo roleInfo, IRequestCallback callback) {

        JSONObject json = new JSONObject();

        try {
            json.put("route_path", Hosts.BASIC_URL_UPLOAD_ROLEINFO);
            json.put("report_role_type", type);

            json.put("uname", SdkLoginInfo.getInstance().uname);
            json.put("channel_uid", SdkLoginInfo.getInstance().channel_uid);
            json.put("party_id", roleInfo.getPartyId());
            json.put("party_name", roleInfo.getPartyName());
            json.put("server_id", roleInfo.getServerId());
            json.put("server_name", roleInfo.getServerName());
            json.put("role_id", roleInfo.getRoleId());
            json.put("role_name", roleInfo.getRoleName());
            json.put("role_level", roleInfo.getRoleLevel());
            json.put("vip_level", roleInfo.getVipLevel());
            json.put("role_power", roleInfo.getVipLevel());
            json.put("role_gold", roleInfo.getHasGold());
            json.put("role_ctime", roleInfo.getRoleCreateTime());

            json.put("role_charge", roleInfo.getCharge());


        } catch (JSONException e) {
            e.printStackTrace();
        }


        String paramText = SdkDrive.getInstance().invokeJob(json.toString());

        VolleyRequest.post(context, HOST, paramText, callback);

    }


    public void charge(Context context, SdkChargeInfo chargeInfo, IRequestCallback callback) {

        JSONObject json = new JSONObject();


        try {
            json.put("route_path", Hosts.BASIC_URL_RECHARGE_INIT);
            json.put("uname", SdkLoginInfo.getInstance().uname);
            json.put("channel_uid", SdkLoginInfo.getInstance().channel_uid);
            json.put("cp_order_id", chargeInfo.getGame_no());
            json.put("money", chargeInfo.getPay_money());
            json.put("server_id", chargeInfo.getServer_id());
            json.put("server_name", chargeInfo.getServer_name());
            json.put("role_id", chargeInfo.getRole_id());
            json.put("role_name", chargeInfo.getRole_name());
            json.put("role_level", chargeInfo.getRole_level());
            json.put("ext", chargeInfo.getExt());
            json.put("order_name", chargeInfo.getOrder_name());
            json.put("app_name", AppUtils.getAppName(context));

            json.put("role_ctime", chargeInfo.getRoleCreateTime());
            json.put("product_id", chargeInfo.getProduct_id());
            if (!TextUtils.isEmpty(chargeInfo.getSdk_params())){
                json.put("sdk_params", chargeInfo.getSdk_params());
            }
            
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String paramText = SdkDrive.getInstance().invokeJob(json.toString());

        VolleyRequest.post(context, HOST, paramText, callback);
    }

    public void queryOrder(Context context, String order_id,String query_params, IRequestCallback callback) {
        JSONObject json = new JSONObject();
        try {
            json.put("order_id",order_id);
            json.put("query_params",query_params);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String paramText = SdkDrive.getInstance().invokeJob(json.toString());

        VolleyRequest.post(context, HOST, paramText, callback);

    }
}













