/*
 * Copyright (C) 2013-2014 Jolla Ltd. <andrew.den.exter@jollamobile.com>
 * Author: Philip Van Hoof <philip@codeminded.be>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "config.h"

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <libtracker-common/tracker-common.h>
#include <libtracker-extract/tracker-extract.h>



typedef struct BMP_FILEHEAD{                     // BMP file header
    char   bfType[2];                    // "BM"
    uint  bfSize;                      // size of file
    ushort  bfReserved1;
    ushort  bfReserved2;
    uint  bfOffBits;                   // pointer to the pixmap bits
}__attribute__((packed)) BMPFILEHEADER;

typedef struct BMP_INFOHDR {                     // BMP information header
    uint  biSize;                      // size of this struct
    uint  biWidth;                     // pixmap width
    uint  biHeight;                    // pixmap height
    ushort  biPlanes;                    // should be 1
    ushort  biBitCount;                  // number of bits per pixel
    uint  biCompression;               // compression method
    uint  biSizeImage;                 // size of image
    uint  biXPelsPerMeter;             // horizontal resolution
    uint  biYPelsPerMeter;             // vertical resolution
    uint  biClrUsed;                   // number of colors used
    uint  biClrImportant;              // number of important colors
}__attribute__((packed)) BMPINFOHEADER;



static gboolean get_img_resolution (const GFile *file, gint64 *width, gint64 *height)
{
    GFileInputStream *stream;
    GError *error = NULL;
    BMPFILEHEADER   bmpfile;
    BMPINFOHEADER   bmpinfo;

    stream = g_file_read (file, NULL, &error);

    //g_message("sizeof(ulong) =%d  sizeof(uint)=%d ushort=%d!, sizeof(char)=%d, sizeof(BMPFILEHEAD)=%d\n", sizeof(ulong),sizeof(uint),sizeof(ushort), sizeof(char),sizeof(BMPFILEHEADER));

    if (!g_input_stream_read (G_INPUT_STREAM (stream),&bmpfile, sizeof(BMPFILEHEADER), NULL, &error))
    {
        *width = -1;
        *height = -1;
        g_warning ("Error skip bmp file header from stream: '%s'", error->message);
        g_error_free (error);
        g_object_unref(stream);
        return FALSE;
    }

    if (!g_input_stream_read (G_INPUT_STREAM (stream), &bmpinfo, sizeof(BMPINFOHEADER), NULL, &error))
    {
        *width = -1;
        *height = -1;
        g_warning ("Error read bmp file info header from stream: '%s'", error->message);
        g_error_free (error);
        g_object_unref(stream);
        return FALSE;
    }

    *width = bmpinfo.biWidth;
    *height = bmpinfo.biHeight;
    g_input_stream_close (G_INPUT_STREAM (stream), NULL, NULL);
    g_object_unref(stream);
    return TRUE;
}

G_MODULE_EXPORT gboolean
tracker_extract_get_metadata (TrackerExtractInfo *info)
{
	TrackerSparqlBuilder *preupdate, *metadata;
	goffset size;
	const gchar *graph;
	gchar *filename, *uri;
	GFile *file;
    gint64 max_width, max_height;

	preupdate = tracker_extract_info_get_preupdate_builder (info);
	metadata = tracker_extract_info_get_metadata_builder (info);
	graph = tracker_extract_info_get_graph (info);

	file = tracker_extract_info_get_file (info);
	filename = g_file_get_path (file);
	size = tracker_file_get_size (filename);

	if (size < 14) {
		/* Smaller than BMP header, can't be a real BMP file */
		g_free (filename);
		return FALSE;
	}

	tracker_sparql_builder_predicate (metadata, "a");
	tracker_sparql_builder_object (metadata, "nfo:Image");
	tracker_sparql_builder_object (metadata, "nmm:Photo");

	/* TODO: Add actual metadata extraction for BMP files */


    if (get_img_resolution (file, &max_width, &max_height)) {
        //g_message("tracker-extract-bmp: %d max_width=%d, max_height=%d", __LINE__, max_width, max_height);
        if (max_width > 0) {
            tracker_sparql_builder_predicate (metadata, "nfo:width");
            tracker_sparql_builder_object_int64 (metadata, (gint64) max_width);
        }
        if (max_height > 0) {
            tracker_sparql_builder_predicate (metadata, "nfo:height");
            tracker_sparql_builder_object_int64 (metadata, (gint64) max_height);
        }
    }

    g_object_unref(file);

	return TRUE;
}
