package com.bytedance.sdk.openadsdk.hlt;

import android.text.TextUtils;
import androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import com.bytedance.sdk.openadsdk.hlt.ro;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public class yz {

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private ajl f7425lb;
    private lb ro;
    private fm yz;
    private final String fm = "StrategyCenter";
    private int jnr = 0;
    private final ConcurrentHashMap<String, Object> ajl = new ConcurrentHashMap<>();
    private Runnable wsy = new Runnable() { // from class: com.bytedance.sdk.openadsdk.hlt.yz.2
        @Override // java.lang.Runnable
        public void run() {
            yz.this.lb();
        }
    };

    public yz(ajl ajlVar) {
        this.ro = null;
        wsy wsyVar = new wsy(ajlVar);
        this.f7425lb = wsyVar;
        String strLb = wsyVar.lb();
        if (!TextUtils.isEmpty(strLb) && !strLb.startsWith("pag")) {
            strLb = "pag_".concat(strLb);
        }
        this.ro = new lb(this.f7425lb.ro(), strLb);
    }

    public void fm(fm fmVar) {
        this.yz = fmVar;
    }

    public lb ro() {
        return this.ro;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void lb() {
        ajl ajlVar = this.f7425lb;
        if (ajlVar == null || ajlVar.jnr() == null || this.f7425lb.ajl() == null || this.f7425lb.wu()) {
            return;
        }
        this.f7425lb.fm().execute(new Runnable() { // from class: com.bytedance.sdk.openadsdk.hlt.yz.1
            @Override // java.lang.Runnable
            public void run() {
                OutputStream outputStream;
                yz.this.jnr++;
                try {
                    if (yz.this.yz != null) {
                        yz.this.yz.fm();
                    }
                    HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(yz.this.f7425lb.jnr()).openConnection();
                    if (yz.this.f7425lb.wsy() != null && yz.this.f7425lb.wsy().size() > 0) {
                        for (Map.Entry<String, String> entry : yz.this.f7425lb.wsy().entrySet()) {
                            httpURLConnection.addRequestProperty(entry.getKey(), entry.getValue());
                        }
                    }
                    httpURLConnection.setRequestMethod(HlsMediaPlaylist.Interstitial.CUE_TRIGGER_POST);
                    httpURLConnection.setRequestProperty(RtspHeaders.CONTENT_TYPE, "application/json");
                    try {
                        outputStream = httpURLConnection.getOutputStream();
                        try {
                            outputStream.write(yz.this.f7425lb.ajl().toString().getBytes());
                            outputStream.close();
                            int responseCode = httpURLConnection.getResponseCode();
                            if (responseCode == 200) {
                                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                                StringBuffer stringBuffer = new StringBuffer();
                                while (true) {
                                    String line = bufferedReader.readLine();
                                    if (line == null) {
                                        break;
                                    } else {
                                        stringBuffer.append(line);
                                    }
                                }
                                bufferedReader.close();
                                JSONObject jSONObjectFm = yz.this.f7425lb.fm(new JSONObject(stringBuffer.toString()));
                                yz.this.ro.ro();
                                yz.this.yz();
                                yz.this.ro.fm(jSONObjectFm);
                                if (yz.this.yz != null) {
                                    yz.this.yz.ro();
                                }
                            } else if (yz.this.yz != null) {
                                yz.this.yz.fm(responseCode, httpURLConnection.getResponseMessage());
                            }
                        } catch (Throwable th) {
                            th = th;
                            if (outputStream != null) {
                                outputStream.close();
                            }
                            throw th;
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        outputStream = null;
                    }
                } catch (Throwable th3) {
                    if (th3.getMessage() != null) {
                        th3.getMessage();
                    }
                    if (yz.this.yz != null) {
                        yz.this.yz.fm(-1, th3.getMessage());
                    }
                }
                yz.this.ro.fm("local_last_update_time", System.currentTimeMillis());
                yz.this.fm();
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void yz() {
        this.ajl.clear();
    }

    public void fm() {
        if (this.f7425lb != null) {
            int i10 = 3600000;
            int iFm = this.ro.fm("req_interval", 3600000);
            long j10 = 0;
            long jRo = this.ro.ro("local_last_update_time", 0L);
            if (iFm >= 600000 && iFm <= 86400000) {
                i10 = iFm;
            }
            long jCurrentTimeMillis = System.currentTimeMillis() - jRo;
            "before  realInterval=".concat(String.valueOf(jCurrentTimeMillis));
            if (jCurrentTimeMillis >= 0) {
                long j11 = i10;
                if (jCurrentTimeMillis <= j11) {
                    j10 = j11 - jCurrentTimeMillis;
                }
            }
            "after  realInterval=".concat(String.valueOf(j10));
            this.f7425lb.yz().removeCallbacks(this.wsy);
            if (this.jnr > 24) {
                return;
            }
            this.f7425lb.yz().postDelayed(this.wsy, j10);
        }
    }

    public int fm(String str, int i10) {
        lb lbVar = this.ro;
        return lbVar == null ? i10 : lbVar.fm(str, i10);
    }

    public String fm(String str, String str2) {
        lb lbVar = this.ro;
        return lbVar == null ? str2 : lbVar.fm(str, str2);
    }

    public boolean fm(String str, boolean z10) {
        lb lbVar = this.ro;
        return lbVar == null ? z10 : lbVar.fm(str, z10);
    }

    /* JADX WARN: Code restructure failed: missing block: B:10:0x0019, code lost:
    
        if (r4.getClass().isInstance(r0) != false) goto L11;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public <T> T fm(String str, T t10, ro.fm<T> fmVar) {
        if (this.ro != null && str != null) {
            T t11 = (T) this.ajl.get(str);
            if (t11 != null) {
                if (t10 != null) {
                }
                return t11;
            }
            if (fmVar != null) {
                try {
                    T t12 = (T) this.ro.fm(str, t10, fmVar);
                    if (t12 != null) {
                        this.ajl.put(str, t12);
                        return t12;
                    }
                } catch (Exception unused) {
                }
            }
        }
        return t10;
    }
}
