#pragma once

#include "../Helper/delay_helper.h"
#include "stm32h5xx_hal_gpio.h"
#include <cstdint>

#include "../Helper/gpio_helper.hpp"

class Key {
private:
    static constexpr uint32_t kDebounceDelay = 20;
    GpioHelper::Gpio gpio;
    GPIO_PinState normalStatus;

public:
    explicit Key(GpioHelper::Gpio gpio, GPIO_PinState normalStatus) : gpio(gpio), normalStatus(normalStatus) {
        GpioHelper::GpioInit(gpio, GPIO_MODE_INPUT, normalStatus ? GPIO_PULLUP : GPIO_PULLDOWN);
    }
    ~Key()                     = default;
    Key(const Key&)            = delete;
    Key& operator=(const Key&) = delete;
    Key(Key&&)                 = delete;
    Key& operator=(Key&&)      = delete;

    bool IsPressed() {
        auto reading = gpio.Read();
        if (reading != normalStatus) {
            DelayMs(kDebounceDelay);
            reading = gpio.Read();
            if (reading != normalStatus) {
                return true;
            }
        }
        return false;
    }
};
