package com.google.step.YOUR_PROJECT_NAME_HERE.external;

import com.google.step.YOUR_PROJECT_NAME_HERE.data.Card;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public final class W3SchoolClient {
  private static final String CARD_TITLE_ID = "h1";
  private static final String CARD_DESC_ID = "p";
  private static final String CARD_SNIPPET_ID = "w3-example";
  private static final String CARD_CODE_ID = "w3-code";

  public Card search(String w3Link) {

    Document doc;
    try {
      doc = Jsoup.connect(w3Link).get();
      Elements titles = doc.getElementsByTag(CARD_TITLE_ID);
      Elements descriptions = doc.getElementsByTag(CARD_DESC_ID);
      Elements snippets = doc.getElementsByClass(CARD_SNIPPET_ID);

      String title = "";
      String description = "";
      String code = "";

      if (!titles.isEmpty() && !descriptions.isEmpty() && !snippets.isEmpty()) {
        title = titles.first().text();
        description = descriptions.first().text();
        code = snippets.first().getElementsByClass(CARD_CODE_ID).text();
      }
      if (!title.isEmpty() && !description.isEmpty() && !code.isEmpty()) {
        return new Card(title, code, w3Link, description);
      }
      return null;
    } catch (IOException e) {
      return null;
    }
  }
}

@@ -523,17 +523,16 @@
 Link) %7B%0A
-%0A
     Docu
@@ -797,82 +797,201 @@
     
-String title = %22%22;%0A      String description = %22%22;%0A      String code = %22%22;%0A
+if (titles.isEmpty() %7C%7C titles.first().text().isEmpty()) %7B%0A        return null;%0A      %7D%0A      if (descriptions.isEmpty() %7C%7C descriptions.first().text().isEmpty()) %7B%0A        return null;%0A      %7D
 %0A   
@@ -997,22 +997,23 @@
     if (
-!title
+snippet
 s.isEmpt
@@ -1019,69 +1019,133 @@
 ty()
- && !descriptions.isEmpty() && !snippets.isEmpty()) %7B%0A       
+%0A          %7C%7C snippets.first().getElementsByClass(CARD_CODE_ID).text().isEmpty()) %7B%0A        return null;%0A      %7D%0A      String
  tit
@@ -1178,17 +1178,22 @@
 ;%0A      
- 
+String
  descrip
@@ -1234,17 +1234,22 @@
 ;%0A      
- 
+String
  code = 
@@ -1310,93 +1310,9 @@
 ();%0A
-      %7D%0A      if (!title.isEmpty() && !description.isEmpty() && !code.isEmpty()) %7B%0A  
+%0A
     
@@ -1368,35 +1368,8 @@
 n);%0A
-      %7D%0A      return null;%0A
     

Refactor conditional field checks