package com.ironsource.sdk.controller; import com.ironsource.i9; import com.ironsource.nb; import com.ironsource.ra; import com.ironsource.sdk.utils.IronSourceStorageUtils; import com.ironsource.sdk.utils.SDKUtils; import com.ironsource.sf; import com.ironsource.v8; import com.ironsource.vp; import com.ironsource.xf; import com.ironsource.zf; import org.json.JSONException; import org.json.JSONObject; /* loaded from: classes4.dex */ class d { static final String h = "controllerSourceData"; private static final String i = "next_"; private static final String j = "fallback_"; private static final String k = "controllerSourceCode"; private long a; private int b; private c c; private EnumC0180d d = EnumC0180d.NONE; private String e; private String f; private ra g; public class a extends JSONObject { public a() throws JSONException { putOpt(v8.a.i, Integer.valueOf(d.this.b)); putOpt(d.k, Integer.valueOf(d.this.d.a())); } } public static /* synthetic */ class b { static final /* synthetic */ int[] a; static { int[] iArr = new int[c.values().length]; a = iArr; try { iArr[c.FETCH_FROM_SERVER_NO_FALLBACK.ordinal()] = 1; } catch (NoSuchFieldError unused) { } try { a[c.FETCH_FROM_SERVER_WITH_LOCAL_FALLBACK.ordinal()] = 2; } catch (NoSuchFieldError unused2) { } try { a[c.FETCH_FOR_NEXT_SESSION_LOAD_FROM_LOCAL.ordinal()] = 3; } catch (NoSuchFieldError unused3) { } } } public enum c { FETCH_FROM_SERVER_NO_FALLBACK, FETCH_FROM_SERVER_WITH_LOCAL_FALLBACK, FETCH_FOR_NEXT_SESSION_LOAD_FROM_LOCAL } /* renamed from: com.ironsource.sdk.controller.d$d, reason: collision with other inner class name */ public enum EnumC0180d { NONE(0), PREPARED_CONTROLLER_LOADED(1), CONTROLLER_FROM_SERVER(2), MISSING_PREPARED_CONTROLLER_LOAD_LAST_USED_CONTROLLER(3), FAILED_RENAME_PREPARED_CONTROLLER_LOAD_LAST_USED_CONTROLLER(4), FALLBACK_CONTROLLER_RECOVERY(5); private int a; EnumC0180d(int i) { this.a = i; } public int a() { return this.a; } } public d(JSONObject jSONObject, String str, String str2, ra raVar) { int optInt = jSONObject.optInt(v8.a.i, -1); this.b = optInt; this.c = a(optInt); this.e = str; this.f = str2; this.g = raVar; } private c a(int i2) { return i2 != 1 ? i2 != 2 ? c.FETCH_FROM_SERVER_NO_FALLBACK : c.FETCH_FOR_NEXT_SESSION_LOAD_FROM_LOCAL : c.FETCH_FROM_SERVER_WITH_LOCAL_FALLBACK; } private void a(EnumC0180d enumC0180d) { sf a2 = new sf().a(nb.y, Integer.valueOf(this.b)).a(nb.z, Integer.valueOf(enumC0180d.a())); if (this.a > 0) { a2.a(nb.B, Long.valueOf(System.currentTimeMillis() - this.a)); } xf.a(vp.w, a2.a()); } private void a(zf zfVar) { if (this.g.c()) { return; } this.g.a(zfVar, this.f); } private boolean a() { try { if (j()) { return IronSourceStorageUtils.renameFile(h().getPath(), g().getPath()); } return false; } catch (Exception e) { i9.d().a(e); return false; } } private boolean b() throws Exception { return IronSourceStorageUtils.renameFile(i().getPath(), g().getPath()); } private void c() { try { zf g = g(); if (g.exists()) { zf h2 = h(); if (h2.exists()) { h2.delete(); } IronSourceStorageUtils.renameFile(g.getPath(), h2.getPath()); } } catch (Exception e) { i9.d().a(e); } } private void d() { IronSourceStorageUtils.deleteFile(h()); } private void e() { IronSourceStorageUtils.deleteFile(g()); } private zf h() { return new zf(this.e, "fallback_mobileController.html"); } private zf i() { return new zf(this.e, "next_mobileController.html"); } private boolean j() { return h().exists(); } private void l() { sf a2 = new sf().a(nb.y, Integer.valueOf(this.b)); if (this.a > 0) { a2.a(nb.B, Long.valueOf(System.currentTimeMillis() - this.a)); } xf.a(vp.x, a2.a()); } public void a(sf sfVar) { sfVar.a(nb.y, Integer.valueOf(this.b)); xf.a(vp.v, sfVar.a()); this.a = System.currentTimeMillis(); } public void a(Runnable runnable) { if (m()) { return; } if (this.c == c.FETCH_FROM_SERVER_WITH_LOCAL_FALLBACK) { d(); } EnumC0180d enumC0180d = EnumC0180d.CONTROLLER_FROM_SERVER; this.d = enumC0180d; a(enumC0180d); runnable.run(); } public void a(Runnable runnable, Runnable runnable2) { if (m()) { return; } if (this.c != c.FETCH_FROM_SERVER_WITH_LOCAL_FALLBACK || !a()) { l(); runnable2.run(); } else { EnumC0180d enumC0180d = EnumC0180d.FALLBACK_CONTROLLER_RECOVERY; this.d = enumC0180d; a(enumC0180d); runnable.run(); } } public JSONObject f() throws JSONException { return new a(); } public zf g() { return new zf(this.e, v8.f); } public boolean k() { zf zfVar; int i2 = b.a[this.c.ordinal()]; if (i2 == 1) { e(); zfVar = new zf(this.e, SDKUtils.getFileName(this.f)); } else { if (i2 != 2) { if (i2 == 3) { try { zf g = g(); zf i3 = i(); if (!i3.exists() && !g.exists()) { a(new zf(this.e, SDKUtils.getFileName(this.f))); return false; } if (!i3.exists() && g.exists()) { EnumC0180d enumC0180d = EnumC0180d.MISSING_PREPARED_CONTROLLER_LOAD_LAST_USED_CONTROLLER; this.d = enumC0180d; a(enumC0180d); a(new zf(this.e, i3.getName())); return true; } c(); if (b()) { EnumC0180d enumC0180d2 = EnumC0180d.PREPARED_CONTROLLER_LOADED; this.d = enumC0180d2; a(enumC0180d2); d(); a(new zf(this.e, i3.getName())); return true; } if (!a()) { a(new zf(this.e, SDKUtils.getFileName(this.f))); return false; } EnumC0180d enumC0180d3 = EnumC0180d.FAILED_RENAME_PREPARED_CONTROLLER_LOAD_LAST_USED_CONTROLLER; this.d = enumC0180d3; a(enumC0180d3); a(new zf(this.e, i3.getName())); return true; } catch (Exception e) { i9.d().a(e); } } return false; } c(); zfVar = new zf(this.e, SDKUtils.getFileName(this.f)); } a(zfVar); return false; } public boolean m() { return this.d != EnumC0180d.NONE; } }