#include <allegro.h>
#include <math.h>
#include <iostream>
#include <time.h>

#define max(a, b)  (((a) > (b)) ? (a) : (b))

using namespace std;

/*
Nakresli ramcek

@param bmp Bitmapa
@param xlt, ylt Suradnice x a y pre lavy horny roh
@param xlr, ylr Suradnice x a y pre pravy dolny roh
@param col Farba (makecol)
*/

void drawBorder(BITMAP* bmp, int xtl, int ytl, int xlr, int ylr, int col) {
	
	line(bmp, xtl, ytl, xlr, ytl, col); // top
	line(bmp, xtl, ylr, xlr, ylr, col); // bottom
	line(bmp, xtl, ytl, xtl, ylr, col); // left
	line(bmp, xlr, ytl, xlr, ylr, col); // right
}

/*
Nakresli progress bar

@param bmp Bitmapa do ktorej kreslit (BITMAP*)
@param x,y Suradnice zaciatku
@param width,height Sirka a vyska
@param start_r, start_g, start_b RGB pre zaciatok (0 - 255)
@param end_r, end_g, end b RGB pre koniec (0 - 255)
@param progress Vyfarbena cast (0 - 100%)
@param border Urcuje ci sa nakresli ramcek (bool)
@param border_col Farba ramceku (makecol)
@param background_col Farba pozadia (makecol)
*/
void drawProgressBar(BITMAP* bmp, int x, int y, int width, int height, int start_r, int start_g, int start_b, int end_r, int end_g, int end_b, int progress, bool border, int border_col, int background_col) {
	
	// pocet "krokov" pri 100%
	
	int steps = width;
	
	if (steps > 255) steps = 255;
	if (steps < 1) steps = 1;
	
	// krok pre RGB
	
	double step_r = max(start_r, end_r) / steps;
	double step_g = max(start_g, end_g) / steps;
	double step_b = max(start_b, end_b) / steps;	
	
	// pricitat ci odcitat? 
	
	bool add_r = (max(start_r, end_r) == end_r) ? TRUE : FALSE;
	bool add_g = (max(start_g, end_g) == end_g) ? TRUE : FALSE;
	bool add_b = (max(start_b, end_b) == end_b) ? TRUE : FALSE;	
	
	// pozadie
	
	rectfill(bmp, x, y, x + width, y + height, background_col);
	
	// cyklusove premenne
	
	int i;
	int stop = (progress * width) / 100;
	
	// docasne pomocne premenne
	
	int sx, ex, sy, ey;
	int r,g,b;
	
	for (i = 0; i < stop; i++) {
		
		// nastavenie zaciatku a konca
		
		sx = i; // start x
		ex = i; // end x
		
		sy = 0; // start y
		ey = height; // end y
		
		// posun
		
		sx = sx + x;
		ex = ex + x;
		
		sy = sy + y;
		ey = ey + y;
		
		// spocitanie RGB
		
		r = (int) step_r * i;
		g = (int) step_g * i;
		b = (int) step_b * i;
		
		// pripociatat (alebo odpocitat) zaciatok 
		
		r = (add_r) ? (r + start_r) : (start_r - r);
		g = (add_g) ? (g + start_g) : (start_g - g);
		b = (add_b) ? (b + start_b) : (start_b - b);
		
		if (i > steps) {
			
			r = end_r;
			g = end_g;
			b = end_b;
			
		}
		
		// nakreslenie
		
		line (bmp, sx, sy, ex, ey, makecol(r, g, b));
		
	}
	
	if (border) {
		
		// ramcek
		
		drawBorder(bmp, x, y, x + width, y + height, border_col);
		
	}
	
}

/*
Nakresli cely status bar (sirka = SCREEN_W, vyska = SREEN_H / 10);

@param bmp Bitmapa kam kreslit
@param health zdravie (0 - 100)
@param reload_navnada, reload_torpedo1-4 Reloading navnady a torpedovych komor
@param pocet_torped, pocet_navnad
@param cas Cas od novej hry (s)
@param cash Peniaze ($)
@param border, border_col Ramcek, farba (makecol)
@param background_col Farba pozadia (makecol);
*/

void drawStatusBar(BITMAP* bmp, int health, int reload_navnada, int reload_torpedo1, int reload_torpedo2, int reload_torpedo3, int reload_torpedo4, int pocet_torped, int pocet_navnad, int cas, int cash, bool border, int border_col, int background_col) {
	
	int width = SCREEN_W;
	int height = SCREEN_H / 10;
	
	int y = SCREEN_H - height;
	
	// sirka & vyska veci // za komentarom je ideal pre 1024*768
	// health
	int health_height = (int) (height / 3.3); // 23
	int health_hspace = (int) (height / 7.6); // 10
	int health_width  = (int) (width / 3.41); // 300
	int health_wspace = (int) (width / 51.2); // 20
	
	// reload torped
	int reloadt_height = (int) (height / 6.33); // 12
	int reloadt_hspace = (int) (height / 7.6); // 10
	int reloadt_width = (int) (width / 6.83); // 150
	int reloadt_wspace = (int) (width / 51.2); // 20
	
	// reload nevnady
	int reloadn_height = (int) (height / 6.33); // 12
	int reloadn_hspace = (int) (height / 7.6); // 10
	int reloadn_width = (int) (width / 5.12); // 200
	int reloadn_wspace = (int) (width / 12.8); // 80
	
	// text dole pod health (tct - time cash text)
	int tct_height = (int) (height / 3.3); // 23
	int tct_hspace = (int) (height / 7.6); // 10	
	int tct_width = (int) (width / 3.41); // 300
	int tct_wspace = (int) (width / 51.2); // 20
	
	// text v strede (pocet torped a navnad)
	int ptn_height = (int) (height / 3.16); // 24
	int ptn_hspace = (int) (height / 7.6); // 10
	int ptn_width = (int) (width / 8.26); // 124
	int ptn_wspace = (int) (width / 10.24); // 100	
	
	// pozadie, ramcek
	
	rectfill(bmp, 0, y, width, y + height, background_col);
	line(bmp, 0, y, width, y, border_col);
	
	// progress bars ...
	
	// health progressbar

	drawProgressBar(bmp, health_wspace, y + health_hspace, health_width, health_height, 255, 0, 0, 0, 255, 0, health, 1, makecol(255,255,255), makecol(0,0,255));

	// torpedo reload progressbars
	
	drawProgressBar(bmp, width - 2*reloadt_width - 2*reloadt_wspace, y + reloadt_hspace, reloadt_width, reloadt_height, 255, 0, 0, 0, 255, 0, reload_torpedo1, 0, 0, makecol(0,0,0));
	drawProgressBar(bmp, width - reloadt_width - reloadt_wspace, y + reloadt_hspace, reloadt_width, reloadt_height, 255, 0, 0, 0, 255, 0, reload_torpedo2, 0, 0, makecol(0,0,0));
	drawProgressBar(bmp, width - 2*reloadt_width - 2*reloadt_wspace, y + 2*reloadt_hspace + reloadt_height, reloadt_width, reloadt_height, 255, 0, 0, 0, 255, 0, reload_torpedo3, 0, 0, makecol(0,0,0));
	drawProgressBar(bmp, width - reloadt_width - reloadt_wspace, y + 2*reloadt_hspace + reloadt_height, reloadt_width, reloadt_height, 255, 0, 0, 0, 255, 0, reload_torpedo4, 0, 0, makecol(0,0,0));
	
	// navnada reload progressbar
	
	drawProgressBar(bmp, width - reloadn_width - reloadn_wspace, y + 3*reloadt_hspace + 2*reloadt_height, reloadn_width, reloadn_height, 255, 0, 0, 0, 255, 0, reload_torpedo4, 0, 0, makecol(0,0,0));
	
	// text
	
	// ramcek na cas a chash
	drawBorder(bmp, tct_wspace, y + height - tct_hspace - tct_height, tct_wspace + tct_width, height + y - tct_hspace, makecol(255,255,255));
	
	/*
	Rasto - navod na pridanie textu
	
	* cas a peniaze
	
	suradnice zaciatku a koncia si vypocitas z sirky a vysky (premene tct_...)
	sposob najdes v drawBorder nad tymto komentarom
	udaje su ako parametre int cas (v sekundach) a int cash
	ak budes mat problem s prepoctom sekund na min a sec povedz, spravim ti to
	font, velkost, farba, ... je na tebe
	
	* pocet torped a navnad
	
	suradnice zase cez premenne ptn_...
	sposob pod tymto kometarom
	udaje su ako parametre (pocet_torped a pocet_navnad)
	farba a font zalezi od teba, ale font by sa hodil taky displey-ovy (LED)
	
	keby nieco, povedz
	
	*/
	
	// ramcek na pocet torped a navnad
	
	drawBorder(bmp, health_width + 2*health_wspace + ptn_wspace, y + ptn_hspace, health_width + 2*health_wspace + ptn_wspace + ptn_width, y + ptn_hspace + ptn_height, makecol(255,255,255));
	drawBorder(bmp, health_width + 2*health_wspace + ptn_wspace, height + y - ptn_hspace - ptn_height, health_width + 2*health_wspace + ptn_wspace + ptn_width, height + y - ptn_hspace, makecol(255,255,255));
	
}

int main () {
	
	//...
	
	allegro_init();
	install_keyboard();
	set_color_depth(16);
	set_gfx_mode(GFX_AUTODETECT_FULLSCREEN,1024,768,0,0);
	
	clear_to_color(screen, makecol(0, 0, 0));
	/**/
	//drawProgressBar(screen,100,200,300,10,255,0,0,0,255,0,50,1,makecol(255,255,255), makecol(0,0,255));
	/**/
	
	drawStatusBar(screen, rand() % 100, rand() % 100, rand() % 100, rand() % 100, rand() % 100, rand() % 100, 10, 20, 114, 500, 1, makecol(255,255,0), makecol(128,128,128));
	
	while (!key[KEY_ESC]) {};
	
	return 0;
	
}
END_OF_MAIN();
