#ifndef BOTTOM_HALF_HPP
#define BOTTOM_HALF_HPP

#include "driver/i2c.h"
#include "driver/gpio.h"
#include "esp_log.h"

#define BOTTOM_I2C_NUM I2C_NUM_0
#define BOTTOM_I2C_ADDR 126
#define BOTTOM_PIN_INTERUPT GPIO_NUM_0

#define DELTA_BIT_KP 0
#define DELTA_BIT_BUTTON_SWITCH 1
#define DELTA_BIT_TOUCH 2

/// @brief An enum for the possible keypad buttons.
typedef enum {
    k1 = 0,
    k4 = 1,
    k7 = 2,
    star = 3,
    k2 = 4,
    k5 = 5,
    k8 = 6,
    k0 = 7,
    k3 = 8,
    k6 = 9,
    k9 = 10,
    pound = 11,
    ka = 12,
    kb = 13,
    kc = 14,
    kd = 15,
} KeypadKey;

/// @brief An enum for the possible buttons.
typedef enum {
    b1 = 0,
    b2 = 1,
    b3 = 2,
    b4 = 3,
    button_green = 0,
    button_red = 1,
    button_yellow = 2,
    button_blue = 3,
} ButtonKey;

/// @brief An enum for the possible switches.
typedef enum {
    s1 = 0,
    s2 = 1,
    s3 = 2,
    s4 = 3,
} SwitchKey;

/// @brief Initializes communication with the bottom half.
void init_bottom_half();

/// Clears all pending pressed/released switches/buttons/keys/touch sensors.
void clear_all_pressed_released();

/// @brief Gets the key that was just pressed (if any)
/// @param kp an OUT variable for the key that was pressed (if any)
/// @return true if there was a key that was just pressed
bool get_pressed_keypad(KeypadKey* kp);
/// @brief Gets the key that was just released (if any)
/// @param kp an OUT variable for the key that was released (if any)
/// @return true if there was a key that was just released
bool get_released_keypad(KeypadKey* kp);
/// @brief Converts a `KeypadKey` to a char
/// @param kp The value to convert
/// @return The char representing the key
char char_of_keypad_key(KeypadKey kp);
// TODO: add a get_keypad state?

/// @brief Gets the button that was just pressed (if any)
/// @param button an OUT variable for the button that was pressed (if any)
/// @return true if there was a button that was just pressed
bool get_pressed_button(ButtonKey* button);
/// @brief Gets the button that was just released (if any)
/// @param button an OUT variable for the button that was released (if any)
/// @return true if there was a button that was just released
bool get_released_button(ButtonKey* button);
/// @brief Gets the raw state of the buttons b1-b4 as bitflags.
///  B1 is MSB and B4 is LSB.
/// @return Bitflags for b1-b4.
uint8_t get_button_state();

/// @brief Gets the switch that was just flipped up (if any)
/// @param switch_ an OUT variable for the switch that was flipped up (if any)
/// @return true if there was a switch that was just flipped up
bool get_flipped_up_switch(SwitchKey* switch_);
/// @brief Gets the switch that was just flipped down (if any)
/// @param switch_ an OUT variable for the switch that was flipped down (if any)
/// @return true if there was a switch that was just flipped down
bool get_flipped_down_switch(SwitchKey* switch_);
/// @brief Gets the switch that was just flipped (if any)
/// @param switch_ an OUT variable for the switch that was flipped (if any)
/// @return true if there was a switch that was just flipped
bool get_flipped_switch(SwitchKey* switch_);
/// @brief Gets the raw state of the switches s1-s4 as bitflags.
///  S1 is MSB and S4 is LSB.
/// @return Bitflags for s1-s4.
uint8_t get_switch_state();

/// @brief Gets the state of the fingerprint sensor
/// @return true if the fingerprint sensor is touched
bool get_touch_state();
/// @brief Gets whether or not the touch sensor was just pressed
/// @return true if the touch sensor was just pressed
bool get_touch_pressed();
/// @brief Gets whether or not the touch sensor was just released
/// @return true if the touch sensor was just released
bool get_touch_released();

// TODO: add touch sensor for switch

#endif /* BOTTOM_HALF_HPP */