package com.cvte.dss.ups.camera;

import android.content.Context;
import android.util.Log;
import com.cvte.dss.ups.camera.model.CameraUpgradeInfo;
import com.cvte.dss.ups.camera.utils.CameraHelper;
import com.cvte.dss.ups.camera.utils.CommonUtils;
import com.cvte.dss.ups.plugin.IUpsPlugin;
import d.b.a.a.a;
import g.h0.c.g;
import g.h0.c.l;
import g.p;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;

/* JADX INFO: compiled from: Plugin.kt */
/* JADX INFO: loaded from: classes.dex */
public final class Plugin implements IUpsPlugin {
    public static final Companion Companion = new Companion(null);
    public static final String TAG = "CameraPlugin";

    /* JADX INFO: compiled from: Plugin.kt */
    public static final class Companion {
        private Companion() {
        }

        public /* synthetic */ Companion(g gVar) {
            this();
        }
    }

    @Override // com.cvte.dss.ups.plugin.IUpsPlugin
    public IUpsPlugin.FirmwareInfo getFirmwareInfo(Context context) {
        l.g(context, "context");
        CameraHdiApi cameraHdiApiFindDevice = CameraHelper.INSTANCE.findDevice(context);
        if (cameraHdiApiFindDevice != null) {
            CameraManager cameraManager = new CameraManager(cameraHdiApiFindDevice);
            return new IUpsPlugin.FirmwareInfo("camera", "imx586", "camera", cameraManager.getVersion(), cameraManager.getOTAKey(), false, false);
        }
        Log.e(TAG, "can't find camera device");
        return null;
    }

    @Override // com.cvte.dss.ups.plugin.IUpsPlugin
    public List<IUpsPlugin.FirmwareInfo> getFirmwareInfos(Context context) throws ExecutionException, InterruptedException {
        l.g(context, "context");
        List listFindDevices$default = CameraHelper.findDevices$default(CameraHelper.INSTANCE, context, null, 2, null);
        StringBuilder sbB = a.B("getFirmwareInfos; count = ");
        sbB.append(listFindDevices$default.size());
        Log.i(TAG, sbB.toString());
        final ArrayList arrayList = new ArrayList(f.a.y.a.C(listFindDevices$default, 10));
        Iterator it = listFindDevices$default.iterator();
        while (it.hasNext()) {
            arrayList.add(((CameraHdiApi) it.next()).m0getCameraInfo());
        }
        Object[] array = arrayList.toArray(new CompletableFuture[0]);
        if (array == null) {
            throw new p("null cannot be cast to non-null type kotlin.Array<T>");
        }
        CompletableFuture[] completableFutureArr = (CompletableFuture[]) array;
        Object obj = CompletableFuture.allOf((CompletableFuture[]) Arrays.copyOf(completableFutureArr, completableFutureArr.length)).thenApply((Function<? super Void, ? extends U>) new Function<T, U>() { // from class: com.cvte.dss.ups.camera.Plugin.getFirmwareInfos.1
            @Override // java.util.function.Function
            public final List<IUpsPlugin.FirmwareInfo> apply(Void r3) {
                List list = arrayList;
                ArrayList arrayList2 = new ArrayList(f.a.y.a.C(list, 10));
                Iterator<T> it2 = list.iterator();
                while (it2.hasNext()) {
                    arrayList2.add((IUpsPlugin.FirmwareInfo) ((CompletableFuture) it2.next()).get());
                }
                return arrayList2;
            }
        }).get();
        l.b(obj, "CompletableFuture.allOf(…t() }\n            }.get()");
        return (List) obj;
    }

    public List<IUpsPlugin.FirmwareInfo> getFirmwareInfosWithOptions(Context context, String str) {
        l.g(context, "context");
        l.g(str, "options");
        return IUpsPlugin.DefaultImpls.getFirmwareInfosWithOptions(this, context, str);
    }

    public boolean upgradeFirmware(Context context, String str, String str2, boolean z, String str3, IUpsPlugin.IUpgradeStatusListener iUpgradeStatusListener) {
        l.g(context, "context");
        l.g(str, "id");
        l.g(str2, "path");
        l.g(str3, "options");
        l.g(iUpgradeStatusListener, "listener");
        return IUpsPlugin.DefaultImpls.upgradeFirmware(this, context, str, str2, z, str3, iUpgradeStatusListener);
    }

    @Override // com.cvte.dss.ups.plugin.IUpsPlugin
    public boolean upgradeFirmware(final Context context, String str, String str2, boolean z, final IUpsPlugin.IUpgradeStatusListener iUpgradeStatusListener) {
        l.g(context, "context");
        l.g(str, "id");
        l.g(str2, "path");
        l.g(iUpgradeStatusListener, "listener");
        final CameraUpgradeInfo cameraUpgradeInfoDecode = CameraUpgradeInfo.Companion.decode(str);
        if (cameraUpgradeInfoDecode == null) {
            return upgradeFirmware(context, str2, z, iUpgradeStatusListener);
        }
        CameraHelper cameraHelper = CameraHelper.INSTANCE;
        if (cameraHelper.isUpgrading(cameraUpgradeInfoDecode)) {
            Log.e(TAG, "camera isUpgrading");
            iUpgradeStatusListener.onFailed("camera is upgrading");
            return false;
        }
        List<CameraHdiApi> listFindDevices = cameraHelper.findDevices(context, new Plugin$upgradeFirmware$cameras$1(cameraUpgradeInfoDecode));
        final ArrayList arrayList = new ArrayList(f.a.y.a.C(listFindDevices, 10));
        Iterator<T> it = listFindDevices.iterator();
        while (it.hasNext()) {
            arrayList.add(((CameraHdiApi) it.next()).upgradeCamera(str2));
        }
        new Thread(new Runnable() { // from class: com.cvte.dss.ups.camera.Plugin.upgradeFirmware.1
            @Override // java.lang.Runnable
            public final void run() throws ExecutionException, InterruptedException {
                CameraHelper.INSTANCE.upgradeCamera(context, cameraUpgradeInfoDecode, arrayList, iUpgradeStatusListener);
            }
        }).start();
        return true;
    }

    @Override // com.cvte.dss.ups.plugin.IUpsPlugin
    public boolean upgradeFirmware(final Context context, String str, boolean z, final IUpsPlugin.IUpgradeStatusListener iUpgradeStatusListener) {
        l.g(context, "context");
        l.g(str, "path");
        l.g(iUpgradeStatusListener, "listener");
        CameraHelper cameraHelper = CameraHelper.INSTANCE;
        final CameraHdiApi cameraHdiApiFindDevice = cameraHelper.findDevice(context);
        if (!a.a0(str)) {
            Log.e(TAG, "upgrade file not found");
            iUpgradeStatusListener.onFailed("upgrade file no find");
            return false;
        }
        if (cameraHdiApiFindDevice == null) {
            Log.e(TAG, "camera not found");
            iUpgradeStatusListener.onFailed("no find");
            return false;
        }
        if (cameraHelper.isUpgrading(cameraHdiApiFindDevice.getCameraInfo())) {
            Log.e(TAG, "camera isUpgrading");
            iUpgradeStatusListener.onFailed("camera is upgrading");
            return false;
        }
        final String strCopyFileToTemp = CommonUtils.INSTANCE.copyFileToTemp(str, CameraManager.TEMP_CAMERA_FILE_DIR);
        new Thread(new Runnable() { // from class: com.cvte.dss.ups.camera.Plugin.upgradeFirmware.2
            @Override // java.lang.Runnable
            public final void run() {
                new CameraManager(cameraHdiApiFindDevice).upgradeCamera(strCopyFileToTemp, iUpgradeStatusListener, context);
            }
        }).start();
        return true;
    }
}
