#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <math.h>

/********************************************************************************/
/*                   SDL PART	:BEGIN						*/
/********************************************************************************/
#include "SDL.h"

#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_IMPLEMENTATION
#define NK_SDL_IMPLEMENTATION
#include "nuklear.h"
#include "nuklear_sdl.h"

extern uint32_t *videoBuffer;
extern int retrow,retroh;

static SDL_Surface *screen_surface;
struct nk_color background;
int running = 1;
struct nk_context *ctx;
float bg[4];

/* ===============================================================
 *
 *                          EXAMPLE
 *
 * ===============================================================*/
/* This are some code examples to provide a small overview of what can be
 * done with this library. To try out an example uncomment the include
 * and the corresponding function. */
/*#include "../style.c"*/
/*#include "../calculator.c"*/
/*#include "../overview.c"*/
/*#include "../node_editor.c"*/


void quit_app(void)
{
	nk_sdl_shutdown();
	SDL_Quit();
}

int init_app(){

    /* SDL setup */
    if( SDL_Init(SDL_INIT_VIDEO) == -1) {
        printf( "Can't init SDL:  %s\n", SDL_GetError( ) );
        return 1;
    }
    screen_surface = SDL_SetVideoMode(retrow, retroh, 32, SDL_SWSURFACE);

    if(screen_surface == NULL) {
        printf( "Can't set video mode: %s\n", SDL_GetError( ) );
        return 1;
    }

    ctx = nk_sdl_init(screen_surface);
    background = nk_rgb(28,48,62);

    /* style.c */
    /*set_style(ctx, THEME_WHITE);*/
    /*set_style(ctx, THEME_RED);*/
    /*set_style(ctx, THEME_BLUE);*/
    /*set_style(ctx, THEME_DARK);*/

    videoBuffer=(unsigned int *)screen_surface->pixels;

	return 0;

}

void checkInput()
{

    /* Input */
    SDL_Event evt;
    nk_input_begin(ctx);
    while (SDL_PollEvent(&evt)) {
        if (evt.type == SDL_QUIT) running=0;
            nk_sdl_handle_event(&evt);
    }
    nk_input_end(ctx);

}


void exec_app(){

        /* GUI */
        
        if (nk_begin(ctx,"Demo", nk_rect(50, 50, 210, 250),
            NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
            NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
        {
            enum {EASY, HARD};
            static int op = EASY;
            static int property = 20;
            static char buffer[64];
            static int len;

            nk_layout_row_static(ctx, 30, 80, 1);
            if (nk_button_label(ctx, "button"))
                fprintf(stdout, "button pressed\n");
            nk_layout_row_dynamic(ctx, 30, 2);
            if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
            if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
            nk_layout_row_dynamic(ctx, 25, 1);
            nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
            nk_edit_string(ctx, NK_EDIT_SIMPLE, buffer, &len, 64, 0);

            
            nk_layout_row_dynamic(ctx, 30, 1);
            nk_label(ctx, "background:", NK_TEXT_LEFT);
            nk_layout_row_dynamic(ctx, 25, 1);
            if (nk_combo_begin_color(ctx, background, nk_vec2(nk_widget_width(ctx),400))) {
                nk_layout_row_dynamic(ctx, 120, 1);
                background = nk_color_picker(ctx, background, NK_RGBA);
                nk_layout_row_dynamic(ctx, 25, 1);
                background.r = (nk_byte)nk_propertyi(ctx, "#R:", 0, background.r, 255, 1,1);
                background.g = (nk_byte)nk_propertyi(ctx, "#G:", 0, background.g, 255, 1,1);
                background.b = (nk_byte)nk_propertyi(ctx, "#B:", 0, background.b, 255, 1,1);
                background.a = (nk_byte)nk_propertyi(ctx, "#A:", 0, background.a, 255, 1,1);
                nk_combo_end(ctx);
            }
        }
        nk_end(ctx);

        /* -------------- EXAMPLES ---------------- */
        /*calculator(ctx);*/
        /*overview(ctx);*/
        /*node_editor(ctx);*/
        /* ----------------------------------------- */

        /* Draw */
        nk_color_fv(bg, background);
        nk_sdl_render(nk_rgb(30,30,30));

	checkInput();
}

/********************************************************************************/
/*                   SDL PART	:END						*/
/********************************************************************************/


