package com.seewo.ota.u;

import android.net.Uri;
import android.util.Log;
import com.google.gson.Gson;
import com.seewo.ota.OTAApplication;
import com.seewo.ota.udi.model.FirmwareTypeEnum;
import com.seewo.ota.udi.model.Msg;
import com.seewo.ota.udi.model.UpgradeEvent;
import com.seewo.ota.udi.model.UpgradeInfo;
import com.seewo.ota.udi.model.UpsInformation;
import com.seewo.ota.udi.model.UpsInformationList;
import d.d.c.a.a;
import f.t;
import f.u.j;
import f.z.c.l;
import f.z.d.k;
import java.util.List;

/* JADX INFO: compiled from: UpsHelper.kt */
/* JADX INFO: loaded from: classes.dex */
public final class c {
    public static final c a = new c();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static final Gson f2274b = new Gson();

    /* JADX INFO: compiled from: UpsHelper.kt */
    public static final class a implements a.f {
        final /* synthetic */ l<Boolean, t> a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ String f2275b;

        /* JADX WARN: Multi-variable type inference failed */
        a(l<? super Boolean, t> lVar, String str) {
            this.a = lVar;
            this.f2275b = str;
        }

        @Override // d.d.c.a.a.f
        public void onEvent(String str, String str2) {
            Object objFromJson;
            k.e(str, "event");
            k.e(str2, "data");
            Log.i("UpsHelper", "upgradeUps: " + str + ", " + str2);
            try {
                objFromJson = c.f2274b.fromJson(str2, (Class<Object>) UpgradeEvent.class);
            } catch (Exception e2) {
                Log.e("GsonExt", "fromJsonOrNull: " + e2.getMessage() + '\n' + str2);
                objFromJson = null;
            }
            UpgradeEvent upgradeEvent = (UpgradeEvent) objFromJson;
            if (upgradeEvent != null) {
                l<Boolean, t> lVar = this.a;
                String str3 = this.f2275b;
                if (!k.a(upgradeEvent.getCode(), "200")) {
                    lVar.c(Boolean.FALSE);
                    com.seewo.ota.u.a.a().f(j.b(str3), this);
                } else if (upgradeEvent.getStatus().getFinished()) {
                    lVar.c(Boolean.TRUE);
                    com.seewo.ota.u.a.a().f(j.b(str3), this);
                }
            }
        }
    }

    static {
        com.seewo.ota.u.a.a().b(OTAApplication.f1907b);
    }

    private c() {
    }

    public static /* synthetic */ List c(c cVar, FirmwareTypeEnum firmwareTypeEnum, boolean z, int i2, long j2, int i3, Object obj) {
        if ((i3 & 1) != 0) {
            firmwareTypeEnum = null;
        }
        if ((i3 & 2) != 0) {
            z = false;
        }
        boolean z2 = z;
        if ((i3 & 4) != 0) {
            i2 = 6;
        }
        int i4 = i2;
        if ((i3 & 8) != 0) {
            j2 = 5000;
        }
        return cVar.b(firmwareTypeEnum, z2, i4, j2);
    }

    public final List<UpsInformation> b(FirmwareTypeEnum firmwareTypeEnum, boolean z, int i2, long j2) throws InterruptedException {
        Object objFromJson;
        List<UpsInformation> information;
        if (i2 <= 0) {
            Log.e("UpsHelper", "getUpsInformation: tryCount=" + i2);
            return f.u.k.d();
        }
        Uri.Builder builder = new Uri.Builder();
        builder.path("/v1/ups/internal/information");
        if (z) {
            builder.appendQueryParameter("force", String.valueOf(z));
        }
        if (firmwareTypeEnum != null) {
            builder.appendQueryParameter("type", firmwareTypeEnum.getType());
        }
        String strC = com.seewo.ota.u.a.a().c(builder.build().toString());
        if (strC == null) {
            strC = "";
        }
        String str = strC;
        Object objFromJson2 = null;
        try {
            objFromJson = f2274b.fromJson(str, (Class<Object>) Msg.class);
        } catch (Exception e2) {
            Log.e("GsonExt", "fromJsonOrNull: " + e2.getMessage() + '\n' + str);
            objFromJson = null;
        }
        Msg msg = (Msg) objFromJson;
        if (msg == null) {
            Log.e("UpsHelper", "getUpsInformation: " + str + ", msg is null, tryCount=" + i2);
            Thread.sleep(j2);
            return c(this, firmwareTypeEnum, z, i2 - 1, 0L, 8, null);
        }
        Log.e("UpsHelper", "getUpsInformation: " + msg.getMsg());
        if (k.a(msg.getMsg(), "target unreachable")) {
            Thread.sleep(j2);
            return c(this, firmwareTypeEnum, z, i2 - 1, 0L, 8, null);
        }
        try {
            objFromJson2 = f2274b.fromJson(str, (Class<Object>) UpsInformationList.class);
        } catch (Exception e3) {
            Log.e("GsonExt", "fromJsonOrNull: " + e3.getMessage() + '\n' + str);
        }
        UpsInformationList upsInformationList = (UpsInformationList) objFromJson2;
        Log.i("UpsHelper", "getUpsInformation: " + upsInformationList);
        return (upsInformationList == null || (information = upsInformationList.getInformation()) == null) ? f.u.k.d() : information;
    }

    public final boolean d(UpsInformation upsInformation, UpgradeInfo upgradeInfo, l<? super Boolean, t> lVar) {
        k.e(upsInformation, "information");
        k.e(upgradeInfo, "upgradeInfo");
        k.e(lVar, "onSuccess");
        String str = "v1/ups/internal/upgrade/" + upsInformation.getOtaKey() + '/' + upsInformation.getId();
        com.seewo.ota.u.a.a().e(j.b(str), new a(lVar, str));
        return com.seewo.ota.u.a.a().d(str, f2274b.toJson(upgradeInfo));
    }
}
