#ifndef STAR_CODE_H
#define STAR_CODE_H

#include <stddef.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>

/// The max length of a starcode (not counting the star)
#define STARCODE_MAX_LEN 9
#define STARCODE_DISPLAY_TEXT_MAX_LEN 20

/// @brief A handler for a specific star code
struct StarCodeEntry {
    /// @brief The star code without the star
    ///
    /// This must be <= 9 characters.
    ///
    /// You may include a * in the code to match on any character
    const char* code;
    /// @brief The text to display when the star code is entered (or null).
    /// 
    /// This must be <= 20 characters.
    const char* display_text;
    /// @brief The number of microseconds to delay when the star code is entered before calling the handler.
    uint64_t delay_us;
    /// @brief The function to call when the star code is entered.
    /// Can be null.
    void (*callback)(void);
    /// @brief The binary semaphore that will be given when this star code is triggered.
    /// Can be null.
    SemaphoreHandle_t triggered_sem;
};

/// @brief Initializes the star code system.
void init_star_code_system();

/// @brief Handles any keypad presses and releases before they bubble up to the rest of the BLK_BOX.
void star_code_handle_keypad(uint16_t* just_pressed, uint16_t* just_released);

/// @brief Adds a star code to be handled.
/// @param code the star code to add
/// @return true iff the star code was added
bool add_star_code(StarCodeEntry code);

/// @brief Adds a list of star codes to be handled.
/// @param codes the list of star codes to add
/// @param len the length of the list
/// @return true iff all the star codes were added
bool add_star_codes(const StarCodeEntry* codes, size_t len);

/// @brief removes a star code to stop handling it.
/// @param code the star code to remove
/// @return true iff the star code was removed
bool rm_star_code(const char* code);

/// @brief removes all given star codes to stop handling them.
/// @param codes the list of star codes to remove
/// @param len the length of the list
/// @return true iff all star codes were removed
bool rm_star_codes(const StarCodeEntry* codes, size_t len);

/// @brief removes all given star codes to stop handling them.
/// @param codes the list of star codes to remove
/// @param len the length of the list
/// @return true iff all star codes were removed
bool rm_star_codes_str(const char** codes, size_t len);

/// @brief clears all star codes.
void clear_star_codes();

/// @brief Triggers the given star code.
/// @param code the star code to trigger (without the *)
/// @return true iff a star code was triggered
bool trigger_star_code(const char* code);


/// @brief Starts/stops the star code system from handling new star codes.
/// If one is being handled currently, it is canceled.
void set_star_code_sys_enabled(bool enable);

/// @return `true` iff the star code system is handling star codes.
bool star_code_sys_enabled();

/// @brief Prints the star code section of the header to the char_lcd. (row 0, columns 0-9)
void lcd_print_header_star_code();

/// @return `true` iff the starcode system is using the full header.
bool lcd_starcode_displaying_result();

#endif /* STAR_CODE_H */