package com.ifpdos.autotest.lancontrol.module.other.impls.gamma;

import android.annotation.SuppressLint;
import android.util.Log;
import com.seewo.sdk.model.SDKKeyboardCode;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/* JADX INFO: compiled from: GammaFileHelper.java */
/* JADX INFO: loaded from: classes.dex */
public class a {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private static final byte[] f2203a = {103, 97, 109, 109, 97, 0, 0, 0, 1, 0, SDKKeyboardCode.FUNCTION_KEY_BOARD_M, 6, -64, 13, SDKKeyboardCode.FUNCTION_KEY_BOARD_DOWN, -36};

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static final byte[] f2204b = {SDKKeyboardCode.FUNCTION_KEY_BOARD_INSERT, 110, 100, 101, 120, SDKKeyboardCode.FUNCTION_KEY_BOARD_RIGHT_SLASH, 0, 0};

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private static a f2205c;

    private a() {
    }

    private boolean a(File file, byte[] bArr) throws Throwable {
        FileOutputStream fileOutputStream;
        if (bArr == null) {
            b.c.c.l.b.a("GammaFileHelper", "Null data!");
            return false;
        }
        FileOutputStream fileOutputStream2 = null;
        try {
            try {
                fileOutputStream = new FileOutputStream(file);
            } catch (IOException e2) {
                e = e2;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            fileOutputStream.write(bArr);
            fileOutputStream.close();
            try {
                fileOutputStream.close();
            } catch (IOException e3) {
                b.c.c.l.b.b("GammaFileHelper", e3);
            }
            return true;
        } catch (IOException e4) {
            e = e4;
            fileOutputStream2 = fileOutputStream;
            b.c.c.l.b.b("GammaFileHelper", e);
            if (fileOutputStream2 != null) {
                try {
                    fileOutputStream2.close();
                } catch (IOException e5) {
                    b.c.c.l.b.b("GammaFileHelper", e5);
                }
            }
            return false;
        } catch (Throwable th2) {
            th = th2;
            fileOutputStream2 = fileOutputStream;
            if (fileOutputStream2 != null) {
                try {
                    fileOutputStream2.close();
                } catch (IOException e6) {
                    b.c.c.l.b.b("GammaFileHelper", e6);
                }
            }
            throw th;
        }
    }

    private byte[] e(b bVar) {
        byte[] bArrB = bVar.b();
        byte[] bArr = f2203a;
        int length = bArr.length;
        byte[] bArr2 = f2204b;
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(length + bArr2.length + bArrB.length);
        byteBufferAllocate.put(bArr).put(bArr2).put(bArrB);
        return byteBufferAllocate.array();
    }

    private byte[] f(Map<Integer, b> map) {
        if (map == null || map.isEmpty() || map.get(0) == null) {
            return null;
        }
        Set<Integer> setKeySet = map.keySet();
        int size = setKeySet.size();
        int[] iArr = new int[size];
        Iterator<Integer> it = setKeySet.iterator();
        int i = 0;
        while (it.hasNext()) {
            iArr[i] = it.next().intValue();
            i++;
        }
        Arrays.sort(iArr);
        int length = 0;
        for (int i2 = 0; i2 < size; i2++) {
            length += map.get(Integer.valueOf(iArr[i2])).b().length + 8;
        }
        byte[] bArr = f2203a;
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(bArr.length + length);
        byteBufferAllocate.put(bArr);
        for (int i3 = 0; i3 < size; i3++) {
            Integer numValueOf = Integer.valueOf(iArr[i3]);
            String str = String.format(Locale.getDefault(), "Index%d", numValueOf);
            byteBufferAllocate.put(str.getBytes());
            for (int i4 = 0; i4 < f2204b.length - str.length(); i4++) {
                byteBufferAllocate.put((byte) 0);
            }
            byteBufferAllocate.put(map.get(numValueOf).b());
        }
        byte[] bArrArray = byteBufferAllocate.array();
        bArrArray[8] = (byte) map.size();
        return bArrArray;
    }

    private byte[] g(MeasureData[] measureDataArr) {
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(measureDataArr.length * 6);
        for (MeasureData measureData : measureDataArr) {
            byteBufferAllocate.put(measureData.toBytes());
        }
        return byteBufferAllocate.array();
    }

    public static a h() {
        if (f2205c == null) {
            synchronized (a.class) {
                if (f2205c == null) {
                    f2205c = new a();
                }
            }
        }
        return f2205c;
    }

    private byte[] l(File file) {
        if (!file.exists()) {
            b.c.c.l.b.a("GammaFileHelper", "File not exits: " + file.getName());
            return null;
        }
        byte[] bArr = new byte[(int) file.length()];
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.read(bArr);
            fileInputStream.close();
            return bArr;
        } catch (IOException e2) {
            Log.e("GammaFileHelper", "io exception", e2);
            return null;
        }
    }

    public boolean b(Map<Integer, b> map) {
        File file = new File("/atv/pq/", "gamma_v620.b");
        if (file.exists() && !file.delete()) {
            b.c.c.l.b.a("GammaFileHelper", "Delete [" + file.getName() + "] fail!");
            return false;
        }
        try {
            if (file.createNewFile()) {
                return a(file, f(map));
            }
            b.c.c.l.b.a("GammaFileHelper", "Create [" + file.getName() + "] fail!");
            return false;
        } catch (IOException e2) {
            b.c.c.l.b.b("GammaFileHelper", e2);
            return false;
        }
    }

    public boolean c(MeasureData[] measureDataArr) {
        File file = new File("/atv/pq/", "measure_data.b");
        if (file.exists() && !file.delete()) {
            b.c.c.l.b.a("GammaFileHelper", "Delete [" + file.getName() + "] fail!");
            return false;
        }
        try {
            if (file.createNewFile()) {
                return a(file, g(measureDataArr));
            }
            b.c.c.l.b.a("GammaFileHelper", "Create [" + file.getName() + "] fail!");
            return false;
        } catch (IOException e2) {
            b.c.c.l.b.b("GammaFileHelper", e2);
            return false;
        }
    }

    public boolean d(b bVar) {
        File file = new File("/atv/pq/", "gamma_backup.b");
        if (file.exists() && !file.delete()) {
            b.c.c.l.b.a("GammaFileHelper", "Delete [" + file.getName() + "] fail!");
            return false;
        }
        try {
            if (file.createNewFile()) {
                return a(file, e(bVar));
            }
            b.c.c.l.b.a("GammaFileHelper", "Create [" + file.getName() + "] fail!");
            return false;
        } catch (IOException e2) {
            b.c.c.l.b.b("GammaFileHelper", e2);
            return false;
        }
    }

    public c i(File file) {
        return c.a(l(file));
    }

    @SuppressLint({"UseSparseArrays"})
    public Map<Integer, b> j() {
        byte[] bArrL = l(new File("/atv/pq/", "gamma_v620.b"));
        HashMap map = new HashMap();
        if (bArrL == null || bArrL.length < 1544) {
            b.c.c.l.b.a("robin", "[bytes] " + bArrL);
            StringBuilder sb = new StringBuilder();
            sb.append("[bytes] ");
            sb.append(bArrL != null ? bArrL.length : 0);
            b.c.c.l.b.a("robin", sb.toString());
            return map;
        }
        byte[] bArr = new byte[1544];
        byte[] bArr2 = new byte[8];
        byte b2 = bArrL[8];
        int i = 16;
        for (int i2 = 0; i2 < b2; i2++) {
            System.arraycopy(bArrL, i, bArr2, 0, 8);
            int i3 = Integer.parseInt(new String(bArr2).trim().replace("Index", ""));
            int i4 = i + 8;
            System.arraycopy(bArrL, i4, bArr, 0, 1544);
            b bVar = new b();
            bVar.d(bArr);
            map.put(Integer.valueOf(i3), bVar);
            i = i4 + 1544;
        }
        return map;
    }

    public MeasureData[] k() {
        File file = new File("/atv/pq/", "measure_data.b");
        MeasureData[] measureDataArr = null;
        if (!file.exists()) {
            return null;
        }
        byte[] bArrL = l(file);
        if (bArrL != null && bArrL.length >= 6) {
            byte[] bArr = new byte[6];
            measureDataArr = new MeasureData[bArrL.length / 6];
            int i = 0;
            int i2 = 0;
            while (i < bArrL.length) {
                System.arraycopy(bArrL, i, bArr, 0, 6);
                measureDataArr[i2] = MeasureData.fromByteArray(bArr);
                i += 6;
                i2++;
            }
        }
        return measureDataArr;
    }
}
