#include "empire.h"
#include "logic.h"

extern int SIDEBAR_WIDTH, BOTTOMBAR_HEIGHT;

void check_scrolling()
{
	check_scrolling(-1, -1);
}

void avoid_map_gaps()
{  //make sure that the map doesn't scroll too far up or left, so that there's no blank space between it and the UI 
	const int screen_end_x = global.screen_width - SIDEBAR_WIDTH;
	const int screen_end_y = global.screen_height - BOTTOMBAR_HEIGHT;
	
	if (screen_end_x < worldmap.get_length() * MAP_TILE_SIZE)
	{  // if the map won't fit on one screen
		if ((worldmap.get_length() - worldmap.camera.get_scroll_x()) * MAP_TILE_SIZE + worldmap.camera.get_offset_x() < screen_end_x)
		{  //if drawing the map won't cover the whole screen horizontally
			worldmap.camera.scroll(screen_end_x - ((worldmap.get_length() - worldmap.camera.get_scroll_x()) * MAP_TILE_SIZE + worldmap.camera.get_offset_x()), 0);
		}
	}
	else
	{  //center the map horizontally
		worldmap.camera.jump_to_x((worldmap.get_length() - XTILES) / 2);
	}
	
	if (screen_end_y < worldmap.get_height() * MAP_TILE_SIZE)
	{
		if ((worldmap.get_height() - worldmap.camera.get_scroll_y()) * MAP_TILE_SIZE + worldmap.camera.get_offset_y() < screen_end_y)
		{  //if drawing the map won't cover the whole screen vertically
			worldmap.camera.scroll(0, screen_end_y - ((worldmap.get_height() - worldmap.camera.get_scroll_y()) * MAP_TILE_SIZE + worldmap.camera.get_offset_y()));
		}
	}
	else
	{  //center the map vertically
		worldmap.camera.jump_to_y((worldmap.get_height() - YTILES) / 2);
	}
}

void check_scrolling(int forcex, int forcey)
{
	static int increment = MAP_TILE_SIZE / global.scroll_divider;
	
	if (!(mouse_b & 1))
	{  //the screen shouldn't scroll while the mouse button is down
		if (key[KEY_LEFT])
		{
			if (worldmap.camera.get_scroll_x() > 0)
			{
				worldmap.camera.scroll(increment, 0);
			}
		}
		if (key[KEY_RIGHT])
		{
			worldmap.camera.scroll(-increment, 0);
			
			avoid_map_gaps();
		}
		if (key[KEY_DOWN])
		{
			worldmap.camera.scroll(0, -increment);
			
			avoid_map_gaps();
		}
		if (key[KEY_UP])
		{
			if (worldmap.camera.get_scroll_y() > 0)
			{
				worldmap.camera.scroll(0, increment);
			}
		}
	}
	
	if ((forcex != -1) && (forcey != -1))
	{  //refresh increment because scroll_divider may have changed since last time
		increment = MAP_TILE_SIZE / global.scroll_divider;
		worldmap.camera.jump_to(forcex, forcey);
	}  
}

void scroll_to_hq(int plyr)
{
	static bool first = true;
	static int hqx = -1, hqy = -1;
	
	if (first)
	{
		for (int y = 0; y < worldmap.get_height(); y++)
		{
			for (int x = 0; x < worldmap.get_length(); x++)
			{
				_tile *tile = worldmap.get_tile(x, y);
				
				if (tile->is_HQ())
				{
					if (tile->owned_by(plyr))
					{
						hqx = x;
						hqy = y;
						break;
					}
				}
			}
		}
		
		first = false;
	}
	if (scroll_to_location(hqx, hqy) == 1)
	{
		game->state = NOTHING_HAPPENING;
		first = true;
	}
}

int scroll_to_location(int x, int y)
{
	static bool first = true;
	static int targetx, targety, increment;
	if (first)
	{
		increment = MAP_TILE_SIZE / global.AI_scroll_divider;
		targetx = x - (XTILES / 2);  //center on chosen tile
		targety = y - (YTILES / 2);
		if (targetx < 0) targetx = 0;  //make sure it doesn't go past the map ends
		if (targetx + XTILES > worldmap.get_length()) targetx = worldmap.get_length() - XTILES;
		if (targety < 0) targety = 0;
		if (targety + YTILES > worldmap.get_height()) targety = worldmap.get_height() - YTILES;
		
		if (worldmap.get_length() <= XTILES)  //not able to scroll horizontally, center the map horizontally instead
			targetx = (worldmap.get_length() - XTILES) / 2;
		if (worldmap.get_height() <= YTILES)  //not able to scroll vertically, center the map vertically instead
			targety = (worldmap.get_height() - YTILES) / 2;
		
		first = false;
	}

	if (worldmap.camera.get_scroll_x() > targetx)
	{
		worldmap.camera.scroll(increment, 0);
	}
	else if (worldmap.camera.get_scroll_x() < targetx)
	{
		worldmap.camera.scroll(-increment, 0);
	}
	else if ((worldmap.camera.get_scroll_x() == targetx) && (worldmap.camera.get_offset_x() != 0))
	{
		if (worldmap.camera.get_offset_x() >= increment)
		{
			worldmap.camera.scroll(-increment, 0);
		}
		else
		{
			worldmap.camera.scroll(-worldmap.camera.get_offset_x(), 0);
		}
	}

	if (worldmap.camera.get_scroll_y() > targety)
	{
		worldmap.camera.scroll(0, increment);
	}
	else if (worldmap.camera.get_scroll_y() < targety)
	{
		worldmap.camera.scroll(0, -increment);
	}
	else if ((worldmap.camera.get_scroll_y() == targety) && (worldmap.camera.get_offset_y() != 0))
	{
		if (worldmap.camera.get_offset_y() >= increment)
		{
			worldmap.camera.scroll(0, -increment);
		}
		else
		{
			worldmap.camera.scroll(0, -worldmap.camera.get_offset_y());
		}
	}

/*	if ((worldmap.camera.get_scroll_x() == 0) && (worldmap.camera.get_offset_x() > 0))  //I think this was to avoid crashes, back before the changes to the map class
	{
		worldmap.camera.get_offset_x() = 0;
	}
	if ((worldmap.camera.get_scroll_y() == 0) && (worldmap.camera.get_offset_y() > 0))
	{
		worldmap.camera.get_offset_y() = 0;
	}*/

	if ((worldmap.camera.get_scroll_x() == targetx) && (worldmap.camera.get_scroll_y() == targety) && (worldmap.camera.get_offset_x() == 0) && (worldmap.camera.get_offset_y() == 0))
	{  //if it's reached the target
		first = true;
		return 1;
	}
	return 0;
}
