package p138.p176.p291.p323.p324;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;
import p138.p139.p143.p144.C2803;
import p138.p176.p291.C5559;
import p138.p176.p291.p323.p324.C5981;

/* JADX INFO: renamed from: ދ.ހ.ށ.ފ.ލ.ހ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public class C5985 {

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final File f16840;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final C5559 f16841;

    /* JADX INFO: renamed from: ދ.ހ.ށ.ފ.ލ.ހ$֏, reason: contains not printable characters */
    public enum EnumC5986 {
        ATTEMPT_MIGRATION,
        NOT_GENERATED,
        UNREGISTERED,
        REGISTERED,
        REGISTER_ERROR
    }

    public C5985(C5559 c5559) {
        c5559.m8928();
        File filesDir = c5559.f15975.getFilesDir();
        StringBuilder sbM4889 = C2803.m4889("PersistedInstallation.");
        sbM4889.append(c5559.m8929());
        sbM4889.append(".json");
        this.f16840 = new File(filesDir, sbM4889.toString());
        this.f16841 = c5559;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public AbstractC5987 m9326() {
        JSONObject jSONObject;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bArr = new byte[16384];
        try {
            FileInputStream fileInputStream = new FileInputStream(this.f16840);
            while (true) {
                try {
                    int i = fileInputStream.read(bArr, 0, 16384);
                    if (i < 0) {
                        break;
                    }
                    byteArrayOutputStream.write(bArr, 0, i);
                } catch (Throwable th) {
                    try {
                        throw th;
                    } finally {
                        try {
                            fileInputStream.close();
                        } catch (Throwable unused) {
                        }
                    }
                }
            }
            jSONObject = new JSONObject(byteArrayOutputStream.toString());
        } catch (IOException | JSONException unused2) {
            jSONObject = new JSONObject();
        }
        String strOptString = jSONObject.optString("Fid", null);
        EnumC5986 enumC5986 = EnumC5986.ATTEMPT_MIGRATION;
        int iOptInt = jSONObject.optInt("Status", 0);
        String strOptString2 = jSONObject.optString("AuthToken", null);
        String strOptString3 = jSONObject.optString("RefreshToken", null);
        long jOptLong = jSONObject.optLong("TokenCreationEpochInSecs", 0L);
        long jOptLong2 = jSONObject.optLong("ExpiresInSecs", 0L);
        String strOptString4 = jSONObject.optString("FisError", null);
        C5981.C5983 c5983 = (C5981.C5983) AbstractC5987.m9328();
        c5983.f16830 = strOptString;
        c5983.mo9319(EnumC5986.values()[iOptInt]);
        c5983.f16832 = strOptString2;
        c5983.f16833 = strOptString3;
        c5983.mo9321(jOptLong);
        c5983.mo9318(jOptLong2);
        c5983.f16836 = strOptString4;
        return c5983.mo9320();
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public AbstractC5987 m9327(AbstractC5987 abstractC5987) {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("Fid", ((C5981) abstractC5987).f16823);
            jSONObject.put("Status", ((C5981) abstractC5987).f16824.ordinal());
            jSONObject.put("AuthToken", ((C5981) abstractC5987).f16825);
            jSONObject.put("RefreshToken", ((C5981) abstractC5987).f16826);
            jSONObject.put("TokenCreationEpochInSecs", ((C5981) abstractC5987).f16828);
            jSONObject.put("ExpiresInSecs", ((C5981) abstractC5987).f16827);
            jSONObject.put("FisError", ((C5981) abstractC5987).f16829);
            C5559 c5559 = this.f16841;
            c5559.m8928();
            File fileCreateTempFile = File.createTempFile("PersistedInstallation", "tmp", c5559.f15975.getFilesDir());
            FileOutputStream fileOutputStream = new FileOutputStream(fileCreateTempFile);
            fileOutputStream.write(jSONObject.toString().getBytes("UTF-8"));
            fileOutputStream.close();
            if (!fileCreateTempFile.renameTo(this.f16840)) {
                throw new IOException("unable to rename the tmpfile to PersistedInstallation");
            }
        } catch (IOException | JSONException unused) {
        }
        return abstractC5987;
    }
}
