package xyz.stream.api.viewmodel;

import android.app.Activity;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
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.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import xyz.stream.api.entity.LeagueEntity;
import xyz.stream.api.entity.MatchChannelEntity;
import xyz.stream.api.entity.MatchEntity;
import xyz.stream.utils.f0;

/* JADX INFO: loaded from: classes3.dex */
public abstract class BaseSportsViewModel extends ApiViewModel {
    public List<LeagueEntity> cacheLeagueList;
    public androidx.lifecycle.v liveCollectionList;
    public androidx.lifecycle.v liveLeagueList;
    public androidx.lifecycle.v liveMatchList;
    public androidx.lifecycle.v livePreviewMatchList;
    public MatchEntity selectedMatch;
    private final ExecutorService singleThreadExecutor = Executors.newFixedThreadPool(10);
    public Map<String, List<LeagueEntity>> collectionLeagueMap = new LinkedHashMap();
    public Map<String, String> sportTypeMap = new HashMap();

    /* JADX INFO: Access modifiers changed from: private */
    public void buildCollectionLeagueMap(List<LeagueEntity> list) {
        HashMap map = new HashMap();
        this.sportTypeMap.clear();
        if (list != null) {
            for (LeagueEntity leagueEntity : list) {
                if (leagueEntity != null && leagueEntity.getSportType() != null && !leagueEntity.getSportType().isEmpty()) {
                    String sportType = leagueEntity.getSportType();
                    if (!map.containsKey(sportType)) {
                        map.put(sportType, new ArrayList());
                        this.sportTypeMap.put(sportType, leagueEntity.getDisplayName());
                    }
                    List list2 = (List) map.get(sportType);
                    Objects.requireNonNull(list2);
                    list2.add(leagueEntity);
                }
            }
        }
        ArrayList<Map.Entry> arrayList = new ArrayList(map.entrySet());
        Collections.sort(arrayList, new androidx.browser.trusted.b(12));
        this.collectionLeagueMap = new LinkedHashMap();
        for (Map.Entry entry : arrayList) {
            this.collectionLeagueMap.put((String) entry.getKey(), (List) entry.getValue());
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void fetchPreviewMatchList(androidx.lifecycle.w wVar) {
        xyz.stream.api.o.f().r(null, false, new l(this, wVar, 1));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ int lambda$buildCollectionLeagueMap$2(Map.Entry entry, Map.Entry entry2) {
        return ((List) entry2.getValue()).size() - ((List) entry.getValue()).size();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$getCollectionList$0(int i10, int i11, androidx.lifecycle.w wVar) {
        ArrayList arrayListD = xyz.stream.api.d.b().d(11, i10, i11);
        if (arrayListD == null) {
            arrayListD = new ArrayList();
        }
        wVar.j(arrayListD);
        xyz.stream.api.d.b().h(11, i10, i11);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ int lambda$sortMatchMapByDate$1(SimpleDateFormat simpleDateFormat, String str, String str2) {
        try {
            Date date = simpleDateFormat.parse(str);
            Date date2 = simpleDateFormat.parse(str2);
            if (date != null && date2 != null) {
                return date.compareTo(date2);
            }
        } catch (Exception unused) {
        }
        return str.compareTo(str2);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public Map<String, List<MatchEntity>> sortMatchMapByDate(Map<String, List<MatchEntity>> map) {
        if (map != null && !map.isEmpty()) {
            try {
                ArrayList<String> arrayList = new ArrayList(map.keySet());
                Collections.sort(arrayList, new com.google.android.material.button.c(new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()), 4));
                LinkedHashMap linkedHashMap = new LinkedHashMap();
                for (String str : arrayList) {
                    linkedHashMap.put(str, map.get(str));
                }
                return linkedHashMap;
            } catch (Exception unused) {
            }
        }
        return map;
    }

    public List<LeagueEntity> applySavedLeagueOrder(List<LeagueEntity> list) {
        if (list == null || list.isEmpty()) {
            return new ArrayList();
        }
        ArrayList arrayListM = f0.o().m(Integer.class, "sp_sports_league_order");
        if (arrayListM.isEmpty()) {
            return new ArrayList(list);
        }
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        for (LeagueEntity leagueEntity : list) {
            if (leagueEntity != null) {
                linkedHashMap.put(leagueEntity.getId(), leagueEntity);
            }
        }
        ArrayList arrayList = new ArrayList();
        Iterator it = arrayListM.iterator();
        while (it.hasNext()) {
            LeagueEntity leagueEntity2 = (LeagueEntity) linkedHashMap.remove((Integer) it.next());
            if (leagueEntity2 != null) {
                arrayList.add(leagueEntity2);
            }
        }
        arrayList.addAll(linkedHashMap.values());
        return arrayList;
    }

    public void clickMatchItem(Activity activity, MatchEntity matchEntity) {
        ArrayList arrayList = new ArrayList();
        this.selectedMatch = matchEntity;
        if ((matchEntity.getState().intValue() == 1 || this.selectedMatch.getState().intValue() == 2) && this.selectedMatch.getChannelList() != null) {
            for (MatchEntity.ChannelListEntity channelListEntity : this.selectedMatch.getChannelList()) {
                arrayList.add(new MatchChannelEntity(channelListEntity.getChannelId().intValue(), channelListEntity.getChannelName(), channelListEntity.getPictureUrl()));
            }
        } else if (this.selectedMatch.getPlaybackList() != null) {
            for (MatchEntity.PlaybackListEntity playbackListEntity : this.selectedMatch.getPlaybackList()) {
                arrayList.add(new MatchChannelEntity(playbackListEntity.getId().intValue(), playbackListEntity.getChannelName(), null));
            }
        }
        if (arrayList.isEmpty()) {
            showNoVideoSource(activity);
        } else if (arrayList.size() > 1) {
            showChoiceChannelDialog(activity, arrayList);
        } else {
            handlePressMatch(activity, ((MatchChannelEntity) arrayList.get(0)).getId());
        }
    }

    public void getCollectionList(int i10, int i11) {
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.liveCollectionList = wVar;
        this.singleThreadExecutor.execute(new a(i10, i11, 1, wVar));
    }

    public abstract int getLeagueIcon(int i10);

    public void getLeagueList() {
        boolean z10;
        ArrayList arrayList;
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.liveLeagueList = wVar;
        xyz.stream.api.r rVarA = xyz.stream.api.r.a();
        synchronized (rVarA) {
            z10 = !rVarA.f35933a.isEmpty();
        }
        if (z10) {
            xyz.stream.api.r rVarA2 = xyz.stream.api.r.a();
            synchronized (rVarA2) {
                arrayList = new ArrayList(rVarA2.f35933a);
            }
            this.cacheLeagueList = new ArrayList(applySavedLeagueOrder(arrayList));
            xyz.stream.api.r.a().b(this.cacheLeagueList);
            buildCollectionLeagueMap(this.cacheLeagueList);
            wVar.j(this.cacheLeagueList);
            return;
        }
        List<LeagueEntity> list = this.cacheLeagueList;
        if (list != null) {
            this.cacheLeagueList = applySavedLeagueOrder(list);
            xyz.stream.api.r.a().b(this.cacheLeagueList);
            buildCollectionLeagueMap(this.cacheLeagueList);
            wVar.j(this.cacheLeagueList);
            return;
        }
        xyz.stream.api.o oVarF = xyz.stream.api.o.f();
        l lVar = new l(this, wVar, 2);
        oVarF.getClass();
        HashMap map = new HashMap();
        xyz.stream.api.o.g("sports/football/leagues", oVarF.n(), map, new xyz.stream.api.m(oVarF, "sports/football/leagues", map, lVar, 1));
    }

    public void getMatchList(String str) {
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.liveMatchList = wVar;
        xyz.stream.api.o oVarF = xyz.stream.api.o.f();
        if (str == null) {
            str = "";
        }
        oVarF.r(str, true, new l(this, wVar, 0));
    }

    public void getPreviewMatchList() {
        androidx.lifecycle.w wVar = new androidx.lifecycle.w();
        this.livePreviewMatchList = wVar;
        re.a aVarO = xyz.stream.api.live.g.o();
        if (aVarO == null) {
            fetchPreviewMatchList(wVar);
        } else {
            aVarO.e(new m(this, wVar));
        }
    }

    public abstract void handlePressMatch(Activity activity, int i10);

    public void saveLeagueOrder(List<LeagueEntity> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        ArrayList arrayList = new ArrayList();
        for (LeagueEntity leagueEntity : list) {
            if (leagueEntity != null) {
                arrayList.add(leagueEntity.getId());
            }
        }
        f0.o().C("sp_sports_league_order", arrayList);
        this.cacheLeagueList = new ArrayList(list);
        xyz.stream.api.r.a().b(this.cacheLeagueList);
        buildCollectionLeagueMap(this.cacheLeagueList);
    }

    public abstract void showChoiceChannelDialog(Activity activity, List<MatchChannelEntity> list);

    public abstract void showNoVideoSource(Activity activity);
}
