#include "menu.h"
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <stdio.h>
#include "renderer.h"
#include "scaller.h"
#include "scene.h"
#include "text.h"

#define BLINK_FREQUENCY 0.00005

static RenderId_t menuObjects[10];
static unsigned menuObjectsSize = 0;

static float blinkDelta = 0;
static bool increaseTextBrightness = false;
static Uint8 textOpacity = 255;

static Mix_Music * music;

Scene_t menuScene = { NULL, 0, 0, 0, 0, 0 };

int menu_staticRender(void){

    MonitorSize_t size = renderer_getMonitorSize();
    float aspectRatio = (float)size.width / size.height;


    SDL_Surface* backgroundImageSurface = IMG_Load("./assets/img/menu_bg.png");
    if(backgroundImageSurface == NULL){
        printf("cannot load background");
        return -1;
    }

    music = Mix_LoadMUS("./assets/music/menu.wav");
	if(music == NULL) {
		fprintf(stderr, "Menu music not found!\n");
		exit(1);
	}

    if(Mix_PlayMusic(music, -1) != 0){
		printf("Music sound could not be played!\n"
			"SDL_Error: %s\n", Mix_GetError());
		Mix_FreeMusic(music);
		running = false;
	}

    RenderObject_t backgroundImage = EMPTY_RENDER_OBJECT;
    strcpy(backgroundImage.name, "BackgroundImg");
    backgroundImage.z = 0;
    backgroundImage.surface = backgroundImageSurface;
    backgroundImage.width = 1;
    backgroundImage.height = 1;
    menuObjects[menuObjectsSize] = scene_addRenderObject(&menuScene, backgroundImage, 0, 0);
    ++menuObjectsSize;

    RenderObject_t legendOfAdam = EMPTY_RENDER_OBJECT;
    strcpy(legendOfAdam.name, "TheLegendOfA");
    SDL_Color white = {255, 255, 255, 255};
    text_renderText(&legendOfAdam, 9, 1.f, white, "./assets/fonts/Quinquefive.ttf", 48, "The legend of adame");
    menuObjects[menuObjectsSize] = scene_addRenderObject(&menuScene, legendOfAdam, CAMERA_WIDTH * 0.095f, CAMERA_HEIGHT * 0.2f);
    ++menuObjectsSize;

    RenderObject_t shadowOfAdam = EMPTY_RENDER_OBJECT;
    strcpy(legendOfAdam.name, "shadowOfAdam");
    SDL_Color black = {0, 0, 0, 255};
    text_renderText(&legendOfAdam, 8, 1.f, black, "./assets/fonts/Quinquefive.ttf", 48, "The legend of adame");
    menuObjects[menuObjectsSize] = scene_addRenderObject(&menuScene, legendOfAdam, CAMERA_WIDTH * 0.085f, CAMERA_HEIGHT * 0.23f);
    ++menuObjectsSize;

    renderer_setScene(&menuScene);
    return 0;
}

int menu_render(float delta){
    for(unsigned i = 3; i < menuObjectsSize; ++i){
        scene_removeRenderObject(&menuScene, menuObjects[i]);
    }
    menuObjectsSize = 3;

    blinkDelta += delta;

    if(increaseTextBrightness && blinkDelta >= BLINK_FREQUENCY){
        textOpacity += 1;
        if(textOpacity == 255) increaseTextBrightness = false;
        blinkDelta = 0;
    }
    else if(!increaseTextBrightness && blinkDelta >= BLINK_FREQUENCY){
        textOpacity -= 1;
        if(textOpacity == 1) increaseTextBrightness = true;
        blinkDelta = 0;
    }

    RenderObject_t startTextShadow = EMPTY_RENDER_OBJECT;
    strcpy(startTextShadow.name, "StartTextShadow");
    SDL_Color black = {0, 0, 0, textOpacity};
    text_renderText(&startTextShadow, 9, 1.f, black, "./assets/fonts/Quinquefive.ttf", 28, "Press space to start");
    menuObjects[menuObjectsSize] = scene_addRenderObject(&menuScene, startTextShadow, CAMERA_WIDTH * 0.245f, CAMERA_HEIGHT * 0.855f);
    ++menuObjectsSize;

    RenderObject_t startText = EMPTY_RENDER_OBJECT;
    strcpy(startText.name, "StartText");
    SDL_Color orange = {255,165,0, textOpacity};
    text_renderText(&startText, 10, 1.f, orange, "./assets/fonts/Quinquefive.ttf", 28, "Press space to start");
    menuObjects[menuObjectsSize] = scene_addRenderObject(&menuScene, startText, CAMERA_WIDTH * 0.25f, CAMERA_HEIGHT * 0.85f);
    ++menuObjectsSize;
    return 0;
}

void menu_remove(void){
    //scene_free(&menuScene);
    Mix_FreeMusic(music);

    menuObjectsSize = 0;
}
