package com.yyrh.behavior;

import com.yyrh.constant.SDKSettings;
import com.yyrh.networking.urlhttp.CallBackUtil;
import com.yyrh.networking.urlhttp.UrlHttpUtil;
import com.yyrh.utils.SignUtils;
import com.yyrh.utils.YYLog;


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


import static com.yyrh.constant.URLConstants.ACTION_LOG_URL;

import static com.yyrh.constant.URLConstants.SDK_AD_LOG;
import static com.yyrh.constant.URLConstants.SDK_ERROR_LOG;
import static com.yyrh.constant.URLConstants.SDK_LOG;

public class ReportLogUtil {


    public static void reportNormalLog(String action,String reqName,String reqParam){



        String timestamp = System.currentTimeMillis()/1000  + "";




        JSONObject json = new JSONObject();
        try {
            json.put("action",action);
            json.put("cs_ver",SDKSettings.SDK_VERSION);
            json.put("device_model",SDKSettings.device_model);
            json.put("device_ver",SDKSettings.version);

            json.put("game_code",SDKSettings.game_code);
            json.put("gcp_code",SDKSettings.gcp_code);
            json.put("ifa",SDKSettings.imei);
            json.put("log_type",SDK_LOG);

            json.put("package_name",SDKSettings.package_name);
            json.put("req_name",reqName);
            json.put("req_param",reqParam);
            json.put("system",SDKSettings.system);

            json.put("timestamp",timestamp);


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

        String sign = SignUtils.sdkLogParamSign(timestamp,json.toString());



        String url =  ACTION_LOG_URL + "timestamp="  + timestamp +"&sign="+sign;



        UrlHttpUtil.postJson(url,json.toString(), new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {
                YYLog.i(errorMessage);
            }

            @Override
            public void onResponse(String response) {
                YYLog.i(response);
            }
        });
    }

    public static void reportErrorLog(String action,String reqName,String reqParam){

        String timestamp = System.currentTimeMillis()/1000  + "";




        JSONObject json = new JSONObject();
        try {
            json.put("action",action);
            json.put("cs_ver",SDKSettings.SDK_VERSION);
            json.put("device_model",SDKSettings.device_model);
            json.put("device_ver",SDKSettings.version);

            json.put("game_code",SDKSettings.game_code);
            json.put("gcp_code",SDKSettings.gcp_code);
            json.put("ifa",SDKSettings.imei);
            json.put("log_type",SDK_ERROR_LOG);

            json.put("package_name",SDKSettings.package_name);
            json.put("req_name",reqName);
            json.put("req_param",reqParam);
            json.put("system",SDKSettings.system);

            json.put("timestamp",timestamp);


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

        String sign = SignUtils.sdkLogParamSign(timestamp,json.toString());



        String url =  ACTION_LOG_URL + "timestamp="  + timestamp +"&sign="+sign;







        UrlHttpUtil.postJson(url,json.toString(), new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {
                YYLog.i(errorMessage);
            }

            @Override
            public void onResponse(String response) {
                YYLog.i(response);
            }
        });
    }

    public static void reportAdLog(String action,String adParam){



        String timestamp = System.currentTimeMillis()/1000  + "";




        JSONObject json = new JSONObject();
        try {
            json.put("action",action);
            json.put("ad_param",adParam);
            json.put("cs_ver",SDKSettings.SDK_VERSION);
            json.put("device_model",SDKSettings.device_model);
            json.put("device_ver",SDKSettings.version);

            json.put("game_code",SDKSettings.game_code);
            json.put("gcp_code",SDKSettings.gcp_code);
            json.put("ifa",SDKSettings.imei);
            json.put("log_type",SDK_AD_LOG);

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

            json.put("system",SDKSettings.system);

            json.put("timestamp",timestamp);

            json.put("yyrh_sp_code",SDKSettings.sp_code);


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

        String sign = SignUtils.sdkLogParamSign(timestamp,json.toString());



        String url =  ACTION_LOG_URL + "timestamp="  + timestamp +"&sign="+sign;





        UrlHttpUtil.postJson(url,json.toString(), new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {
                YYLog.i(errorMessage);
            }

            @Override
            public void onResponse(String response) {
                YYLog.i(response);
            }
        });


    }


}
