#ifndef SHIELD_RICHSHIELD2_H
#define SHIELD_RICHSHIELD2_H

#include <Arduino.h>
#include "KK_ButtonHandler.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <TM1651.h>
#include <FastLED.h>

class Shield_RichShield2 {
  public:
    Shield_RichShield2();
    
    void begin();
    void update(); // Muss in den loop()

    // --- Encoder & Taster ---
    KK_ButtonHandler btnEncoder;
    int getEncoderPosition();
    void resetEncoder();

    // --- Sensoren ---
    float getTemperature();
    long getDistance();
    bool isMotionDetected();
    int getMicLevel();
    int getMicPercent();

    // --- Joystick ---
    int getJoyX();
    int getJoyY();
    String getJoyDirection();

    // --- Ausgaben ---
    void setBatteryLevel(uint8_t level); // 0-7
    void setBatteryPercent(uint8_t percent); // 0-100%
    void setRGB(uint8_t index, uint8_t r, uint8_t g, uint8_t b);
    void clearRGB();

    // --- 16x8 Matrix ---
    void showMatrixText(String text);
    void showMatrixTextScroll(String text, int speed); // <-- DIESE ZEILE IST NEU
    void showMatrixNumber(int num);
    void setMatrixPixel(int x, int y, bool state);
    void clearMatrix();

  private:
    OneWire _oneWire;
    DallasTemperature _sensors;
    TM1651 _batteryDisplay;
    Adafruit_8x16matrix _matrix;
    CRGB _leds[4];

    // Hardware Pins
    const int _pinEncCLK = 2;
    const int _pinEncDT  = 3;
    const int _pinUsTrig = 7;
    const int _pinUsEcho = 8;
    const int _pinPIR    = 6;
    const int _pinJoyX   = A0;
    const int _pinJoyY   = A1;
    const int _pinMic    = A2;
    
    // Interne State-Variablen für den Encoder
    int _encoderPos;
    int _lastEncCLK;
};

#endif