/*
 * rayui.h
 *
 * Collections of functions to aid creating GUIs using raylib
 *
 * HEADER FILE
 *
 * History:
 *      20250902 Creation from imgmover prototype.
 *
 * Author: Dario Rodriguez dario@darionomono.com
 * (c) Dario Rodriguez 2025
 * This program is licensed under the terms of GNU GPL v2.1+
 */


#ifndef RAYUI_H
#define RAYUI_H

#include "raylib.h"

#ifndef FILLXY
#define FILLXY(whxy,valx,valy) (whxy).x=(valx),(whxy).y=(valy)
#endif

#ifndef FILLWH
#define FILLWH(whxy,valw,valh) (whxy).w=(valw),(whxy).h=(valh)
#endif

#ifndef FILLWHXY
#define FILLWHXY(whxy,valw,valh,valx,valy) (whxy).w=(valw),(whxy).h=(valh),(whxy).x=(valx),(whxy).y=(valy)
#endif

#ifndef UNROLLWHXY
#define UNROLLWHXY(whxy) (whxy).w,(whxy).h,(whxy).x,(whxy).y
#endif

#ifndef RD
#define RD 0
#endif

#ifndef WR
#define WR 1
#endif

#define MAXSCROLLABLERECT 8
#define SIZESCROLLABLERECTID 16

typedef struct whxy_t {
        int w;
        int h;
        int x;
        int y;
} whxy_t;

typedef struct font_t {
        Font font;
        int height;
} font_t;

typedef struct menudata_t {
        char *title;
        whxy_t whxy;
        int sizeoptions;
        char **options;
        whxy_t optionswhxy;
        int flag_open;
        int flag_stickyopen;
        int currentoption;
} menudata_t;

typedef struct menubar_t {
        int height;
        int sizemenudata;
        menudata_t **menudata;
        font_t *ptrfont;
} menubar_t;

typedef struct rayui_t {
        int w;
        int h;
        int windowinit;
        menubar_t *menubar;
        const unsigned char *defaultfontdata;
        int sizedefaultfontdata;
        font_t *font;
        font_t *fontbig;
        font_t *fonthuge;
        struct timeval deadline;
        int targetfps;
} rayui_t;

typedef struct scrollrect_t {
        whxy_t whxy;
        char id[SIZESCROLLABLERECTID];
        int scrollpos;
        int (*tryselect)(void *, Vector2);
        void *userptr;
        int scrollthreshold;
} scrollrect_t;

typedef struct mousedata_t {
        Vector2 oldmousepos;
        Vector2 mousepos;
        Vector2 wheel;
        int lmbpressed;
        int lmbreleased;
        int oldlmbdown;
        int lmbdown;
        int oldrmbdown;
        int rmbdown;
        int click_avail;
        int has_mousechanges;
        int needs_nextredraw;
        int is_scrolling;
        char scrollingid[SIZESCROLLABLERECTID];
        int scrollspeed;
        long long scrollstart;
        long long scrolllast;
        Vector2 scrollstartpos;
        int scrollposstart;
        int scrollpos;
        int usedscrollablerect;
        scrollrect_t scrollablerect[MAXSCROLLABLERECT];
} mousedata_t;

rayui_t *rayui_init(int w, int h, char *title, char *menus);
void rayui_free(rayui_t *rayui);

int rayui_scrollablerectreset(rayui_t *rayui, mousedata_t *mousedata);
int rayui_scrollablerectadd(rayui_t *rayui, mousedata_t *mousedata,whxy_t *rect, char *id, int scrollpos, int (*tryselect)(void *userptr, Vector2 mousepos),void *userptr, int scrollthreshold);
int rayui_getmousedata(rayui_t *rayui, mousedata_t *mousedata);
int is_rayui_scrolling(rayui_t *rayui, mousedata_t *mousedata,char *id, int *newscrollpos); /* newscrollpos is filled if it returns true */

menubar_t *rayuimenubar_init(char *menus, font_t *font);
void rayuimenubar_free(menubar_t *menubar);
int rayuimenubar_mouse(menubar_t *menubar, Vector2 mousepos, int lmbpressed, int lmbreleased, int lmbdown, int *click_avail, char **sel_menu, char **sel_submenu);
int rayuimenubar_draw(menubar_t *menubar, int windowwidth, int windowheight, int *needs_nextredraw);

void rayui_settargetfps(rayui_t *rayui, int newfps);
int has_rayui_timeleft(rayui_t *rayui);
void rayui_timereset(rayui_t *rayui);
long long global_currentmilliseconds(void);

font_t *rayuifont_init(int size);
void rayuifont_free(font_t *font);

#ifndef __GNUC__
void global_messagebox(char *format, ...);
#else
        void global_messagebox(char *format, ...) __attribute__ ((format (printf,1,2)));
#endif
Image global_loadimage(const char *filename);

int is_global_insidewhxy(Vector2 pos, whxy_t *whxy, int margin);



#endif