package com.merakianalytics.orianna.examples;

import java.util.HashSet;

import com.merakianalytics.orianna.types.common.Queue;
import com.merakianalytics.orianna.types.common.Region;
import com.merakianalytics.orianna.types.common.Season;
import com.merakianalytics.orianna.types.core.match.Match;
import com.merakianalytics.orianna.types.core.match.MatchHistory;
import com.merakianalytics.orianna.types.core.match.Participant;
import com.merakianalytics.orianna.types.core.summoner.Summoner;

public class CollectMatches {
    private static MatchHistory filterMatchHistory(final Summoner summoner) {
        final MatchHistory matchHistory = MatchHistory.forSummoner(summoner).withQueues(Queue.ARAM).withSeasons(Season.SEASON_8).get();
        return matchHistory;
    }

    public static void main(final String[] args) {
        final Summoner summoner = Summoner.named("GustavEnk").withRegion(Region.EUROPE_WEST).get();
        final Region region = summoner.getRegion();

        final HashSet<String> unpulledSummonerIds = new HashSet<>();
        unpulledSummonerIds.add(summoner.getId());
        final HashSet<String> pulledSummonerIds = new HashSet<>();

        final HashSet<Long> unpulledMatchIds = new HashSet<>();
        final HashSet<Long> pulledMatchIds = new HashSet<>();

        while(!unpulledSummonerIds.isEmpty()) {
            // Get a new summoner from our list of unpulled summoners and pull their match history
            final String newSummonerId = unpulledSummonerIds.iterator().next();
            final Summoner newSummoner = Summoner.withId(newSummonerId).withRegion(region).get();
            final MatchHistory matches = filterMatchHistory(newSummoner);
            for(final Match match : matches) {
                if(!pulledMatchIds.contains(match.getId())) {
                    unpulledMatchIds.add(match.getId());
                }
            }
            unpulledSummonerIds.remove(newSummonerId);
            pulledSummonerIds.add(newSummonerId);

            while(!unpulledMatchIds.isEmpty()) {
                // Get a random match from our list of matches
                final long newMatchId = unpulledMatchIds.iterator().next();
                final Match newMatch = Match.withId(newMatchId).withRegion(region).get();
                for(final Participant p : newMatch.getParticipants()) {
                    if(!pulledSummonerIds.contains(p.getSummoner().getId()) && !unpulledSummonerIds.contains(p.getSummoner().getId())) {
                        unpulledSummonerIds.add(p.getSummoner().getId());
                    }
                }
                // The above lines will trigger the match to load its data by iterating over all the participants.
                // If you have a database in your datapipeline, the match will automatically be stored in it.
                unpulledMatchIds.remove(newMatchId);
                pulledMatchIds.add(newMatchId);
            }
        }
    }
}
@@ -1008,32 +1008,31 @@
 ng%3E unpulled
-Summoner
+Account
 Ids = new Ha
@@ -1054,32 +1054,31 @@
     unpulled
-Summoner
+Account
 Ids.add(summ
@@ -1081,24 +1081,31 @@
 summoner.get
+Account
 Id());%0A     
@@ -1135,24 +1135,23 @@
 %3E pulled
-Summoner
+Account
 Ids = ne
@@ -1312,32 +1312,31 @@
 le(!unpulled
-Summoner
+Account
 Ids.isEmpty(
@@ -1467,24 +1467,23 @@
 ring new
-Summoner
+Account
 Id = unp
@@ -1483,32 +1483,31 @@
 d = unpulled
-Summoner
+Account
 Ids.iterator
@@ -1577,24 +1577,23 @@
 thId(new
-Summoner
+Account
 Id).with
@@ -1902,32 +1902,31 @@
     unpulled
-Summoner
+Account
 Ids.remove(n
@@ -1923,32 +1923,31 @@
 s.remove(new
-Summoner
+Account
 Id);%0A       
@@ -1953,32 +1953,31 @@
       pulled
-Summoner
+Account
 Ids.add(newS
@@ -1975,24 +1975,23 @@
 .add(new
-Summoner
+Account
 Id);%0A%0A  
@@ -2364,32 +2364,31 @@
   if(!pulled
-Summoner
+Account
 Ids.contains
@@ -2403,24 +2403,31 @@
 mmoner().get
+Account
 Id()) && !un
@@ -2428,32 +2428,31 @@
 && !unpulled
-Summoner
+Account
 Ids.contains
@@ -2467,24 +2467,31 @@
 mmoner().get
+Account
 Id())) %7B%0A   
@@ -2519,24 +2519,23 @@
 unpulled
-Summoner
+Account
 Ids.add(

Switch to using accountIds for match collection examples instead of matchIds