#include "http_curl.h"
#include <curl/curl.h>
#include <json-c/json.h>
#include <json-c/json_util.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *url_encode(char *s) { return curl_easy_escape(NULL, s, 0); }

char *web_search_news(char *q) {
  const int MAX_RETRIES = 3;
  for (int retry = 0; retry < MAX_RETRIES; retry++) {
    char *news = malloc(4096);
    if (!news)
      return NULL;
    news[0] = 0;
    char *q_encoded = url_encode(q);
    snprintf(
        news, 4096,
        "https://search.molodetz.nl/"
        "search?q=%s&format=json&categories=news&preferences=eJx1WMuy4zYO_"
        "Zp4o4ormaRqahZepZL5gexVFAlLiEhCzYdt3a8PoIdFWt2Ldl8fkCCIxwForRL0FBDirQc"
        "PQdmLVb7PqoebyokulrSycPP2Il81uclCgtvlrh6oybcBItkHhNsFHW9qp0Cv-"
        "faXshEuDtJA5vb_P_--RHWHCCro4fbLJQ3g4BZRdF1YQbYptqzLw7NNqtt2G8JDOyn-"
        "eqXQX9ZtbUyz3UzU4BOEVlnsveO_t_3KPJTXYNrt3BX9liHMLfo2YWIFK4j-"
        "jh4Ta9WBrN3QdZ8YplcvzazKgk63v0OGy0BphDneDNwV3-CSg23vFJxKCX1_"
        "mwKkNF8MRtVZtgJ8j57d_L9e9W0bSaOyjQOD6qf__"
        "MG366CJ7OCxbRdHxhptHmiA2nb571OmskGWuRxRi8hOfFJj0edXMyk9ijpWm0TmvYqN3As"
        "f0LZ3tOtJ09g4DIFCibGfG_"
        "6UU0K52CuOvFGbSo8vis0TR9yADi3Kv8LaDlOX9QhpX5IM9v2hcvHDVWt9NVBs0xN78Q4B"
        "OI7bTg5QjAyyCzUKztgMU7M6rXCegJxQDMknAwbgi9Pk7SYDMamEvIA4IQMjEOiJ5vhu2G"
        "T519NZfSFbzS3MLmS1BQf-BCWHHGet3nZq4uX8KdY4-"
        "gcnccyx6tdXYcLdBBJzdydyDps0qOS4usplAThR6J6eKkBjMHAGSy6v_"
        "rwH9CMqXW6Y58IJPeDXwPVQIFw5qtvjQQY6CP32lWXAgSS3fycyAZQptxP1fNXJqlmyKx4"
        "XKCWOHgilU7muIEjNCO2UkRhUF5R8bEcOnPEQVsevALzeycNK4lWKZfmOznSHYej6HArF6"
        "FVxPnr-EynH72N7VnHEAlPJXOj5B-NAxzFcHEGFuRG-"
        "jVhc31IXE1zDbjfTp9KT8nudTbtT_"
        "azUsY3MHKGsGprAb4ccpwrICoFqKMBEhQkrW2Dc65TLb2XBcwEcoirHp9xxDT-2_"
        "QGMwXSiu0ApMVETx5SgSA0mS51jvE4zN4793loZM0tKubw5drEMfx7IwglfbZKsbOTjjTr"
        "lE-omat6lQskeicaZEnGMRvH17ook9-TlKr_vlqp0EcaLE-"
        "glTQ91JhvwxyouYoDx7L4Nr3y35PyEltKxP-Ru7sHtOTEBhJS7MtyLU3n1yJufT-"
        "gK0az0fD46ZMcdqVhmuRdEDn2JPemFI3mOZRNnT34WTtmN4nwbQ237Cp3OWuETQ8ZHL5lX"
        "LHwwlS0da7vonJ2zc1U1R2VEykHDkN9pOvbXnva8u4CvWy4PMC_"
        "lTRAm3aYcYVzJ3bVJFs1LBU6TsgeHFz7K-Ko4llo6VqyVmw4GEGUbbRfLeCT4_"
        "H721oJWbl2Qk_"
        "u6qSuVSSHzkBRL1tcD3Efi8eXtQW0pmzsnPygsd3NHfRrJnRLk7lmt4eEG4QORtjyXGPsf"
        "uczYhWWjNH3DI5LMV7gQd7GehKmbIe_"
        "NRBSymdvgVawUnxqVKpce3bREefaCubIU0uyICdEXCXy3qMeS63k5ljTeCz3tbYKb2mHj2"
        "qTKA7a2dYrmhlfx3LBTRDf8O_Q0bOcth49stoplzwDn5mYnwZWKaq5dV-"
        "QI4Ucy7gDpRzLRzK44i79-freYty1bY6M7b_Nc4LH0hkghBeWj5SZc-o9D0z-"
        "q0DrFI6ch_wOz3-JBxYF5-jsrzN49HL6WxD9C6-hrqArAPZkP6-"
        "N5JIJ9npDqUjw1Sf5FSBXZ7zLuepxRasvwWh4TD2CJZ7p9sJuM8PexaOKXhHS7TYrymoFY"
        "1tAkhFJky_L9ulzouNeEMt10qojIRMxNkUvCyAi0J_-7c5d3Ptr5npuFbLldY7hfo1_z_"
        "HhCHEbmzomVxcXmaafVb08mhlLnAtS1sUKnMlrhU8UExZNX0zF5xXKqD9gPqTGqIoX1Zm6"
        "OWGde5G6rdl8czYkyc3qdNPxU0CMxyd4tPfdWEcfcZZ_"
        "y3pUyz345vhMnChNO8igujySLhufvsFbV96aNY3H2kYfhOJTPGjVQdbcFqB05U_6YEd7I-"
        "_Gj0DItSsIWyx7oqkfmE7v5k4Y7ojF-"
        "gtXxAnzLVLt6mZaWtn1C9xmqgsWyaoCVpdLkllGv1vCg-cPLggp1cTleTwlVCk9pVQo_"
        "3tWlaInbEb4n2XtQjl_eQ92jUKcv8pVtjkcpxw-w5k2GphT_-ttv_30dmiN8eeWq_"
        "cvkWCJePSTpypyoG_OTgvE4FgMcn62reuRul-QBKYo-yPSprB34CP_"
        "hQ8c9vTolpXDFYvbldvCorr4A54Cs8HnKWeDP9lnpS7_vPyIcP7RMNvPYF2-"
        "k2vUnpGfgsl86aUzsy7XeKLQ8IunxPSzu28QzPJW08nNO4EA91uFJWZ05VhSWsuQ3Dbc4F"
        "qVtHrb3Fv2d5BSp11XZhZ8WzP-3fwFOiVFV",
        q_encoded);
    free(q_encoded);

    char *ret = curl_get(news);
    free(news);
    if (!ret)
      continue;

    json_object *json_ret = json_tokener_parse(ret);
    if (!json_ret) {
      free(ret);
      continue;
    }

    json_object *json_results = json_object_object_get(json_ret, "results");
    json_object *json_result = json_object_array_get_idx(json_results, 0);
    if (json_result) {
      json_object_put(json_ret);
      return ret;
    }
    json_object_put(json_ret);
    free(ret);
  }
  return NULL;
}

char *web_search(char *q) {
  const int MAX_RETRIES = 3;
  for (int retry = 0; retry < MAX_RETRIES; retry++) {
    char *news = malloc(4096);
    if (!news)
      return NULL;
    news[0] = 0;
    char *q_encoded = url_encode(q);
    snprintf(
        news, 4096,
        "https://search.molodetz.nl/"
        "search?q=%s&format=json&preferences=eJx1WMuy4zYO_"
        "Zp4o4ormaRqahZepZL5gexVFAlLiEhCzYdt3a8PoIdFWt2Ldl8fkCCIxwForRL0FBDirQc"
        "PQdmLVb7PqoebyokulrSycPP2Il81uclCgtvlrh6oybcBItkHhNsFHW9qp0Cv-"
        "faXshEuDtJA5vb_P_--RHWHCCro4fbLJQ3g4BZRdF1YQbYptqzLw7NNqtt2G8JDOyn-"
        "eqXQX9ZtbUyz3UzU4BOEVlnsveO_t_3KPJTXYNrt3BX9liHMLfo2YWIFK4j-"
        "jh4Ta9WBrN3QdZ8YplcvzazKgk63v0OGy0BphDneDNwV3-CSg23vFJxKCX1_"
        "mwKkNF8MRtVZtgJ8j57d_L9e9W0bSaOyjQOD6qf__"
        "MG366CJ7OCxbRdHxhptHmiA2nb571OmskGWuRxRi8hOfFJj0edXMyk9ijpWm0TmvYqN3As"
        "f0LZ3tOtJ09g4DIFCibGfG_"
        "6UU0K52CuOvFGbSo8vis0TR9yADi3Kv8LaDlOX9QhpX5IM9v2hcvHDVWt9NVBs0xN78Q4B"
        "OI7bTg5QjAyyCzUKztgMU7M6rXCegJxQDMknAwbgi9Pk7SYDMamEvIA4IQMjEOiJ5vhu2G"
        "T519NZfSFbzS3MLmS1BQf-BCWHHGet3nZq4uX8KdY4-"
        "gcnccyx6tdXYcLdBBJzdydyDps0qOS4usplAThR6J6eKkBjMHAGSy6v_"
        "rwH9CMqXW6Y58IJPeDXwPVQIFw5qtvjQQY6CP32lWXAgSS3fycyAZQptxP1fNXJqlmyKx4"
        "XKCWOHgilU7muIEjNCO2UkRhUF5R8bEcOnPEQVsevALzeycNK4lWKZfmOznSHYej6HArF6"
        "FVxPnr-EynH72N7VnHEAlPJXOj5B-NAxzFcHEGFuRG-"
        "jVhc31IXE1zDbjfTp9KT8nudTbtT_"
        "azUsY3MHKGsGprAb4ccpwrICoFqKMBEhQkrW2Dc65TLb2XBcwEcoirHp9xxDT-2_"
        "QGMwXSiu0ApMVETx5SgSA0mS51jvE4zN4793loZM0tKubw5drEMfx7IwglfbZKsbOTjjTr"
        "lE-omat6lQskeicaZEnGMRvH17ook9-TlKr_vlqp0EcaLE-"
        "glTQ91JhvwxyouYoDx7L4Nr3y35PyEltKxP-Ru7sHtOTEBhJS7MtyLU3n1yJufT-"
        "gK0az0fD46ZMcdqVhmuRdEDn2JPemFI3mOZRNnT34WTtmN4nwbQ237Cp3OWuETQ8ZHL5lX"
        "LHwwlS0da7vonJ2zc1U1R2VEykHDkN9pOvbXnva8u4CvWy4PMC_"
        "lTRAm3aYcYVzJ3bVJFs1LBU6TsgeHFz7K-Ko4llo6VqyVmw4GEGUbbRfLeCT4_"
        "H721oJWbl2Qk_"
        "u6qSuVSSHzkBRL1tcD3Efi8eXtQW0pmzsnPygsd3NHfRrJnRLk7lmt4eEG4QORtjyXGPsf"
        "uczYhWWjNH3DI5LMV7gQd7GehKmbIe_"
        "NRBSymdvgVawUnxqVKpce3bREefaCubIU0uyICdEXCXy3qMeS63k5ljTeCz3tbYKb2mHj2"
        "qTKA7a2dYrmhlfx3LBTRDf8O_Q0bOcth49stoplzwDn5mYnwZWKaq5dV-"
        "QI4Ucy7gDpRzLRzK44i79-freYty1bY6M7b_Nc4LH0hkghBeWj5SZc-o9D0z-"
        "q0DrFI6ch_wOz3-JBxYF5-jsrzN49HL6WxD9C6-hrqArAPZkP6-"
        "N5JIJ9npDqUjw1Sf5FSBXZ7zLuepxRasvwWh4TD2CJZ7p9sJuM8PexaOKXhHS7TYrymoFY"
        "1tAkhFJky_L9ulzouNeEMt10qojIRMxNkUvCyAi0J_-7c5d3Ptr5npuFbLldY7hfo1_z_"
        "HhCHEbmzomVxcXmaafVb08mhlLnAtS1sUKnMlrhU8UExZNX0zF5xXKqD9gPqTGqIoX1Zm6"
        "OWGde5G6rdl8czYkyc3qdNPxU0CMxyd4tPfdWEcfcZZ_"
        "y3pUyz345vhMnChNO8igujySLhufvsFbV96aNY3H2kYfhOJTPGjVQdbcFqB05U_6YEd7I-"
        "_Gj0DItSsIWyx7oqkfmE7v5k4Y7ojF-"
        "gtXxAnzLVLt6mZaWtn1C9xmqgsWyaoCVpdLkllGv1vCg-cPLggp1cTleTwlVCk9pVQo_"
        "3tWlaInbEb4n2XtQjl_eQ92jUKcv8pVtjkcpxw-w5k2GphT_-ttv_30dmiN8eeWq_"
        "cvkWCJePSTpypyoG_OTgvE4FgMcn62reuRul-QBKYo-yPSprB34CP_"
        "hQ8c9vTolpXDFYvbldvCorr4A54Cs8HnKWeDP9lnpS7_vPyIcP7RMNvPYF2-"
        "k2vUnpGfgsl86aUzsy7XeKLQ8IunxPSzu28QzPJW08nNO4EA91uFJWZ05VhSWsuQ3Dbc4F"
        "qVtHrb3Fv2d5BSp11XZhZ8WzP-3fwFOiVFV",
        q_encoded);
    free(q_encoded);

    char *ret = curl_get(news);
    free(news);
    if (!ret)
      continue;

    json_object *json_ret = json_tokener_parse(ret);
    if (!json_ret) {
      free(ret);
      continue;
    }

    json_object *json_results = json_object_object_get(json_ret, "results");
    json_object *json_result = json_object_array_get_idx(json_results, 0);
    if (json_result) {
      json_object_put(json_ret);
      return ret;
    }
    json_object_put(json_ret);
    free(ret);
  }
  return NULL;
}

char *web_search_engine(char *q) {
  const int MAX_RETRIES = 3;
  for (int retry = 0; retry < MAX_RETRIES; retry++) {
    char *searx = malloc(4096);
    if (!searx)
      return NULL;
    searx[0] = 0;
    snprintf(searx, 4096, "https://searx.molodetz.nl/search?q=%s&format=json",
             q);
    char *ret = curl_get(searx);
    free(searx);
    if (!ret)
      continue;

    json_object *json_ret = json_tokener_parse(ret);
    if (!json_ret) {
      free(ret);
      continue;
    }

    json_object *json_results = json_object_object_get(json_ret, "results");
    json_object *json_result = json_object_array_get_idx(json_results, 0);
    if (json_result) {
      json_object_put(json_ret);
      return ret;
    }
    json_object_put(json_ret);
    free(ret);
  }
  return NULL;
}
