#include "menu.h"

CMenu::CMenu()
{
	allegro_init();
	set_color_depth(32);
	set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
	alfont_init();
	install_timer();
	install_keyboard();
	install_joystick(JOY_TYPE_AUTODETECT);

    titulo = load_bmp(IMAGEM_TITULO, NULL);
    fontMenu = alfont_load_font(MENU_FONT_NAME);
    alfont_set_font_size(fontMenu, MENU_FONT_SIZE);
    foco = 0;
    colorFocusOn = makecol(254, 224, 14);
    colorFocusOff = makecol(255, 255, 255);
    gkernel = new CGKernel;
}


CMenu::~CMenu()
{
    delete gkernel;
    destroy_bitmap(titulo);
    alfont_destroy_font(fontMenu);
	clear_keybuf();
	alfont_exit();
	allegro_exit();
}

void CMenu::Go()
{
    #define FUNDO_X 266
    #define FUNDO_Y 242
    #define FUNDO_W 107
    #define FUNDO_H 114
    BITMAP* buffer = create_bitmap(FUNDO_W, FUNDO_H);
	bool sair = false;
	bool precisaAtualizar = true;
	while(!sair)
	{
	    poll_joystick();
        if ((key[KEY_UP] || joy[0].stick[0].axis[1].d1) && foco > 0)
        {
            precisaAtualizar = true;
            foco--;
        }
        if ((key[KEY_DOWN] || joy[0].stick[0].axis[1].d2) && foco < NUM_OPCOES - 1)
        {
            precisaAtualizar = true;
            foco++;
        }
        if (key[KEY_ENTER])
        {
            switch(foco)
            {
                case 0:
                    gkernel->Go();
                    blit(titulo, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
                    precisaAtualizar = true;
                    break;

                case 1:
                    // Options->Go();
                    break;

                case 2:
                    sair = true;
                    break;
            }
        }

        if (precisaAtualizar)
        {
            blit(titulo, buffer, FUNDO_X, FUNDO_Y, 0, 0, FUNDO_W, FUNDO_H);
            alfont_textout_centre_aa_ex(buffer, fontMenu, OPCAO_1, buffer->w / 2, 0, (foco == 0 ? colorFocusOn : colorFocusOff), -1);
            alfont_textout_centre_aa_ex(buffer, fontMenu, OPCAO_2, buffer->w / 2, MENU_FONT_SIZE, (foco == 1 ? colorFocusOn : colorFocusOff), -1);
            alfont_textout_centre_aa_ex(buffer, fontMenu, OPCAO_3, buffer->w / 2, MENU_FONT_SIZE * 2, (foco == 2 ? colorFocusOn : colorFocusOff), -1);
            blit(buffer, screen, 0, 0, FUNDO_X, FUNDO_Y, FUNDO_W, FUNDO_H);
            precisaAtualizar = false;
            rest(150);
        }
	}
	destroy_bitmap(buffer);
}

void CMenu::Apresentacao()
{
	BITMAP* logo = load_bmp(IMAGEM_APRESENTACAO, NULL);
	BITMAP* buffer = create_bitmap(SCREEN_W, SCREEN_H);

	clear_to_color(screen, makecol(255, 255, 255));
	blit(logo, screen, 0, 0, SCREEN_W / 2 - logo->w / 2, SCREEN_H / 2 - logo->h / 2, logo->w, logo->h);
	bool joypad_button_pressed = false;
    while(!keypressed() && !joypad_button_pressed)
    {
        poll_joystick();
        for (int c = 0; c < joy[0].num_buttons; c++)
        {
            if (joy[0].button[c].b)
                joypad_button_pressed = true;
        }
    }
    rest(150);
	clear_keybuf();
    for (int alpha = 254; alpha > 0; alpha--)
    {
    	poll_joystick();
        for (int c = 0; c < joy[0].num_buttons; c++)
        {
            if (joy[0].button[c].b)
                joypad_button_pressed = true;
        }
    	if (keypressed() || joypad_button_pressed)
    		alpha = 0;
        clear_to_color(buffer, makecol(255, 255, 255));
        set_trans_blender(0, 0, 0, alpha);
        draw_trans_sprite(buffer, logo, SCREEN_W / 2 - logo->w / 2, SCREEN_H / 2 - logo->h / 2);
        set_trans_blender(0, 0, 0, 255 - alpha);
        draw_trans_sprite(buffer, titulo, 0, 0);
        blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
        vsync();
    }
	destroy_bitmap(buffer);
	destroy_bitmap(logo);
	rest(150);
}
