package pirateboat.torrent;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import lombok.extern.slf4j.Slf4j;
import pirateboat.utilities.HttpHelper;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Slf4j
public class PirateBay extends HttpUser implements TorrentSearchEngine {

    PirateBay(HttpHelper httpHelper) {
        super(httpHelper);
    }

    @Override
    public List<Torrent> searchTorrents(String searchName) {

        CopyOnWriteArrayList<Torrent> torrentList = new CopyOnWriteArrayList<>();

        String resultString = null;
        resultString = httpHelper.getPage(buildSearchUrl(searchName), null, "lw=s");

        torrentList.addAll(parseTorrentsOnResultPage(resultString, searchName));

        // sort the findings
        torrentList.sort(TorrentHelper.torrentSorter);

        return torrentList;
    }

    private String buildSearchUrl(String searchName) {
        return String.format(getBaseUrl() + "/q.php?q=%s&cat=", URLEncoder.encode(searchName, StandardCharsets.UTF_8));
    }

    @Override
    public String getBaseUrl() {
        return "https://apibay.org";
    }

    private List<Torrent> parseTorrentsOnResultPage(String pageContent, String searchName) {

        ArrayList<Torrent> torrentList = new ArrayList<>();

        try {
            JsonElement jsonRoot = JsonParser.parseString(pageContent);
            JsonArray listOfTorrents = jsonRoot.getAsJsonArray();

            listOfTorrents.forEach(jsonElement -> {
                Torrent tempTorrent = new Torrent(toString());
                final JsonObject jsonObject = jsonElement.getAsJsonObject();
                tempTorrent.name = jsonObject.get("name").getAsString();
                tempTorrent.magnetUri = TorrentHelper.buildMagnetUriFromHash(jsonObject.get("info_hash").getAsString(), tempTorrent.name);
                tempTorrent.seeder = jsonObject.get("seeders").getAsInt();
                tempTorrent.leecher = jsonObject.get("leechers").getAsInt();
                tempTorrent.lsize = jsonObject.get("size").getAsLong() / 1024.0f / 1024.0f;
                TorrentHelper.evaluateRating(tempTorrent, searchName);
                if (TorrentHelper.isValidTorrent(tempTorrent)) {
                    torrentList.add(tempTorrent);
                }
            });
        } catch (JsonSyntaxException e) {
            log.error("coudn't extract torrent ", e);
        }
        return torrentList;
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }
}

@@ -695,126 +695,14 @@
     
-CopyOnWriteArrayList%3CTorrent%3E torrentList = new CopyOnWriteArrayList%3C%3E();%0A%0A        String resultString = null;%0A       
+String
  res
@@ -788,26 +788,78 @@
     
-torrentList.addAll
+CopyOnWriteArrayList%3CTorrent%3E torrentList = new CopyOnWriteArrayList%3C%3E
 (par
@@ -1118,24 +1118,11 @@
 mat(
-getBaseUrl() + %22
+%22%25s
 /q.p
@@ -1135,16 +1135,30 @@
 s&cat=%22,
+ getBaseUrl(),
  URLEnco
@@ -1541,24 +1541,70 @@
 geContent);%0A
+            if (jsonRoot.isJsonArray()) %7B%0A    
             
@@ -1653,25 +1653,28 @@
 sonArray();%0A
-%0A
+    
             
@@ -1725,24 +1725,28 @@
             
+   
+ 
 Torrent temp
@@ -1776,24 +1776,28 @@
 oString());%0A
+    
             
@@ -1869,32 +1869,36 @@
                 
+    
 tempTorrent.name
@@ -1946,32 +1946,36 @@
                 
+ 
+   
 tempTorrent.magn
@@ -2089,32 +2089,36 @@
                 
+    
 tempTorrent.seed
@@ -2152,32 +2152,36 @@
 s%22).getAsInt();%0A
+    
                 
@@ -2249,32 +2249,36 @@
                 
+    
 tempTorrent.lsiz
@@ -2349,24 +2349,28 @@
             
+    
 TorrentHelpe
@@ -2424,24 +2424,28 @@
             
+  
+  
 if (TorrentH
@@ -2493,32 +2493,36 @@
                 
+    
 torrentList.add(
@@ -2543,32 +2543,36 @@
                 
+    
 %7D%0A            %7D)
@@ -2573,12 +2573,31 @@
     
+ 
+   
 %7D);%0A
+            %7D%0A%0A
     
@@ -2629,16 +2629,40 @@
 ception 
+%7C IllegalStateException 
 e) %7B%0A   
@@ -2685,11 +2685,17 @@
 or(%22
+%5B%7B%7D%5D 
 cou
+l
 dn't
@@ -2714,13 +2714,39 @@
 rent
- %22, e
+: %7B%7D %22, this, e.getStackTrace()
 );%0A 

fix exception handling in PB