package com.appsflyer.internal;

import android.content.Context;
import android.content.Intent;
import com.adjust.sdk.Constants;
import com.appsflyer.AFInAppEventType;
import com.appsflyer.AFLogger;
import com.appsflyer.AppsFlyerInAppPurchaseValidatorListener;
import com.appsflyer.AppsFlyerLib;
import com.appsflyer.AppsFlyerLibCore;
import com.appsflyer.AppsFlyerProperties;
import com.appsflyer.ServerConfigHandler;
import com.appsflyer.ServerParameters;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.json.JSONException;
import org.json.JSONObject;

public final class y implements Runnable {

    /* renamed from: ʻ  reason: contains not printable characters */
    public Map<String, String> f2150;

    /* renamed from: ʼ  reason: contains not printable characters */
    public String f2151;

    /* renamed from: ʽ  reason: contains not printable characters */
    public String f2152;

    /* renamed from: ˊ  reason: contains not printable characters */
    public final Intent f2153;

    /* renamed from: ˋ  reason: contains not printable characters */
    public String f2154;

    /* renamed from: ˎ  reason: contains not printable characters */
    public String f2155;

    /* renamed from: ˏ  reason: contains not printable characters */
    public WeakReference<Context> f2156;

    /* renamed from: ॱ  reason: contains not printable characters */
    public String f2157;

    /* renamed from: ॱॱ  reason: contains not printable characters */
    public String f2158;

    public y(Context context, String str, String str2, String str3, String str4, String str5, String str6, Map<String, String> map, Intent intent) {
        this.f2156 = new WeakReference<>(context);
        this.f2155 = str;
        this.f2152 = str2;
        this.f2158 = str4;
        this.f2154 = str5;
        this.f2157 = str6;
        this.f2150 = map;
        this.f2151 = str3;
        this.f2153 = intent;
    }

    /* renamed from: ˊ  reason: contains not printable characters */
    public static HttpURLConnection m1252(String str, String str2) throws IOException {
        try {
            ab abVar = new ab(null, AppsFlyerLib.getInstance().isTrackingStopped());
            abVar.f1965 = str;
            abVar.f1967 = false;
            AFLogger.afDebugLog("Calling ".concat(String.valueOf(str2)));
            return abVar.m1163(str2);
        } catch (Throwable th) {
            AFLogger.afErrorLog("Could not send callStats request", th);
            return null;
        }
    }

    /* renamed from: ˋ  reason: contains not printable characters */
    public static void m1253(boolean z, String str, String str2, String str3, String str4) {
        if (AppsFlyerLibCore.f1871 != null) {
            StringBuilder sb = new StringBuilder("Validate callback parameters: ");
            sb.append(str);
            sb.append(" ");
            sb.append(str2);
            sb.append(" ");
            sb.append(str3);
            AFLogger.afDebugLog(sb.toString());
            if (z) {
                AFLogger.afDebugLog("Validate in app purchase success: ".concat(String.valueOf(str4)));
                AppsFlyerLibCore.f1871.onValidateInApp();
                return;
            }
            AFLogger.afDebugLog("Validate in app purchase failed: ".concat(String.valueOf(str4)));
            AppsFlyerInAppPurchaseValidatorListener appsFlyerInAppPurchaseValidatorListener = AppsFlyerLibCore.f1871;
            if (str4 == null) {
                str4 = "Failed validating";
            }
            appsFlyerInAppPurchaseValidatorListener.onValidateInAppFailure(str4);
        }
    }

    public final void run() {
        String str = this.f2155;
        if (str != null && str.length() != 0 && !AppsFlyerLib.getInstance().isTrackingStopped()) {
            HttpURLConnection httpURLConnection = null;
            try {
                Context context = this.f2156.get();
                if (context != null) {
                    HashMap hashMap = new HashMap();
                    hashMap.put("public-key", this.f2152);
                    hashMap.put("sig-data", this.f2158);
                    hashMap.put("signature", this.f2151);
                    final HashMap hashMap2 = new HashMap();
                    hashMap2.putAll(hashMap);
                    Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() {
                        /* class com.appsflyer.internal.y.AnonymousClass4 */

                        public final void run() {
                            y yVar = y.this;
                            Map map = hashMap2;
                            Map<String, String> map2 = yVar.f2150;
                            WeakReference<Context> weakReference = yVar.f2156;
                            if (weakReference.get() != null) {
                                StringBuilder sb = new StringBuilder();
                                sb.append(ServerConfigHandler.getUrl("https://%svalidate.%s/api/v4.11/androidevent?buildnumber=5.0.0&app_id="));
                                sb.append(weakReference.get().getPackageName());
                                String obj = sb.toString();
                                HttpURLConnection httpURLConnection = null;
                                String string = AppsFlyerLibCore.m1100(weakReference.get()).getString(Constants.REFERRER, null);
                                if (string == null) {
                                    string = "";
                                }
                                AppsFlyerLibCore instance = AppsFlyerLibCore.getInstance();
                                j jVar = new j();
                                jVar.f2090 = weakReference.get();
                                jVar.f2085 = yVar.f2155;
                                jVar.f2089 = AFInAppEventType.PURCHASE;
                                jVar.f2094 = string;
                                jVar.f2096 = yVar.f2153;
                                Map<String, Object> r3 = instance.m1145(jVar);
                                r3.put("price", yVar.f2154);
                                r3.put("currency", yVar.f2157);
                                JSONObject jSONObject = new JSONObject(r3);
                                JSONObject jSONObject2 = new JSONObject();
                                try {
                                    for (Map.Entry entry : map.entrySet()) {
                                        jSONObject2.put((String) entry.getKey(), entry.getValue());
                                    }
                                    jSONObject.put("receipt_data", jSONObject2);
                                } catch (JSONException e) {
                                    AFLogger.afErrorLog("Failed to build 'receipt_data'", e);
                                }
                                if (map2 != null) {
                                    JSONObject jSONObject3 = new JSONObject();
                                    try {
                                        for (Map.Entry<String, String> entry2 : map2.entrySet()) {
                                            jSONObject3.put(entry2.getKey(), entry2.getValue());
                                        }
                                        jSONObject.put("extra_prms", jSONObject3);
                                    } catch (JSONException e2) {
                                        AFLogger.afErrorLog("Failed to build 'extra_prms'", e2);
                                    }
                                }
                                String jSONObject4 = jSONObject.toString();
                                if (ae.f1979 == null) {
                                    ae.f1979 = new ae();
                                }
                                ae.f1979.m1180("server_request", obj, jSONObject4);
                                try {
                                    HttpURLConnection r6 = y.m1252(jSONObject4, obj);
                                    int i = -1;
                                    if (r6 != null) {
                                        i = r6.getResponseCode();
                                    }
                                    AppsFlyerLibCore.getInstance();
                                    String r1 = AppsFlyerLibCore.m1128(r6);
                                    if (ae.f1979 == null) {
                                        ae.f1979 = new ae();
                                    }
                                    ae.f1979.m1180("server_response", obj, String.valueOf(i), r1);
                                    JSONObject jSONObject5 = new JSONObject(r1);
                                    if (i == 200) {
                                        StringBuilder sb2 = new StringBuilder("Validate-WH response - 200: ");
                                        sb2.append(jSONObject5.toString());
                                        AFLogger.afInfoLog(sb2.toString());
                                    } else {
                                        StringBuilder sb3 = new StringBuilder("Validate-WH response failed - ");
                                        sb3.append(i);
                                        sb3.append(": ");
                                        sb3.append(jSONObject5.toString());
                                        AFLogger.afWarnLog(sb3.toString());
                                    }
                                    r6.disconnect();
                                } catch (Throwable th) {
                                    if (0 != 0) {
                                        httpURLConnection.disconnect();
                                    }
                                    throw th;
                                }
                            }
                        }
                    }, 5, TimeUnit.MILLISECONDS);
                    hashMap.put("dev_key", this.f2155);
                    hashMap.put("app_id", context.getPackageName());
                    hashMap.put("uid", AppsFlyerLib.getInstance().getAppsFlyerUID(context));
                    hashMap.put(ServerParameters.ADVERTISING_ID_PARAM, AppsFlyerProperties.getInstance().getString(ServerParameters.ADVERTISING_ID_PARAM));
                    String jSONObject = new JSONObject(hashMap).toString();
                    String url = ServerConfigHandler.getUrl("https://%ssdk-services.%s/validate-android-signature");
                    if (ae.f1979 == null) {
                        ae.f1979 = new ae();
                    }
                    ae.f1979.m1180("server_request", url, jSONObject);
                    HttpURLConnection r2 = m1252(jSONObject, url);
                    int i = -1;
                    if (r2 != null) {
                        i = r2.getResponseCode();
                    }
                    AppsFlyerLibCore.getInstance();
                    String r5 = AppsFlyerLibCore.m1128(r2);
                    if (ae.f1979 == null) {
                        ae.f1979 = new ae();
                    }
                    ae.f1979.m1180("server_response", url, String.valueOf(i), r5);
                    JSONObject jSONObject2 = new JSONObject(r5);
                    jSONObject2.put("code", i);
                    if (i == 200) {
                        StringBuilder sb = new StringBuilder("Validate response 200 ok: ");
                        sb.append(jSONObject2.toString());
                        AFLogger.afInfoLog(sb.toString());
                        m1253(jSONObject2.optBoolean("result") ? jSONObject2.getBoolean("result") : false, this.f2158, this.f2154, this.f2157, jSONObject2.toString());
                    } else {
                        AFLogger.afInfoLog("Failed Validate request");
                        m1253(false, this.f2158, this.f2154, this.f2157, jSONObject2.toString());
                    }
                    r2.disconnect();
                }
            } catch (Throwable th) {
                if (0 != 0) {
                    httpURLConnection.disconnect();
                }
                throw th;
            }
        }
    }
}
