#pragma once

#include "layer.h"

#include <span>
#include <unordered_map>

class MainWindow;
//--------------------------------------------------------------
class KeyboardLayer : public Layer
{
  static constexpr int MIN_MIDI_NOTE = 21;      // Minimum MIDI note number for a standard piano (A0)
  static constexpr int MAX_MIDI_NOTE = 108;     // Maximum MIDI note number for a standard piano (C8)

  enum class KeyColor : uint8_t
  {
    White,
    Black
  };
  struct PianoKey
  {
    int               midiNote;     // MIDI note number of the key (0-127)
    KeyColor          color;        // Color of the key (white or black)
    quokka_gfx::FPos  pos;          // Position of the key on the piano
    quokka_gfx::FSize size;         // Size of the key

    bool isPressed;     // Whether the key is currently pressed
  };

  public:
  KeyboardLayer()                                        = delete;      // Default constructor
  virtual ~KeyboardLayer()                               = default;     // Default destructor
  KeyboardLayer(const KeyboardLayer &obj)                = delete;      // Copy constructor
  KeyboardLayer(KeyboardLayer &&obj) noexcept            = delete;      // Move constructor
  KeyboardLayer &operator=(const KeyboardLayer &obj)     = delete;      // Copy assignment operator
  KeyboardLayer &operator=(KeyboardLayer &&obj) noexcept = delete;      // Move assignment operator

  explicit KeyboardLayer(MainWindow &owner, const quokka_gfx::Size &size);     // Constructor

  void initKeys(const quokka_gfx::Size &size);                  // Initialize the piano keys
  void updateActiveNotes(const std::span<const int> notes);     // Update the active notes based on the midi notes (0-127)

  void update(float dt) override {}     // Update the layer state
  void render() const override;         // Render the layer

  protected:
  std::unordered_map<int, PianoKey> m_keys;     // Map of MIDI note to piano key

  private:
  void drawKeys(const quokka_gfx::Renderer *renderer) const;           // Draw the piano keys
  void drawKeySymbols(const quokka_gfx::Renderer *renderer) const;     // Draw symbols on the piano keys (e.g., Middle C marker, ...)
};
//--------------------------------------------------------------
