package me.ryanw.overcast.impl;

import me.ryanw.overcast.api.OvercastPlayer;
import me.ryanw.overcast.impl.object.ParsedPlayer;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class OvercastAPI {

    private static String userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36";
    private static int connectionTimeout = 3000;

    public static OvercastPlayer getPlayer(String username) throws IOException {
        String url = "https://oc.tc/users/" + username;
        Connection.Response response = Jsoup.connect(url).method(Connection.Method.GET).userAgent(userAgent).timeout(connectionTimeout).execute();
        if (response.statusCode() != 200) return null;
        return new ParsedPlayer(response.parse());
    }

    public static OvercastPlayer getPlayerByUrl(String url) throws IOException {
        Connection.Response response = Jsoup.connect(url).method(Connection.Method.GET).userAgent(userAgent).timeout(connectionTimeout).execute();
        if (response.statusCode() != 200) return null;
        return new ParsedPlayer(response.parse());
    }

    public static List<OvercastPlayer> getPlayers(List<String> usernames) throws IOException {
        List<OvercastPlayer> playerList = new ArrayList<OvercastPlayer>();
        for (String username : usernames) {
            String url = "https://oc.tc/users/" + username;
            Connection.Response response = Jsoup.connect(url).method(Connection.Method.GET).userAgent(userAgent).timeout(connectionTimeout).execute();
            if (response.statusCode() != 200) break;
            playerList.add(new ParsedPlayer(response.parse()));
        }
        return playerList;
    }

    public static List<OvercastPlayer> getPlayersByUrl(List<String> urls) throws IOException {
        List<OvercastPlayer> playerList = new ArrayList<OvercastPlayer>();
        for (String url : urls) {
            Connection.Response response = Jsoup.connect(url).method(Connection.Method.GET).userAgent(userAgent).timeout(connectionTimeout).execute();
            if (response.statusCode() != 200) break;
            playerList.add(new ParsedPlayer(response.parse()));
        }
        return playerList;
    }
}

@@ -147,24 +147,62 @@
 Connection;%0A
+import org.jsoup.HttpStatusException;%0A
 import org.j
@@ -582,35 +582,16 @@
 sername)
- throws IOException
  %7B%0A     
@@ -633,32 +633,50 @@
 s/%22 + username;%0A
+        try %7B%0A    
         Connecti
@@ -818,104 +818,115 @@
     
-if (response.statusCode() != 200) return null;%0A        return new ParsedPlayer(response.parse())
+    return new ParsedPlayer(response.parse());%0A        %7D catch (IOException ignored) %7B%7D%0A        return null
 ;%0A  
@@ -990,37 +990,37 @@
 ng url) 
-throws IOException %7B%0A
+ %7B%0A        try %7B%0A    
         
@@ -1170,104 +1170,115 @@
     
-if (response.statusCode() != 200) return null;%0A        return new ParsedPlayer(response.parse())
+    return new ParsedPlayer(response.parse());%0A        %7D catch (IOException ignored) %7B%7D%0A        return null
 ;%0A  
@@ -1355,35 +1355,16 @@
 ernames)
- throws IOException
  %7B%0A     
@@ -1529,32 +1529,54 @@
 s/%22 + username;%0A
+            try %7B%0A    
             Conn
@@ -1714,36 +1714,74 @@
 ();%0A            
-if (
+    System.out.println(username + %22 - %22 + 
 response.statusC
@@ -1785,32 +1785,23 @@
 usCode()
- != 200) break;%0A
+);%0A    
         
@@ -1847,32 +1847,157 @@
 ponse.parse()));
+%0A%0A            %7D catch (HttpStatusException ignored) %7B%7D catch (NullPointerException ignored) %7B%7D catch (IOException ignored) %7B%7D
 %0A        %7D%0A     
@@ -2102,27 +2102,8 @@
 rls)
- throws IOException
  %7B%0A 
@@ -2202,32 +2202,54 @@
 g url : urls) %7B%0A
+            try %7B%0A    
             Conn
@@ -2391,57 +2391,8 @@
     
-        if (response.statusCode() != 200) break;%0A
     
@@ -2447,24 +2447,148 @@
 .parse()));%0A
+            %7D catch (HttpStatusException ignored) %7B%7D catch (NullPointerException ignored) %7B%7D catch (IOException ignored) %7B%7D%0A
         %7D%0A  

Fix bad response in getPlayers() throwing exception, stopping the loop, Closes #18