#include "../wou.h"

// numero de tiles na grelha
#define GRID_WIDTH 23
#define GRID_HEIGHT 24

BITMAP *grid;	// buffer com a grelha

int dragging; // o numero do tile que tamos a arrastar

// onde começa a desenhar o mapa
int CANTO_X;
int CANTO_Y;

/*void mouse_cross() {
	// set mouse bitmap
	behind_mouse = create_bitmap(16,16);
	mouse_bmp = create_bitmap(16,16);
	clear(mouse_bmp);
	putpixel(mouse_bmp, 16/2, 16/2, makecol(255,0,0));
	putpixel(mouse_bmp, (16/2)+1, 16/2, makecol(255,0,0));
	putpixel(mouse_bmp, (16/2)-1, 16/2, makecol(255,0,0));
	putpixel(mouse_bmp, 16/2, (16/2)+1, makecol(255,0,0));
	putpixel(mouse_bmp, 16/2, (16/2)-1, makecol(255,0,0));
}*/

void init_all() {
	CANTO_X = 0;
	CANTO_Y = 0;
	
	allegro_init();
	install_keyboard();
	install_mouse();
	display.init();

/*	// definições da janela
	set_gfx_mode(GFX_AUTODETECT_WINDOWED, 1024, 768, 0, 0);
	set_display_switch_mode(SWITCH_BACKGROUND);
	set_window_title("WoU Map Editor");*/
	
	grid = create_bitmap(GRID_WIDTH*TILE_W, GRID_HEIGHT*TILE_H);

	set_palette((RGB*)tiles[PALETTE_GAME].dat);
	clear_to_color(vbuffer,1);
	//mouse_cross();

	map.load();
}

void blit_buffer(BITMAP *bmp) {
	acquire_screen();
	blit(bmp, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
	release_screen();
}

void draw_menu() {
	BITMAP* tilebmp;
	tilebmp = ((BITMAP*)tiles[TILES_TERRAIN].dat);
	blit(tilebmp,vbuffer,0,0,5,GAMEPLAY_H+10,tilebmp->w,tilebmp->h);
}

void draw_all() {
   	map.draw(); // gamezone map
   	draw_menu(); // pcx com os tiles ah direita
}

/*void draw_mouse() {
	int x, y;
    x = mouse_x, y = mouse_y;
	blit(vbuffer,behind_mouse,x,y,0,0,16,16);
	draw_sprite(vbuffer, mouse_bmp, x,y);
	blit_buffer(vbuffer);
	blit(behind_mouse,vbuffer,0,0,x,y,16,16);
}*/

void draw_map() {
	int i,j,xi,yi,side,line,col;
	int p;
	side = TILE_W; // the tile side dimensions (it's a square!)

	for (i=0; i<GAMEPLAY_W; i++) {
		for (j=0; j<GAMEPLAY_H; j++) {
			// se ta fora da zona
			if (CANTO_X+i>ZONE_W || CANTO_Y+j>ZONE_H || CANTO_X+i<0 || CANTO_X+j<0) p = 248;
			else {
				p = map.tile[CANTO_X+i][CANTO_Y+j].num; // get the tile number for my tile[i][j]
			}
			line = p/30; // line from tileset to get the tile num = p
			col = p%30; // column from tileset to get the tile num = p
			xi = (side*col)+col+1;
			yi = (side*line)+line+1;
			blit((BITMAP*)tiles[TILES_TERRAIN].dat, gamearea, xi, yi, i*side, j*side, side, side);
		}
	}
	blit(gamearea,vbuffer,0,0,5,5,GAMEPLAY_W,GAMEPLAY_H);
}

void get_brush() {
	int line, col,xi,yi;
	// vamos ver em que linha e coluna eu carreguei
	line = (mouse_y-GAMEPLAY_H-10)/17;
	col = (mouse_x-5)/17;
	xi = (TILE_W*col)+col+1;
	yi = (TILE_H*line)+line+1;
	dragging = col+line*30;
	textprintf_ex(vbuffer, (FONT*)fonts[FONT_04B11].dat, SCREEN_W/2, SCREEN_H-10, makecol(255,35,70), -1,"DRAGGING: %i", dragging);
	//blit(TERRAIN, mouse_bmp, xi,yi,0,0,TILE_W,TILE_H);
}

void drop_on_map() {
	int col,line;
	col = (mouse_x-5)/16;
	line = (mouse_y-5)/16;
	map.tile[CANTO_X+col][CANTO_Y+line].num = dragging;
	textprintf_ex(vbuffer, (FONT*)fonts[FONT_04B11].dat, SCREEN_W/2, SCREEN_H-10, makecol(255,35,70), -1,"DROP: %i:%i", CANTO_X+line,CANTO_Y+col);
}

void edit_map() {
	int with_tile = FALSE; // se tenho um tile em vez do ponteiro do rato
	while (!key[KEY_ESC]) {
		if (key[KEY_RIGHT]) CANTO_X++;
		else if (key[KEY_LEFT]) CANTO_X--;
		else if(key[KEY_UP]) CANTO_Y--;
		else if (key[KEY_DOWN]) CANTO_Y++;
		if ((mouse_b & 1) && mouse_y>GAMEPLAY_H+10) {
			with_tile = true;
			get_brush();
		}
		else if ((mouse_b &1) && mouse_y<GAMEPLAY_H+10 && with_tile) {
			drop_on_map();
		}
		draw_all();
		rectfill(vbuffer, SCREEN_W/2, SCREEN_H-10,SCREEN_W,SCREEN_H,1);
		rest(1);
	}
}

int main() {
	init_all();
	edit_map();
	map.save();
	allegro_exit();
	return 0;
}
END_OF_MAIN();
