package g.f.c.b.tea;

import android.util.Log;
import com.ifpdos.udi.sdk.service.sdk.UdiGlobal;
import com.ifpdos.udi.sdk.service.sdk.json.IConverter;
import com.ifpdos.udi.sdk.service.sdk.json.IJsonObject;
import com.ifpdos.udi.sdk.service.sdk.json.MissingFieldException;
import com.ifpdos.udi.sdk.service.sdk.log.IRequestTracker;
import com.ifpdos.udi.sdk.service.sdk.server.IRequest;
import com.ifpdos.udi.sdk.service.sdk.server.IUdiServer;
import com.ifpdos.udi.sdk.service.sdk.server.Response;
import com.ifpdos.udi.sdk.service.sdk.server.UdiException;
import com.ifpdos.udi.service.core.pc.UpgradeResultNotify;
import com.ifpdos.udi.service.core.pc.UpgradeStatusNotify;
import com.ifpdos.udi.service.tea.TeaDeviceHelper;
import com.ifpdos.udi.service.tea.UpgradeTeaBody;
import com.tencent.api.TEANativeInsideApi;
import g.a.a.a.a;
import g.f.c.b.d.pc.utils.FileUtil;
import g.f.c.b.d.pc.utils.PropertiesUtils;
import g.f.c.b.tea.TeaDeviceInfoUpgradeResultApi;
import g.h.a.i;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import kotlin.Metadata;
import kotlin.Pair;
import kotlin.io.f;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.e0;
import kotlin.jvm.internal.m;
import kotlin.jvm.internal.o;
import kotlin.reflect.KClass;
import kotlin.reflect.w.internal.y0.n.c2.c;
import kotlin.text.Charsets;
import kotlinx.serialization.json.internal.AbstractJsonLexerKt;
import s.coroutines.Dispatchers;
import s.coroutines.Job;

/* JADX INFO: loaded from: classes.dex */
@Metadata(k = 3, mv = {1, 6, 0}, xi = 48)
public final /* synthetic */ class s1 extends m implements Function1<IRequest, Response> {
    public s1(Object obj) {
        super(1, obj, upgradeTeaDeviceTeaServiceCoreAdapter.class, "upgradeTeaDevice", "upgradeTeaDevice(Lcom/ifpdos/udi/sdk/service/sdk/server/IRequest;)Lcom/ifpdos/udi/sdk/service/sdk/server/Response;", 0);
    }

    @Override // kotlin.jvm.functions.Function1
    public Response invoke(IRequest iRequest) throws IOException, UdiException {
        String str;
        Response.Companion companion;
        Response.ErrorBody errorBody;
        IJsonObject iJsonObjectA;
        IJsonObject iJsonObjectMerge;
        boolean z2;
        IRequest iRequest2 = iRequest;
        o.f(iRequest2, "p0");
        upgradeTeaDeviceTeaServiceCoreAdapter upgradeteadeviceteaservicecoreadapter = (upgradeTeaDeviceTeaServiceCoreAdapter) this.receiver;
        Objects.requireNonNull(upgradeteadeviceteaservicecoreadapter);
        o.f(iRequest2, "request");
        UdiCompileUtils udiCompileUtils = UdiCompileUtils.a;
        if (udiCompileUtils.c(iRequest2)) {
            Reader inputStreamReader = new InputStreamReader(iRequest2.getData(), Charsets.f9181b);
            BufferedReader bufferedReader = inputStreamReader instanceof BufferedReader ? (BufferedReader) inputStreamReader : new BufferedReader(inputStreamReader, 8192);
            try {
                String strC = f.c(bufferedReader);
                i.E(bufferedReader, null);
                str = strC;
            } catch (Throwable th) {
                try {
                    throw th;
                } catch (Throwable th2) {
                    i.E(bufferedReader, th);
                    throw th2;
                }
            }
        } else {
            str = null;
        }
        try {
            IJsonObject iJsonObjectG = udiCompileUtils.g(a.P("id", new Pair(udiCompileUtils.h(iRequest2.getUrl()).get(4), "String")));
            if (str != null) {
                try {
                    iJsonObjectA = udiCompileUtils.a(str);
                } catch (Throwable th3) {
                    o.f(iRequest2, "request");
                    o.f(th3, "throwable");
                    ((IRequestTracker) UdiGlobal.INSTANCE.get(e0.a(IRequestTracker.class))).onParseRequestFailed(iRequest2, null, str, th3);
                    companion = Response.INSTANCE;
                    errorBody = new Response.ErrorBody(a.t("params decode failed: ", str));
                    return companion.makeErrorResponse(400, errorBody);
                }
            } else {
                iJsonObjectA = null;
            }
            if (iJsonObjectA == null || (iJsonObjectMerge = iJsonObjectG.merge(iJsonObjectA)) == null) {
                iJsonObjectMerge = iJsonObjectG;
            }
            try {
                UpgradeTeaBody upgradeTeaBody = (UpgradeTeaBody) udiCompileUtils.b(iJsonObjectMerge, upgradeteadeviceteaservicecoreadapter.f4764b);
                UdiCompileUtils.f(udiCompileUtils, iRequest2, str, upgradeTeaBody, null, 8);
                TeaServiceCore teaServiceCore = upgradeteadeviceteaservicecoreadapter.a;
                String url = iRequest2.getUrl();
                Map<String, String> fields = iRequest2.getFields();
                o.f(upgradeTeaBody, "body");
                o.f(url, "uri");
                o.f(fields, "fields");
                Objects.requireNonNull(teaServiceCore);
                if (!PropertiesUtils.a.o()) {
                    throw new UdiException(Response.CODE_NOT_ALLOWED, new Response.ErrorBody("Not Support This Functional"));
                }
                TeaDeviceHelper teaDeviceHelper = TeaDeviceHelper.a;
                final String path = upgradeTeaBody.getPath();
                o.f(path, "filePath");
                if (TeaDeviceHelper.f1377k) {
                    z2 = false;
                } else {
                    TeaDeviceHelper.f1377k = true;
                    teaDeviceHelper.l();
                    ((ExecutorService) TeaDeviceHelper.f1370d.getValue()).execute(new Runnable() { // from class: g.f.c.b.i.i
                        @Override // java.lang.Runnable
                        public final void run() {
                            UdiGlobal udiGlobal;
                            String json;
                            KClass kClassA;
                            String str2 = path;
                            o.f(str2, "$filePath");
                            try {
                                String absolutePath = new File("/sdcard/temp/udi/tea_firmware/", new File(str2).getName()).getAbsolutePath();
                                o.e(absolutePath, "destPath");
                                String strA = FileUtil.a("upgradeDevice", str2, absolutePath);
                                if (strA == null) {
                                    TeaDeviceInfoUpgradeStatusApi teaDeviceInfoUpgradeStatusApi = TeaDeviceInfoUpgradeStatusApi.a;
                                    TeaDeviceInfoUpgradeStatusApi.a("tea_id", new UpgradeStatusNotify(1.0f));
                                    TeaDeviceInfoUpgradeResultApi teaDeviceInfoUpgradeResultApi = TeaDeviceInfoUpgradeResultApi.a;
                                    UpgradeResultNotify upgradeResultNotify = new UpgradeResultNotify(true, "file no exist or copy failed");
                                    o.f("tea_id", "id");
                                    o.f(upgradeResultNotify, "notify");
                                    TeaDeviceInfoUpgradeResultApi.a aVar = TeaDeviceInfoUpgradeResultApi.f4727b;
                                    o.f(aVar, "token");
                                    udiGlobal = UdiGlobal.INSTANCE;
                                    json = ((IConverter) udiGlobal.get(e0.a(IConverter.class))).toJson(upgradeResultNotify, aVar);
                                    kClassA = e0.a(IUdiServer.class);
                                } else {
                                    TeaDeviceHelper.f1371e = strA;
                                    TeaDeviceInfoUpgradeStatusApi teaDeviceInfoUpgradeStatusApi2 = TeaDeviceInfoUpgradeStatusApi.a;
                                    TeaDeviceInfoUpgradeStatusApi.a("tea_id", new UpgradeStatusNotify(0.1f));
                                    String strB = FileUtil.b(strA);
                                    if (strB != null) {
                                        TeaDeviceInfoUpgradeStatusApi.a("tea_id", new UpgradeStatusNotify(0.2f));
                                        Log.d("TeaDeviceHelper", "upgradePath: " + strA + "; md5 = [" + strB + AbstractJsonLexerKt.END_LIST);
                                        TEANativeInsideApi tEANativeInsideApi = TeaDeviceHelper.f1376j;
                                        if (tEANativeInsideApi != null) {
                                            TeaDeviceHelper.c cVar = TeaDeviceHelper.f1373g;
                                            tEANativeInsideApi.registerTeaDevEventCb(cVar);
                                            o.f(cVar, "listener");
                                            TeaDeviceHelper.f1378l.add(cVar);
                                            TEANativeInsideApi tEANativeInsideApi2 = TeaDeviceHelper.f1376j;
                                            if (tEANativeInsideApi2 != null) {
                                                Log.d("TeaDeviceHelper", "startDeviceOTA result = [" + tEANativeInsideApi2.startDeviceOTA(strA, strB) + AbstractJsonLexerKt.END_LIST);
                                                TeaDeviceInfoUpgradeStatusApi.a("tea_id", new UpgradeStatusNotify(0.3f));
                                                TeaDeviceHelper.f1375i = c.h0(c.b(Dispatchers.f9455b), null, null, new r1(null), 3, null);
                                                return;
                                            }
                                            o.n("teaNativeInsideApi");
                                        } else {
                                            o.n("teaNativeInsideApi");
                                        }
                                        throw null;
                                    }
                                    TeaDeviceInfoUpgradeStatusApi.a("tea_id", new UpgradeStatusNotify(1.0f));
                                    TeaDeviceInfoUpgradeResultApi teaDeviceInfoUpgradeResultApi2 = TeaDeviceInfoUpgradeResultApi.a;
                                    UpgradeResultNotify upgradeResultNotify2 = new UpgradeResultNotify(true, "file md5 failed");
                                    o.f("tea_id", "id");
                                    o.f(upgradeResultNotify2, "notify");
                                    TeaDeviceInfoUpgradeResultApi.a aVar2 = TeaDeviceInfoUpgradeResultApi.f4727b;
                                    o.f(aVar2, "token");
                                    udiGlobal = UdiGlobal.INSTANCE;
                                    json = ((IConverter) udiGlobal.get(e0.a(IConverter.class))).toJson(upgradeResultNotify2, aVar2);
                                    kClassA = e0.a(IUdiServer.class);
                                }
                                IUdiServer.DefaultImpls.sendEvent$default((IUdiServer) udiGlobal.get(kClassA), "v1/tea/ota/upgrade/result/tea_id", json, null, false, 12, null);
                            } catch (Exception e2) {
                                TeaDeviceHelper teaDeviceHelper2 = TeaDeviceHelper.a;
                                teaDeviceHelper2.h().unregisterTeaDevEventCb();
                                teaDeviceHelper2.n(true, "upgrade failed");
                                Job job = TeaDeviceHelper.f1375i;
                                if (job != null) {
                                    c.o(job, null, 1, null);
                                }
                                TeaDeviceHelper.f1375i = null;
                                teaDeviceHelper2.e();
                                TeaDeviceHelper.f1377k = false;
                                Log.e("TeaDeviceHelper", "upgradeDevice error", e2);
                            }
                        }
                    });
                    z2 = true;
                }
                if (z2) {
                    return Response.INSTANCE.makeOkResponse();
                }
                throw new UdiException(500, new Response.ErrorBody("Internal Server Error"));
            } catch (MissingFieldException e2) {
                UdiCompileUtils udiCompileUtils2 = UdiCompileUtils.a;
                UdiCompileUtils.e(udiCompileUtils2, iRequest2, str, e2, null, 8);
                return udiCompileUtils2.d(null, iJsonObjectG.toString(), str, e2.getMissingFields());
            } catch (Throwable th4) {
                o.f(iRequest2, "request");
                o.f(th4, "throwable");
                ((IRequestTracker) UdiGlobal.INSTANCE.get(e0.a(IRequestTracker.class))).onParseRequestFailed(iRequest2, null, str, th4);
                companion = Response.INSTANCE;
                errorBody = new Response.ErrorBody(a.t("params decode failed: ", str));
                return companion.makeErrorResponse(400, errorBody);
            }
        } catch (Exception e3) {
            o.f(iRequest2, "request");
            o.f(e3, "throwable");
            return Response.INSTANCE.makeErrorResponse(400, new Response.ErrorBody(a.t("params decode failed: ", a.j((IRequestTracker) UdiGlobal.INSTANCE.get(e0.a(IRequestTracker.class)), iRequest2, null, str, e3))));
        }
    }
}
