#include "wou.h"

int map_load() {
  int i, j;
  PACKFILE *fp;

	fp = pack_fopen("data/map/map1.map", "rp");
	for (i=0; i<ZONE_W; i++) {
		for (j=0; j<ZONE_H; j++) {
			tiles[j][i].num = pack_igetl(fp);    // tile layer
			fringes[j][i].num = pack_igetl(fp);  // fringe layer
		}
	}
	pack_fclose(fp);

	objects_load();   // creates object layer
	db_readtiles();   // tiles attributes from db
	db_readfringe();  // fringe attributes from db
}

void map_save() {
  int i, j;
	PACKFILE *fp;

	pfile = pack_fopen("data/map/map1.map", "wp");
	for (i=0; i<ZONE_W; i++) {
		for (j=0; j<ZONE_H; j++) {
			pack_iputl(tiles[j][i].num, fp);   // tile layer
			pack_iputl(fringes[j][i].num, fp); // fringe layer
		}
	}
	pack_fclose(fp);
	objects_save(); // saves object layer
}

void map_loadObjects() {
  int x, y, count;
	PACKFILE *file;

	fp = pack_fopen("data/map/map1.obj", "rp");
	while (!pack_feof(fp)) {
    x = pack_igetl(fp);     // x of the object
    y = pack_igetl(fp);     // y of the object
    count = pack_igetl(fp); // how many objects we have?

		for (int k=0; k<count; k++) {
			/* PRECISA DE REVISAO */
			Object *o = new Object;
			o->num = pack_igetl(fp);
			objects[i][j]->addObject(o);
		}
	}
	pack_fclose(fp);
}

void map_saveObjects() {
	int x, y;
	PACKFILE *file;
	file = pack_fopen("data/map/map1.obj", "wp");

	for (x=0; x<ZONE_W; x++) {
		for (y=0; y<ZONE_H; y++) {
			if (objects[y][x]) {
				pack_iputl(x, file);
				pack_iputl(y, file);
				pack_iputl(objects[y][x]->count, file);

				while (objects[y][x]->head!=NULL) {
					Object *o = objects[y][x]->head; // ponteiro para o objecto head
					pack_iputl(o->num, file);
					objects[y][x]->delObject();
				}
			}
		}
	}
	pack_fclose(file);
}

void map_draw(int x, int y, int zoom) {
	map_drawTiles(x, y, zoom);
	map_drawFringe(x, y, zoom);
	map_drawObjects(x, y, zoom);
}

void map_drawTiles(int x, int y, int zoom) {
	int coords[2];
	int i, j, a, b;
	for (i=0; i<GAMEPLAY_W; i++) {
		for (j=0; j<GAMEPLAY_H; j++) {
			a = i+x;
			b = j+y;
			// so that world is round
			if (a>ZONE_W-1) a = a-ZONE_W;
			if (b>ZONE_H-1) b = b-ZONE_H;
			get_gif_coords(tiledb[tile[a][b].num].icon, coords);
			blit(tilesBMP, gamearea, coords[0], coords[1], i*TILE_W, j*TILE_H, TILE_W, TILE_H);
		}
	}
}

void map_drawFringe(int x, int y, int zoom) {
	int coords[2];
	int i, j, a, b;
	for (i=0; i<GAMEPLAY_W/TILE_W; i++) {
		for (j=0; j<GAMEPLAY_H/TILE_H; j++) {
      a = i+x;
			b = j+y;
      // so that world is round
			if (a>ZONE_W-1) a = a-ZONE_W;
			if (b>ZONE_H-1) b = b-ZONE_H;
			get_gif_coords(fringedb[fringe[a][b].num].icon, coords);
			masked_blit(fringeBMP, gamearea, coords[0], coords[1], i*TILE_W, j*TILE_H, TILE_W, TILE_H);
		}
	}
}

//******************************************************************************
// Desenha a layer de objectos
//******************************************************************************
void Map::drawObjects(int x, int y, int zoom) {
	int i,j,a,b;
	int coords[2];
	for (i=0; i<GAMEPLAY_W/TILE_W; i++) {
		for (j=0; j<GAMEPLAY_H/TILE_H; j++) {
  			a = i+x;
			b = j+y;
			/* Para dar 'a volta ao mundo' */
			if (a>549) a = a-550;
			if (b>549) b = b-550;
			if (objects[a][b]!=NULL && objects[a][b]->head!=NULL) {
				ObjectList ol = (ObjectList)*objects[a][b]; // copiar o objecto
				// percorrer todos os objectos da lista
				while (ol.head!=NULL) {
					Object *o = ol.head;
					get_gif_coords(o->icon, coords); // coordenadas no PCX
					masked_blit(objectsBMP, gamearea, coords[0], coords[1], i*TILE_W, j*TILE_H, TILE_W, TILE_H);
					ol.delObject();
				}
			}
		}
	}
}

void objlist_add(object **head, object *ob) {
	ob->next = *head;
}

void objlist_delete(object **head, object *ob) {
	head = head->next;
	count--;
}

void objlist_free(object **head) {
  object *curr;
  curr = *head;
  while (curr) {

  }
}
