package com.bytedance.sdk.openadsdk.core.settings;

import android.content.Context;
import android.os.SystemClock;
import androidx.annotation.Nullable;
import com.bytedance.sdk.component.utils.wey;
import com.bytedance.sdk.openadsdk.core.gof;
import com.bytedance.sdk.openadsdk.core.settings.jnr;
import com.bytedance.sdk.openadsdk.utils.dsz;
import com.bytedance.sdk.openadsdk.utils.xgn;
import defpackage.e;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes2.dex */
public abstract class vt implements jnr {
    private boolean ku;
    private final fm vt;
    private final String wsy;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private final ConcurrentHashMap<String, Object> f7284lb = new ConcurrentHashMap<>();
    private final Object yz = new Object();
    private final Object jnr = new Object();
    private final CountDownLatch ajl = new CountDownLatch(1);
    private Properties wu = new Properties();

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    private volatile boolean f7283ef = false;

    public interface fm {
        void fm();

        void ro();
    }

    public vt(fm fmVar) {
        String strFm = fm();
        this.wsy = strFm;
        this.vt = fmVar;
        xgn.fm(new com.bytedance.sdk.component.wu.ro.lb(e.i("SetL_", strFm)) { // from class: com.bytedance.sdk.openadsdk.core.settings.vt.1
            @Override // java.lang.Runnable
            public void run() {
                vt.this.fm(false);
            }
        });
    }

    private void ajl() {
        if (this.ku && gof.fm() != null) {
            fm(true);
        }
        if (this.f7283ef) {
            return;
        }
        try {
            SystemClock.elapsedRealtime();
            this.ajl.await(xgn.ajl() ? 4 : 8, TimeUnit.SECONDS);
        } catch (InterruptedException e10) {
            wey.fm("SdkSettings.Prop", "awaitLoadedLocked: ", e10);
        }
    }

    @Nullable
    private File wsy() {
        Context contextFm = gof.fm();
        if (contextFm != null) {
            return new File(contextFm.getFilesDir(), this.wsy);
        }
        return null;
    }

    public abstract String fm();

    public void jnr() {
        fm fmVar = this.vt;
        if (fmVar != null) {
            fmVar.ro();
        }
    }

    public boolean lb() {
        return this.f7283ef;
    }

    public void yz() {
        File fileWsy = wsy();
        if (fileWsy == null || !fileWsy.exists()) {
            return;
        }
        fileWsy.delete();
    }

    public jnr.fm ro() {
        return new ro();
    }

    public String fm(String str, String str2) {
        if (str == null || str.isEmpty()) {
            return str2;
        }
        ajl();
        return this.wu.getProperty(str, str2);
    }

    public int fm(String str, int i10) {
        if (str != null && !str.isEmpty()) {
            ajl();
            try {
                return Integer.parseInt(this.wu.getProperty(str, String.valueOf(i10)));
            } catch (NumberFormatException e10) {
                wey.fm("SdkSettings.Prop", "", e10);
            }
        }
        return i10;
    }

    public long fm(String str, long j10) {
        if (str != null && !str.isEmpty()) {
            ajl();
            try {
                return Long.parseLong(this.wu.getProperty(str, String.valueOf(j10)));
            } catch (NumberFormatException e10) {
                wey.fm("SdkSettings.Prop", "", e10);
            }
        }
        return j10;
    }

    public float fm(String str, float f10) {
        if (str != null && !str.isEmpty()) {
            ajl();
            try {
                return Float.parseFloat(this.wu.getProperty(str, String.valueOf(f10)));
            } catch (NumberFormatException e10) {
                wey.fm("SdkSettings.Prop", "", e10);
            }
        }
        return f10;
    }

    public class ro implements jnr.fm {
        private final Map<String, Object> ro = new HashMap();

        /* JADX INFO: renamed from: lb, reason: collision with root package name */
        private final Object f7285lb = new Object();

        public ro() {
        }

        @Override // com.bytedance.sdk.openadsdk.core.settings.jnr.fm
        public void fm() {
            Object obj;
            Properties properties = new Properties();
            synchronized (this.f7285lb) {
                try {
                    properties.putAll(vt.this.wu);
                    boolean z10 = false;
                    for (Map.Entry<String, Object> entry : this.ro.entrySet()) {
                        String key = entry.getKey();
                        Object value = entry.getValue();
                        if (value != this && value != null) {
                            if (properties.containsKey(key) && (obj = properties.get(key)) != null && obj.equals(value)) {
                            }
                            properties.put(key, String.valueOf(value));
                            z10 = true;
                        } else if (properties.containsKey(key)) {
                            properties.remove(key);
                            z10 = true;
                        }
                    }
                    this.ro.clear();
                    if (z10) {
                        vt.this.fm(properties);
                        vt.this.wu = properties;
                        vt.this.f7284lb.clear();
                        vt.this.ku = false;
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }

        @Override // com.bytedance.sdk.openadsdk.core.settings.jnr.fm
        public jnr.fm fm(String str, String str2) {
            synchronized (this.f7285lb) {
                this.ro.put(str, str2);
            }
            return this;
        }

        @Override // com.bytedance.sdk.openadsdk.core.settings.jnr.fm
        public jnr.fm fm(String str, int i10) {
            synchronized (this.f7285lb) {
                this.ro.put(str, Integer.valueOf(i10));
            }
            return this;
        }

        @Override // com.bytedance.sdk.openadsdk.core.settings.jnr.fm
        public jnr.fm fm(String str, long j10) {
            synchronized (this.f7285lb) {
                this.ro.put(str, Long.valueOf(j10));
            }
            return this;
        }

        @Override // com.bytedance.sdk.openadsdk.core.settings.jnr.fm
        public jnr.fm fm(String str) {
            synchronized (this.f7285lb) {
                this.ro.put(str, this);
            }
            return this;
        }

        @Override // com.bytedance.sdk.openadsdk.core.settings.jnr.fm
        public jnr.fm fm(String str, float f10) {
            synchronized (this.f7285lb) {
                this.ro.put(str, Float.valueOf(f10));
            }
            return this;
        }

        @Override // com.bytedance.sdk.openadsdk.core.settings.jnr.fm
        public jnr.fm fm(String str, boolean z10) {
            synchronized (this.f7285lb) {
                this.ro.put(str, Boolean.valueOf(z10));
            }
            return this;
        }
    }

    public boolean fm(String str, boolean z10) {
        if (str != null && !str.isEmpty()) {
            ajl();
            try {
                return Boolean.parseBoolean(this.wu.getProperty(str, String.valueOf(z10)));
            } catch (Exception e10) {
                wey.fm("SdkSettings.Prop", "", e10);
            }
        }
        return z10;
    }

    public void fm(boolean z10) {
        fm fmVar;
        Object obj;
        synchronized (this.yz) {
            try {
                if (!this.f7283ef || z10) {
                    if (gof.fm() != null) {
                        this.ku = false;
                        File fileWsy = wsy();
                        if (fileWsy != null && fileWsy.exists()) {
                            Properties properties = new Properties();
                            FileInputStream fileInputStream = null;
                            try {
                                try {
                                    FileInputStream fileInputStream2 = new FileInputStream(fileWsy);
                                    try {
                                        properties.load(fileInputStream2);
                                        properties.size();
                                        new StringBuilder("items from ").append(fileWsy.getAbsolutePath());
                                        if (!properties.isEmpty()) {
                                            this.wu = properties;
                                            this.f7284lb.clear();
                                        }
                                        dsz.fm(fileInputStream2);
                                        obj = this.yz;
                                    } catch (OutOfMemoryError unused) {
                                        fileInputStream = fileInputStream2;
                                        try {
                                            com.bytedance.sdk.component.utils.wsy.lb(fileWsy);
                                        } catch (Throwable th) {
                                            wey.fm("SdkSettings.Prop", "delete: ", th);
                                        }
                                        if (fileInputStream != null) {
                                            dsz.fm(fileInputStream);
                                        }
                                        obj = this.yz;
                                    } catch (Throwable th2) {
                                        th = th2;
                                        fileInputStream = fileInputStream2;
                                        wey.fm("SdkSettings.Prop", "reload: ", th);
                                        if (fileInputStream != null) {
                                            dsz.fm(fileInputStream);
                                        }
                                        obj = this.yz;
                                    }
                                } catch (Throwable th3) {
                                    if (fileInputStream != null) {
                                        dsz.fm(fileInputStream);
                                    }
                                    this.yz.notifyAll();
                                    throw th3;
                                }
                            } catch (OutOfMemoryError unused2) {
                            } catch (Throwable th4) {
                                th = th4;
                            }
                            obj.notifyAll();
                        }
                    } else {
                        this.ku = true;
                    }
                    boolean z11 = this.f7283ef;
                    this.ajl.countDown();
                    this.f7283ef = true;
                    if (!z11 && (fmVar = this.vt) != null) {
                        fmVar.fm();
                    }
                }
            } catch (Throwable th5) {
                throw th5;
            }
        }
    }

    public <T> T fm(String str, T t10, jnr.ro<T> roVar) {
        T tRo;
        if (str != null && !str.isEmpty()) {
            T t11 = (T) this.f7284lb.get(str);
            if (t11 != null) {
                return t11;
            }
            ajl();
            String property = this.wu.getProperty(str, null);
            if (property != null && roVar != null && (tRo = roVar.ro(property)) != null) {
                this.f7284lb.put(str, tRo);
                return tRo;
            }
        }
        return t10;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void fm(Properties properties) {
        FileOutputStream fileOutputStream;
        synchronized (this.jnr) {
            FileOutputStream fileOutputStream2 = null;
            try {
                try {
                    fileOutputStream = new FileOutputStream(wsy());
                } catch (Exception e10) {
                    e = e10;
                }
            } catch (Throwable th) {
                th = th;
            }
            try {
                properties.store(fileOutputStream, (String) null);
                dsz.fm(fileOutputStream);
            } catch (Exception e11) {
                e = e11;
                fileOutputStream2 = fileOutputStream;
                wey.fm("SdkSettings.Prop", "saveToLocal: ", e);
                if (fileOutputStream2 != null) {
                    dsz.fm(fileOutputStream2);
                }
            } catch (Throwable th2) {
                th = th2;
                fileOutputStream2 = fileOutputStream;
                if (fileOutputStream2 != null) {
                    dsz.fm(fileOutputStream2);
                }
                throw th;
            }
        }
    }
}
