#include "allegro5/allegro.h"
#include "allegro5/allegro_acodec.h"
#include "allegro5/allegro_audio.h"
#include "allegro5/allegro_video.h"
#include "allegro5/allegro_primitives.h"
#include "allegro5/allegro_image.h"
#include "allegro5/allegro_font.h"
#include "allegro5/allegro_ttf.h"

#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>

const int sw = 800;
const int sh = 600;

int main(int argc , char** argv) {
   
	if (!al_init()) {
		printf("Allegro init failed.\n");
		return 1;
	}
	if (!al_install_audio() || !al_install_keyboard() || !al_install_mouse()) {
		printf("Install failed.\n");
		return 2;
	}
	if (!al_init_font_addon() || !al_init_ttf_addon() || !al_init_acodec_addon() || !al_init_video_addon() || !al_init_image_addon() || !al_init_primitives_addon()) {
		printf("Addon init failed.\n");
		return 5;
	}

	/// This is important, it sets our current working directory to the directory with our executable in it
	ALLEGRO_PATH* p = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
	al_change_directory(al_path_cstr(p, '\\'));
	al_destroy_path(p);
	p = 0;

	al_set_new_display_flags(ALLEGRO_WINDOWED);

	ALLEGRO_DISPLAY* d = al_create_display(sw, sh);
	
	ALLEGRO_EVENT_QUEUE* q = al_create_event_queue();

	ALLEGRO_FONT* f = al_load_ttf_font("Verdana.ttf", -72, 0);

	ALLEGRO_TIMER* t = al_create_timer(1.0 / 60.0);

	ALLEGRO_SAMPLE* s = al_load_sample("Sensors7secs.wav");

	if (!d || !q || !f || !t || !s) {
		printf("Failed setup.\n");
		return -1;
	}

	al_register_event_source(q, al_get_display_event_source(d));
	al_register_event_source(q, al_get_timer_event_source(t));
	al_register_event_source(q, al_get_keyboard_event_source());

	al_reserve_samples(2);

	double elapsed = 0.0;
	double pct = 0.0;
	double animation_rate = 4.0;

	bool quit = false;
	bool redraw = true;

	al_play_sample(s, 0.75f, 0.0f, 1.0f, ALLEGRO_PLAYMODE_LOOP, (ALLEGRO_SAMPLE_ID*)0);

	al_start_timer(t);

	while (!quit) {
		if (redraw) {
			al_set_target_backbuffer(d);
			al_clear_to_color(al_map_rgb(0, 0, 0));

			ALLEGRO_TRANSFORM tran;
			/// Draw some text in red
			al_identity_transform(&tran);
			al_translate_transform(&tran, -sw / 2, -sh / 2);
			al_scale_transform(&tran, pct, pct);
			al_translate_transform(&tran, sw / 2, sh / 2);
			al_use_transform(&tran);
			al_draw_multiline_textf(f, al_map_rgb(255, 0, 0), sw / 2, sh / 2 - al_get_font_line_height(f) + 5,
				sw, al_get_font_line_height(f) + 10, ALLEGRO_ALIGN_CENTRE, "Hello Dr.\nMoriarty!");
			/// Draw the same thing in white over the top
			al_identity_transform(&tran);
			al_translate_transform(&tran, -sw / 2, -sh / 2);
			al_scale_transform(&tran, pct, pct);
			al_translate_transform(&tran, sw / 2 + 5, sh / 2 + 5);
			al_use_transform(&tran);
			al_draw_multiline_textf(f, al_map_rgb(255 , 255 , 255), sw / 2, sh / 2 - al_get_font_line_height(f) + 5,
				sw, al_get_font_line_height(f) + 10, ALLEGRO_ALIGN_CENTRE, "Hello Dr.\nMoriarty!");
			
			al_flip_display();
			redraw = false;
		}

		do {
			ALLEGRO_EVENT ev;
			al_wait_for_event(q, &ev);
			if (ev.type == ALLEGRO_EVENT_TIMER) {
				redraw = true;
				elapsed = ev.timer.count*(1.0 / 60.0);
				pct = 1.0 - (sin((2.0*M_PI)*(elapsed / animation_rate) - M_PI) + 1) / 2.0;
				pct *= 3.0;
				if (pct < 0.001) {
					pct = 0.001;
				}
			}
			if ((ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) || (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)) {
				quit = true;
			}
		} while (!al_is_event_queue_empty(q));

	}

   return 0;
} 

