//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <stdio.h>
#include <stdlib.h>

#include "Unit1.h"
#include "json/json.h"
#include "curl/curl.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#pragma comment(lib, "bcb_libcurd.lib")
TForm1 *Form1;

struct MemoryStruct
{
  char *memory;
  size_t size;
};

static size_t

WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)userp;
 
  mem->memory = (char*)realloc(mem->memory, mem->size + realsize + 1);

  if(mem->memory == NULL) {
    /* out of memory! */
    printf("not enough memory (realloc returned NULL)\n");
    return 0;
  }

  memcpy(&(mem->memory[mem->size]), contents, realsize);
  mem->size += realsize;
  mem->memory[mem->size] = 0;
 
  return realsize;
}

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------   
bool __fastcall TForm1::Specs_Inform_Parsing(AnsiString JSON_Specs)
{
    /*****************************************************
    function : 스펙 정보를 파싱한다.
    Param-1 : JSON  구조의 스펙 정보 문자열 
    *****************************************************/
    Json::Reader reader ;
    Json::Value root ;
    AnsiString usTemp ;
    bool result = true ;
    double tx1,tx2 ;
 
    try
    {
        reader.parse(JSON_Specs.c_str(), root) ;
 
        usTemp = root["ith_25_upper"].asCString() ;    // 항목(ith_25_upper)에 대한 결과 값이 문자열로 usTemp 로 들어 갑니다.
        tx1= usTemp.ToDouble() ;                       // 값이 double 형이기 때문에 doble 형으로 변환해서 제가 원하는 변수 List에 넣었습니다.
 
        usTemp = root["ith_85_upper"].asCString() ;
        tx1 = usTemp.ToDouble() ;
 
        usTemp = root["se_25_lower"].asCString() ;
        tx1 = usTemp.ToDouble() ;
    }
    catch(...)
    {
        result = false ;
    }

    return result ;
}

//---------------------------------------------------------------------------
void __fastcall TForm1::btn2Click(TObject *Sender)
{
    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();

    struct MemoryStruct chunk;

    chunk.memory = (char*) malloc(1);  /* will be grown as needed by the realloc above */
    chunk.size = 0;    /* no data at this point */

    if (curl)
    {
        //curl_easy_setopt(curl, CURLOPT_URL, "google.com");
        //curl_easy_setopt(curl, CURLOPT_URL, "http://date.jsontest.com/");

        //Input URL
        curl_easy_setopt(curl, CURLOPT_URL, "http://samples.openweathermap.org/data/2.5/forecast?lat=35&lon=139&appid=c32e4ebbf5e810e59ab0ab4965ae2e24");
        //Call Back function
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
        //Write memory
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
        //
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        res = curl_easy_perform(curl);

        if (res != CURLE_OK)  edit2->Text = "Error" ;
        else
        {
            Json::Value jsonData;
            Json::Reader jsonReader;


            if(jsonReader.parse(chunk.memory, jsonData))
            {
                //onData.toStyledString();

                //String s = jsonData["list"][0]["weather"][0]["main"].asCString();

                Memo1->Lines->Clear();
                Memo1->Lines->Add(">>>Particular JsonData Parsing<<<\n");
                //Memo1->Lines->Add("Weatherstring : " + s);



                Memo1->Lines->Add(">>>JsonData Parsing<<<\n");
                Memo1->Lines->Add(chunk.memory);


            }

            //Data Read from URL
            //Memo1->Lines->Clear();
            //Memo1->Lines->Add(chunk.memory) ;
        }

        curl_easy_cleanup(curl);
    }
}
//---------------------------------------------------------------------------
