package xyz.netfly.ui.live.epg;

import androidx.room.RoomDatabase;
import e.t0;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import xyz.stream.api.database.AppDatabase;
import xyz.stream.api.database.entity.EpgEntity;
import xyz.stream.api.entity.LiveEpgEntity;

/* JADX INFO: loaded from: classes3.dex */
public final class q {

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public static volatile q f35376p;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final ExecutorService f35377a = Executors.newFixedThreadPool(5);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final LinkedHashMap f35378b = new LinkedHashMap();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Map f35379c = Collections.synchronizedMap(new HashMap());

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public volatile ScheduledExecutorService f35380d = Executors.newSingleThreadScheduledExecutor();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public volatile n f35381e = null;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public volatile int f35382f = 0;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final Object f35383g = new Object();

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final ArrayList f35384h = new ArrayList();

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public volatile ExecutorService f35385i = Executors.newSingleThreadExecutor();

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public volatile m f35386j = null;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public volatile int f35387k = 0;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final Object f35388l = new Object();

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final HashMap f35389m = new HashMap();

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final Map f35390n = new LinkedHashMap<String, List<LiveEpgEntity>>() { // from class: xyz.netfly.ui.live.epg.EpgManager$1
        {
            super(300, 0.75f, true);
        }

        @Override // java.util.LinkedHashMap
        public final boolean removeEldestEntry(Map.Entry<String, List<LiveEpgEntity>> entry) {
            return size() > 300;
        }
    };

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public AppDatabase f35391o;

    public static void a(List list, LinkedHashMap linkedHashMap) {
        if (list == null || list.isEmpty()) {
            return;
        }
        Iterator it = list.iterator();
        while (it.hasNext()) {
            LiveEpgEntity liveEpgEntity = (LiveEpgEntity) it.next();
            if (liveEpgEntity != null) {
                StringBuilder sb = new StringBuilder();
                String startTime = liveEpgEntity.getStartTime();
                if (startTime == null) {
                    startTime = "";
                }
                sb.append(startTime);
                sb.append("_");
                String endTime = liveEpgEntity.getEndTime();
                if (endTime == null) {
                    endTime = "";
                }
                sb.append(endTime);
                sb.append("_");
                String title = liveEpgEntity.getTitle();
                sb.append(title != null ? title : "");
                linkedHashMap.put(sb.toString(), liveEpgEntity);
            }
        }
    }

    public static LiveEpgEntity g(List list) {
        if (list.isEmpty()) {
            return null;
        }
        Iterator it = list.iterator();
        while (it.hasNext()) {
            LiveEpgEntity liveEpgEntity = (LiveEpgEntity) it.next();
            if (kotlinx.coroutines.y.J(liveEpgEntity.getStartTime(), liveEpgEntity.getEndTime())) {
                return liveEpgEntity;
            }
        }
        return null;
    }

    public static q o() {
        if (f35376p == null) {
            synchronized (q.class) {
                try {
                    if (f35376p == null) {
                        f35376p = new q();
                    }
                } finally {
                }
            }
        }
        return f35376p;
    }

    public static long p(String str) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        simpleDateFormat.setTimeZone(TimeZone.getDefault());
        Date date = simpleDateFormat.parse(w(str));
        if (date == null) {
            return 0L;
        }
        return date.getTime();
    }

    public static String q(int i10, String str) {
        StringBuilder sb = new StringBuilder();
        sb.append(i10);
        sb.append("_");
        sb.append(str);
        sb.append("_");
        xyz.stream.utils.e.d().getClass();
        sb.append(xyz.stream.utils.e.f());
        return sb.toString();
    }

    public static ArrayList r(String str) {
        ArrayList arrayList = new ArrayList();
        try {
            Locale locale = Locale.US;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", locale);
            simpleDateFormat.setTimeZone(TimeZone.getDefault());
            Date date = simpleDateFormat.parse(w(str));
            if (date != null) {
                Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
                calendar.setTime(date);
                calendar.set(11, 23);
                calendar.set(12, 59);
                calendar.set(13, 59);
                calendar.set(14, RoomDatabase.MAX_BIND_PARAMETER_CNT);
                SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd", locale);
                simpleDateFormat2.setTimeZone(TimeZone.getTimeZone("UTC"));
                String str2 = simpleDateFormat2.format(date);
                if (str2 != null && !str2.isEmpty() && !arrayList.contains(str2)) {
                    arrayList.add(str2);
                }
                String str3 = simpleDateFormat2.format(calendar.getTime());
                if (str3 != null && !str3.isEmpty() && !arrayList.contains(str3)) {
                    arrayList.add(str3);
                }
            }
        } catch (Exception e10) {
            u.a.g(e10, new StringBuilder("getUtcDateKeysForLocalDate error: "));
        }
        if (arrayList.isEmpty()) {
            arrayList.add(w(str));
        }
        return arrayList;
    }

    public static String w(String str) {
        return (str == null || str.isEmpty()) ? kotlinx.coroutines.flow.h.y("yyyy-MM-dd") : str;
    }

    public final void b() {
        ExecutorService executorService;
        synchronized (this.f35383g) {
            this.f35387k++;
            this.f35384h.clear();
            this.f35386j = null;
            executorService = this.f35385i;
            this.f35385i = null;
        }
        if (executorService != null) {
            executorService.shutdownNow();
        }
    }

    public final void c() {
        b();
        synchronized (this.f35378b) {
            this.f35382f++;
            this.f35378b.clear();
            this.f35381e = null;
        }
        synchronized (this.f35379c) {
            this.f35379c.clear();
        }
        ScheduledExecutorService scheduledExecutorService = this.f35380d;
        if (scheduledExecutorService != null) {
            scheduledExecutorService.shutdownNow();
        }
        this.f35380d = null;
    }

    public final void d(String str, p pVar, List list) {
        ArrayList arrayList = list == null ? null : new ArrayList(list);
        synchronized (this.f35388l) {
            try {
                pVar.f35375b = arrayList;
                if (arrayList != null) {
                    ((HashMap) this.f35390n).put(str, new ArrayList(arrayList));
                }
            } finally {
            }
        }
        pVar.f35374a.countDown();
        synchronized (this.f35388l) {
            try {
                if (this.f35389m.get(str) == pVar) {
                    this.f35389m.remove(str);
                }
            } finally {
            }
        }
    }

    public final void e(int i10) {
        try {
            androidx.room.util.b.b((RoomDatabase) ((v6.j) this.f35391o.epgDao()).f33700a, false, true, new vf.b(i10, 0, kotlinx.coroutines.flow.h.p(-2)));
        } catch (Exception e10) {
            u.a.g(e10, new StringBuilder("deleteEpgByChannelBeforeDate error: "));
        }
    }

    public final List f(String str, ArrayList arrayList) {
        ArrayList arrayList2 = new ArrayList();
        if (arrayList.isEmpty()) {
            return arrayList2;
        }
        try {
            long jP = p(str);
            Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
            calendar.setTimeInMillis(p(str));
            calendar.set(11, 23);
            calendar.set(12, 59);
            calendar.set(13, 59);
            calendar.set(14, RoomDatabase.MAX_BIND_PARAMETER_CNT);
            long timeInMillis = calendar.getTimeInMillis();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                LiveEpgEntity liveEpgEntity = (LiveEpgEntity) it.next();
                if (liveEpgEntity != null && liveEpgEntity.getStartTime() != null && liveEpgEntity.getEndTime() != null) {
                    Date date = simpleDateFormat.parse(liveEpgEntity.getStartTime());
                    Date date2 = simpleDateFormat.parse(liveEpgEntity.getEndTime());
                    if (date != null && date2 != null) {
                        if (Math.min(date2.getTime(), timeInMillis) - Math.max(date.getTime(), jP) >= 60000) {
                            arrayList2.add(liveEpgEntity);
                        }
                    }
                }
            }
            Collections.sort(arrayList2, new com.google.android.material.button.c(this, 2));
            return arrayList2;
        } catch (Exception e10) {
            u.a.g(e10, new StringBuilder("filterEpgListForLocalDate error: "));
            return arrayList;
        }
    }

    public final void h(m mVar) {
        synchronized (this.f35383g) {
            try {
                if (mVar == this.f35386j) {
                    this.f35386j = null;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        y();
    }

    public final void i(n nVar) {
        boolean z10;
        synchronized (this.f35378b) {
            try {
                if (nVar == this.f35381e) {
                    this.f35381e = null;
                    z10 = true;
                } else {
                    z10 = false;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        if (z10) {
            z();
        }
    }

    public final ArrayList j(int i10, String str) {
        ArrayList arrayList = new ArrayList();
        xyz.stream.utils.e.d().getClass();
        for (EpgEntity epgEntity : (List) androidx.room.util.b.b((RoomDatabase) ((v6.j) this.f35391o.epgDao()).f33700a, true, false, new vf.c(i10, str, xyz.stream.utils.e.f()))) {
            arrayList.add(new LiveEpgEntity(epgEntity.getEpgId(), epgEntity.getTitle(), epgEntity.getDescription(), epgEntity.getStartTime(), epgEntity.getEndTime()));
        }
        Collections.sort(arrayList, new com.google.android.material.button.c(this, 2));
        return arrayList;
    }

    public final l k(int i10, String str) {
        l lVar;
        ArrayList<String> arrayListR = r(str);
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        boolean z10 = true;
        for (String str2 : arrayListR) {
            String strQ = q(i10, str2);
            synchronized (this.f35388l) {
                try {
                    ArrayList arrayList = null;
                    if (((HashMap) this.f35390n).containsKey(strQ)) {
                        List list = (List) ((LinkedHashMap) this.f35390n).get(strQ);
                        if (list != null) {
                            arrayList = new ArrayList(list);
                        }
                        lVar = new l(arrayList, true);
                    } else {
                        lVar = new l(null, false);
                    }
                } finally {
                }
            }
            List listJ = lVar.f35366b ? lVar.f35365a : j(i10, str2);
            if (!lVar.f35366b && listJ.isEmpty()) {
                z10 = false;
            }
            a(listJ, linkedHashMap);
        }
        ArrayList arrayList2 = new ArrayList(linkedHashMap.values());
        Collections.sort(arrayList2, new com.google.android.material.button.c(this, 2));
        return new l(f(str, arrayList2), z10);
    }

    public final void l(int i10, String str, xyz.stream.api.e eVar) {
        int i11;
        String strW = w(str);
        synchronized (this.f35383g) {
            i11 = this.f35387k;
        }
        this.f35377a.execute(new i(this, i10, strW, eVar, i11, 0));
    }

    public final List m(int i10, String str, o oVar) {
        ArrayList arrayList;
        t0 t0Var;
        t0 t0Var2;
        ArrayList<String> arrayListR = r(str);
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        for (String str2 : arrayListR) {
            if (Thread.currentThread().isInterrupted() || !oVar.isActive()) {
                return new ArrayList();
            }
            ArrayList arrayListJ = j(i10, str2);
            if (arrayListJ.isEmpty()) {
                String strQ = q(i10, str2);
                synchronized (this.f35388l) {
                    try {
                        List list = (List) ((LinkedHashMap) this.f35390n).get(strQ);
                        arrayList = null;
                        boolean z10 = false;
                        if (list != null) {
                            t0Var2 = new t0(new p(new CountDownLatch(0), list), z10);
                        } else {
                            p pVar = (p) this.f35389m.get(strQ);
                            if (pVar != null) {
                                t0Var2 = new t0(pVar, z10);
                            } else {
                                p pVar2 = new p(new CountDownLatch(1), null);
                                this.f35389m.put(strQ, pVar2);
                                t0Var = new t0((Object) pVar2, true);
                            }
                        }
                        t0Var = t0Var2;
                    } finally {
                    }
                }
                if (t0Var.f25329a) {
                    try {
                        xyz.stream.api.o.f().o(i10, str2, new k(this, strQ, t0Var, i10, str2));
                    } catch (Exception e10) {
                        u.a.g(e10, new StringBuilder("getEpgDataFromServer request error: "));
                        d(strQ, (p) t0Var.f25330b, null);
                    }
                    p pVar3 = (p) t0Var.f25330b;
                    try {
                        pVar3.f35374a.await();
                        List list2 = pVar3.f35375b;
                        if (list2 != null) {
                            arrayList = new ArrayList(list2);
                        }
                    } catch (InterruptedException unused) {
                        Thread.currentThread().interrupt();
                    }
                } else {
                    p pVar4 = (p) t0Var.f25330b;
                    try {
                        pVar4.f35374a.await();
                        List list3 = pVar4.f35375b;
                        if (list3 != null) {
                            arrayList = new ArrayList(list3);
                        }
                    } catch (InterruptedException unused2) {
                        Thread.currentThread().interrupt();
                    }
                }
                arrayListJ = arrayList;
            }
            if (Thread.currentThread().isInterrupted() || !oVar.isActive()) {
                return new ArrayList();
            }
            a(arrayListJ, linkedHashMap);
        }
        ArrayList arrayList2 = new ArrayList(linkedHashMap.values());
        Collections.sort(arrayList2, new com.google.android.material.button.c(this, 2));
        return f(str, arrayList2);
    }

    public final void n(int i10, xyz.stream.api.e eVar) {
        int i11;
        synchronized (this.f35378b) {
            i11 = this.f35382f;
        }
        this.f35377a.execute(new xyz.netfly.mobile.player.x(this, i10, i11, eVar, 2));
    }

    /* JADX WARN: Removed duplicated region for block: B:12:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean s(m mVar) {
        boolean z10;
        synchronized (this.f35383g) {
            if (mVar != null) {
                try {
                    z10 = mVar == this.f35386j && mVar.f35370d == this.f35387k;
                } finally {
                }
            }
        }
        return z10;
    }

    /* JADX WARN: Removed duplicated region for block: B:12:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean t(n nVar) {
        boolean z10;
        synchronized (this.f35378b) {
            if (nVar != null) {
                try {
                    z10 = nVar == this.f35381e && nVar.f35372b == this.f35382f;
                } finally {
                }
            }
        }
        return z10;
    }

    public final boolean u(int i10) {
        boolean z10 = true;
        if (i10 == -1) {
            return true;
        }
        synchronized (this.f35383g) {
            if (i10 != this.f35387k) {
                z10 = false;
            }
        }
        return z10;
    }

    public final boolean v(int i10) {
        boolean z10;
        synchronized (this.f35378b) {
            z10 = i10 == this.f35382f;
        }
        return z10;
    }

    public final void x(n nVar, LiveEpgEntity liveEpgEntity) {
        List<xyz.stream.api.e> list;
        synchronized (this.f35379c) {
            list = (List) this.f35379c.remove(nVar.f35373c);
        }
        if (list == null) {
            return;
        }
        for (xyz.stream.api.e eVar : list) {
            if (v(nVar.f35372b)) {
                eVar.onSuccess(liveEpgEntity);
            }
        }
    }

    public final void y() {
        synchronized (this.f35383g) {
            try {
                if (this.f35386j == null && !this.f35384h.isEmpty()) {
                    m mVar = (m) this.f35384h.remove(0);
                    this.f35386j = mVar;
                    if (this.f35385i == null || this.f35385i.isShutdown() || this.f35385i.isTerminated()) {
                        this.f35385i = Executors.newSingleThreadExecutor();
                    }
                    this.f35385i.execute(new com.tekartik.sqflite.g(this, mVar, 24));
                }
            } finally {
            }
        }
    }

    public final void z() {
        n nVar;
        ScheduledExecutorService scheduledExecutorService;
        synchronized (this.f35378b) {
            try {
                if (this.f35381e != null) {
                    return;
                }
                Iterator it = this.f35378b.values().iterator();
                if (it.hasNext()) {
                    nVar = (n) it.next();
                    it.remove();
                    this.f35381e = nVar;
                } else {
                    nVar = null;
                }
                if (nVar != null) {
                    synchronized (this) {
                        try {
                            if (this.f35380d == null || this.f35380d.isShutdown() || this.f35380d.isTerminated()) {
                                this.f35380d = Executors.newSingleThreadScheduledExecutor();
                            }
                            scheduledExecutorService = this.f35380d;
                        } catch (Throwable th) {
                            throw th;
                        }
                    }
                    scheduledExecutorService.schedule(new com.tekartik.sqflite.g(this, nVar, 25), 1L, TimeUnit.SECONDS);
                }
            } finally {
            }
        }
    }
}
