#include <stdio.h>

#include "kamery.h"
//#include "xvid.h"
//#include "curl.c"
//#include "jpeg.c"
#include <gd.h>
#include <sys/time.h>
#include <gdfonts.h>
#include "gdfontl.h"

#define BPP 3



static int
fetchBitmap(struct MemoryStruct *bmpdata, char *url, char *authBasic)
{
	printf("Http sent...\n");
  
  //we put the content from url into curldata
	struct MemoryStruct curldata;
	if(! GetImageWithCurl(&curldata, url, authBasic) ){
    printf("Downloading image has failed (%s, %s).", url, authBasic);
    return 1;    
	}
	
	if(!curldata.size){
    printf("Downloaded image has a zero size (%s, %s).", url, authBasic);
    return 1;
	}
  	
  //create image from Pointer
  gdImagePtr im_in, im_out, im;
  im_in = gdImageCreateFromJpegPtr(curldata.size, curldata.memory);
 	
	if(im_in == NULL){
    printf("ERROR: the url does not contain any image (%s,%s).\nResult: %s\n", url, authBasic, curldata.memory);
    return 1;
	}
  	
	if(!im_in->trueColor){
    printf("Something nasty happened... JPEG must be truecolor.\n");
    return 1;
	}

 	free(curldata.memory);


  // new image with dimensions from "global" vars 
  im_out = gdImageCreateTrueColor(XDIM, YDIM);
  
  /* Now copy the large image, but smaller */
  gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0,
                      im_out->sx, im_out->sy,
                      im_in->sx, im_in->sy);
  
  gdImageDestroy(im_in);
  im = im_out; //copying pointer

  int red;
	red = gdImageColorAllocate(im, 255, 0, 0); //allocate black color

  //***** Rendering text with FreeType - needs ttf font 
  //  char *err;
  // 	int brect[8];
  // 	err = gdImageStringFT(im, NULL, 
  //       red, "/usr/share/fonts/truetype/arial.ttf", 10,0,0,160,
  //       asctime(getTimeStructPtr()));
  //  if(err)	fprintf(stderr,"Error : %s\n",err);

  gdImageString(im, gdFontGetLarge(), 0, 0, asctime(getTimeStructPtr()), red);
  
  //*this code (realloc) ended with error or SegFault */
  //bmpdata->size = im->sx * im->sy * BPP;
  //bmpdata->memory = (char *)myrealloc(bmpdata->memory, bmpdata->size);
  
  /*alocating size*/
  if(bmpdata->size == 0){
    bmpdata->size = im->sx * im->sy * BPP;
    bmpdata->memory = (char *)malloc(bmpdata->size);
  }
  
  /*Write RGB values from int **tpixels to char *bmpdata.memory*/
	int x,y,i;
	i=0;
  for(y=0; y < im->sy; y++){
    for(x=0; x < im->sx; x++){
      if(i+2 > bmpdata->size){
        printf("Error: possible buffer overflow, MemoryStruct size is smaller than iterator.\n");
        return 1;
      }
      
      bmpdata->memory[i++] = (im->tpixels[y][x] & 0xFF);         //resulting pos B  
      bmpdata->memory[i++] = (im->tpixels[y][x] & 0xFF00)>>8;    //resulting pos G
      bmpdata->memory[i++] = (im->tpixels[y][x] & 0xFF0000)>>16; //resulting pos R
    }
  }
  
  gdImageDestroy(im);
	return 0;
}



struct tm *getTimeStructPtr(void){
  time_t rawtime;
  struct tm *t;
  time ( &rawtime );
  t = localtime ( &rawtime );
  return t;
}






/*
int
main(int argc,
	 char **argv[])
{

	struct MemoryStruct bmpdata;
	fetchBitmap(&bmpdata, "http://upload.zby.cz/rgb1.png");
  
	FILE *fw;
  fw = fopen ("bitmapa.bmp", "w") ;
  
  int i;
  for(i=0; i < bmpdata.size; i++)
  {
    fputc(bmpdata.memory[i], fw);
  }
  fclose(fw);

}
*/


