#include "HttpRequest.h"
#include "ace/OS.h"
#include "ace/Task.h"
#include "unistd.h"
/*static  size_t header_callback(char *ptr, size_t msize, size_t nmemb, void *stream)
{
  //UNUSED(ptr);
  //UNUSED(size);
  //UNUSED(nmemb);
  //UNUSED(stream);
  const size_t size = msize * nmemb;
  cout << " Header callback obtained size - " << size << endl;
  return size;
}*/
static void*   Newthread(void*);
extern bool html;
HttpRequest *HttpRequest::htt=NULL;
HttpRequest::HttpRequest()
{
   Msisdn.clear();
}

bool HttpRequest::setMsisdn(char * Number)
{
   Msisdn.assign(Number);
}

string HttpRequest::getMsisdn()
{
   return Msisdn;
}

HttpRequest* HttpRequest::instance()
{

   if(htt == NULL)
   {
      htt = new HttpRequest();
   }
   else
   {
      return htt;
   }

   return htt;
}
   void HttpRequest::init()
   {
      struct curl_slist *request_headers = 0;

      if (curl_global_init(CURL_GLOBAL_ALL))
        die("Failed to initialise libcurl");

      curl = curl_easy_init();
      if (!curl)
        die("Failed to create curl handle");

      if (curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback))
      //if (curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, &HttpRequest::header_callback))
        die("Failed to set curl options");
      if (curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, data_callback))
      //if (curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &HttpRequest::data_callback))
        die("Failed to set curl options");
      if (curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer))
        die("Failed to set curl options");
   }


   size_t HttpRequest::data_callback(void *ptr, size_t msize, size_t nmemb, void *stream)
   {
     //UNUSED(ptr);
     //UNUSED(size);
     //UNUSED(nmemb);
     //UNUSED(stream);
     const size_t size = msize * nmemb;
     cout << " Data callback obtained size - " << size << endl;
     //cout << " Data content: " << (char *)ptr << endl;
     cout << " Data content: " << ptr << endl;
     //cout << " Data content stream : " << stream << endl;
     return size;
   }

   size_t HttpRequest::header_callback(char *ptr, size_t msize, size_t nmemb, void *stream)
   {
     //UNUSED(ptr);
     //UNUSED(size);
     //UNUSED(nmemb);
     //UNUSED(stream);
     const size_t size = msize * nmemb;
     cout << " Header callback obtained size - " << size << endl;
     cout << " Header content: " << ptr << endl;
     //cout << " Msisdn Nu - " << getMsisdn() << endl;
     return size;
   }

   void HttpRequest::die(const char *error)
   {
     fprintf(stderr, "httplint: %s\n", error);
     exit(EXIT_FAILURE);
   }


   bool HttpRequest::cleanup()
   {
      curl_global_cleanup();      
   }

   void HttpRequest::check_url(const char *url)
   {
  int i, r;
  CURLcode code;

  start = true;
  /*(for (i = 0; i != sizeof header_table / sizeof header_table[0]; i++)
    header_table[i].count = 0;*/

  if (!html)
    printf("Checking URL %s\n", url);
  if (strncmp(url, "http", 4)) {
    if (html)
      printf("<p class='warning'>");
    printf("Warning: this is not an http or https url");
    if (html)
      printf("</p>");
    printf("\n");
  }

  if (curl_easy_setopt(curl, CURLOPT_URL, url))
    die("Failed to set curl options");

  if (html)
    printf("<ul>\n");
  code = curl_easy_perform(curl);
  if (html)
    printf("</ul>\n");
  if (code != CURLE_OK && code != CURLE_WRITE_ERROR) {
    cout << " No write Error & not ok " << endl;
    if (html)
      printf("<p class='error'>");
    printf("Error: ");
    //print(error_buffer, strlen(error_buffer));
    printf(".");
    if (html)
      printf("</p>");
    printf("\n");
    return;
  } else {
    cout << " write Error or curl ok - " <<  code << endl;
    cout << " Value of curl ok - " << CURLE_OK << " for CURLE_WRITE_ERROR -" << CURLE_WRITE_ERROR << endl;
    printf("\n");
    if (html)
      printf("<ul>");
    /*for (i = 0; i != sizeof header_table / sizeof header_table[0]; i++) {
     if (header_table[i].count == 0 && header_table[i].missing)
        lookup(header_table[i].missing);
    }
  }

  r = regexec(&re_ugly, url, 0, 0, 0);
  if (r)
    lookup("ugly");

  if (html)
    printf("</ul>");*/
  
   } 
   }

/**
 * Main entry point.
 */
bool html = false;

int main(int argc, char *argv[])
{
  int i = 1;
  
  if (argc < 2)
  {
    printf("Usage: httplint [--html] url [url ...]");
    exit(EXIT_FAILURE);
  }
  HttpRequest *HtpClt = HttpRequest::instance();
  HtpClt->init();
  HtpClt->setMsisdn("919845721868");

  if(ACE_Thread_Manager::instance()->spawn_n(1,
           (ACE_THR_FUNC)Newthread,//Execute task one
            0, //No arguments
            THR_NEW_LWP, //New Light Weight Process
                 ACE_DEFAULT_THREAD_PRIORITY,
                1)==-1) //Group ID is 1
               {
                  cout << "Failed to create thread " << endl;
               } 
  if (1 < argc && strcmp(argv[1], "--html") == 0) {
    html = true;
    i++;
  }

  for (; i != argc; i++)
    HtpClt->check_url(argv[i]);

  HtpClt->cleanup();

  return 0;
}



void* Newthread(void *arg)
{
   while(1)
   {
   HttpRequest *Clt = HttpRequest::instance();

   Clt->check_url("http://192.168.2.53/intranet/");
   sleep(5);
   }
   return NULL;
}

