/***************************************************************************
 *            map_area.c
 *
 *  Copyright  2009 - Steffen Pohle
 *  steffen@gulpe.de
 ****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */


#include "map.h"
#include "memoryleak.h"
#include "system.h"

/*****************************************************
 *
 *  Area handling
 *
 */

/* 
 * add the area without checking for it's size, also no update is done
 */
int map_area_append_to_hash (struct map_hash *mh, struct map_area *ma) {
	int size;
	struct map_area *a = mh->areas;

	mh->areas = (struct map_area*) map_hash_getfree_aligned (mh, POINTERALIGNMENT);

	/* copy the area data to the free entry
	 * calc size and set free entry to it's new place */
	size = map_area_getsize (ma);
	map_area_copy (mh->areas, ma);
	mh->areas->next = a;
	mh->free = mh->free + size;
	mh->areascnt++;
	SETFLAG(mh->status, MS_changed);

	return 1;
};

/*
 * add the area without checking for it's size, but do an update
 */
int map_area_add_to_hash (struct map_hash *mh, struct map_area *ma) {
	int size;
	struct map_area *a;

	/* check if the first element is already the one we want to update */
	if (mh->areas && mh->areas->id == ma->id && mh->areas->subid == ma->subid) {
		mh->areas = mh->areas->next;
		mh->areascnt--;
	}

	/* find possible update and go to the end */
	if (mh->areas) {
		/* find last way */	
		a = mh->areas;
		while (a && a->next) {
			if (a->next->id == ma->id && a->next->subid == a->subid) {
				/* found old way.. just delete .. */
				a->next = a->next->next;
				mh->areascnt--;
			}
			if (a->next) a = a->next;
		}
		a->next = (struct map_area*) map_hash_getfree_aligned (mh, POINTERALIGNMENT);
		a = a->next;
	}
	else {
		/* complete new area */
		mh->areas = (struct map_area*) map_hash_getfree_aligned (mh, POINTERALIGNMENT);
		a = (struct map_area*) map_hash_getfree_aligned (mh, POINTERALIGNMENT);
	}
	
	/* copy the area data to the free entry
	 * calc size and set free entry to it's new place */
	size = map_area_getsize (ma);
	map_area_copy (a, ma);
	a->next = NULL;
	mh->free = mh->free + size;
	mh->areascnt++;
	SETFLAG(mh->status, MS_changed);

	return 1;
};


/******************************************************************************
 * Add/Update area information
 * - load/ get hash table
 * - calculate size in bytes for this entry
 * - check if pointer in free is enought for this new entry
 *   if not: move old memory block out of the area and put a new data block inside.
 * - add new area
 */
int map_area_add (struct map_area *area, int loadflags) {
	struct map_hash *mh = NULL;
	int size;

	if (area->p == NULL || area->p_cnt == 0) return 0;
//	area->type = area->subid % MAREA_campsite + 1;
//	d_printf ("subid:%d  type:%d   cnt:%d", area->subid, area->type, area->p_cnt);

	size = map_area_getsize (area);
	mh = map_hash_get (area->p[0].lon, area->p[0].lat, loadflags);
	if (mh == NULL) {
		mh = map_hash_alloc (size + MAP_HASH_DEFAULTSIZE);
		mh->pos.ilon = map_geo2igeo (area->p[0].lon);
		mh->pos.ilat = map_geo2igeo (area->p[0].lat);
		map_hash_add (mh);
	}
	else if (size >= map_hash_getfree_size (mh)) {
		mh = map_hash_realloc (mh, mh->datasize + size + MAP_HASH_DEFAULTSIZE);
	}
	return map_area_add_to_hash (mh, area);
};


/*
 * will copy all the data from one area to another, this function will not
 * check if there are conflicts with the memory at all. Also the next field
 * will be keeped untouched.
 */
void map_area_copy (struct map_area *dest, struct map_area *src) {
	int i;
	
	dest->id = src->id;
	dest->subid = src->subid;
	dest->type = src->type;
	dest->p_cnt = src->p_cnt;
	dest->p = (struct map_pos*) (dest->data);
	for (i = 0; i < dest->p_cnt; i++)
		dest->p[i] = src->p[i];
};


struct map_area *map_area_find (struct map_hash *mh, unsigned long long int id) {
	struct map_area *ma;

	ma = mh->areas;
	while (ma) {
		if (ma->id == id) return ma;
		ma = ma->next;
	}

	return NULL;
};


int map_area_getsize (struct map_area *area) {
	return (sizeof (struct map_area) + area->p_cnt * sizeof (struct map_pos));
};
