// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.sps;

import com.google.sps.data.ShoppingQueryInput;
import java.io.IOException;

import org.jsoup.Connection.Response;
import org.jsoup.Connection.Response;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/** 
 * Queries Google Shopping with the given input.
 */
public class GoogleShoppingQuerier {

  // The parameters for {@code GOOGLE_SEARCH_BASE_URL} define the following:
  // "tbm" defines the type of search;
  // "tbs" defines advanced search parameters;
  // "safe" defines the level of filtering for adult content;
  private final String GOOGLE_SEARCH_BASE_URL =
      "https://www.google.com/search?tbm=shop&tbs=vw:l&safe=active&source=h";

//   public String query(String shoppingQuery) throws IOException, HttpStatusException {
  public String query(ShoppingQueryInput shoppingQueryInput) throws IOException, HttpStatusException {
    String shoppingQuery = shoppingQueryInput.getShoppingQuery();

    if (!isValidShoppingQuery(shoppingQuery)) {
      throw new IOException("Invalid Shopping query.");
    }

    shoppingQuery = polishShoppingQuery(shoppingQuery);

    String query = "q=" + shoppingQuery;
    // "num" parameter defines the maximum number of results to return.
    String maxResultsNumber = "num=" + String.valueOf(shoppingQueryInput.getMaxResultsNumber());
      // "hl" parameter defines the language to use for the Google search.
    String language = "hl=" + shoppingQueryInput.getLanguage();
    
    String searchURL = GOOGLE_SEARCH_BASE_URL + "&" + query + "&" + language + "&" + maxResultsNumber;

    Response response = Jsoup.connect(searchURL)
        .userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
        .execute();

    int statusCode = response.statusCode();
    if (statusCode == 200) {
      Document doc = response.parse();
      return doc.html();
    }
    else {
      System.out.println("Received error code : " + statusCode);
      return "";
    }
  }

  private boolean isValidShoppingQuery(String shoppingQuery) {
    if (shoppingQuery.isEmpty()) {
      return false;
    }

    if (shoppingQuery == null) {
      return false;
    }

    // TO DO: Check if the query defines something that cannot be purchased.

    return true;
  }

  /** 
   * Prepares the query input for Google Search and returns it.
   */
  private String polishShoppingQuery(String shoppingQuery) {
    shoppingQuery =
        shoppingQuery
            .replaceAll("\\s+", " ") // Remove duplicate spaces
            .trim() // Remove spaces from the beginning and end of string
            .replaceAll(" ", "%20") // Replace single space with "%20"
            .replaceAll("[-+=,\n._^\";:~#></|!*]", ""); // Remove special characters
    return shoppingQuery;
  }
}

@@ -725,46 +725,8 @@
 se;%0A
-import org.jsoup.Connection.Response;%0A
 impo
@@ -2241,20 +2241,31 @@
 11; 
-Linux
+CrOS
  x86_64
+ 13099.110.0
 ) Ap
@@ -2276,20 +2276,20 @@
 ebKit/53
-5.21
+7.36
  (KHTML,
@@ -2312,19 +2312,21 @@
 ome/
-19.0.1042.0
+84.0.4147.136
  Saf
@@ -2335,12 +2335,12 @@
 i/53
-5.21
+7.36
 %22)%0A 
@@ -2442,40 +2442,156 @@
     
-Document doc = response.parse();
+// Parse the response object to obtain the document.%0A      Document doc = response.parse();%0A%0A      // TODO: Extract product info from the document.%0A
 %0A   

Update shopping querier