/**
 * Copyright (c) 2021 Dominic Masters
 * 
 * This software is released under the MIT License.
 * https://opensource.org/licenses/MIT
 */

#pragma once
#include "../libs.h"
#include "../input/input.h"
#include "../epoch/epoch.h"
#include "../util/array.h"
#include "../engine/engine.h"

/** The maximum number of items a menu can hold */
#define MENU_ITEMS_MAX 32

/** Callback for when menu events are fired. */
typedef void menucallback_t(void *user, uint8_t i);

typedef struct {
  uint8_t x;
  uint8_t y;
  uint8_t width;
  uint8_t height;
} menuitem_t;

typedef struct {
  menuitem_t items[MENU_ITEMS_MAX];
  uint8_t itemCount;
  uint8_t selected;
  uint8_t cursorX;
  uint8_t cursorY;

  void *user;
  menucallback_t *onSelect;
} menu_t;

/**
 * Initialize a menu.
 * 
 * @param menu Menu to initialize.
 * @param columns Count of rows.
 * @param rows Count of columns.
 */
void menuInit(menu_t *menu);

/**
 * Updates the menu to handle inputs.
 * 
 * @param menu Menu to update.
 * @param engine Engine to update from.
 */
void menuUpdate(menu_t *menu, engine_t *engine);

/**
 * Add an item to the menu
 * 
 * @param menu Menu to add to.
 * @return Item to add to.
 */
menuitem_t * menuAdd(menu_t *menu);