#ifndef TFT_DISPLAY_H
#define TFT_DISPLAY_H

#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>

// =============================================================================
// АВТОМАТИЧЕСКАЯ УНИВЕРСАЛЬНАЯ НАСТРОЙКА ПИНОВ ПОД РАЗНЫЕ МК
// =============================================================================

#if defined(ESP8266)
  #define TFT_CS         15  // D8
  #define TFT_RST        -1  
  #define TFT_DC          2  // D4
#elif defined(CONFIG_IDF_TARGET_ESP32C6)
  #define TFT_CS         18  
  #define TFT_RST        -1  
  #define TFT_DC         19  
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
  #define TFT_CS         10  
  #define TFT_RST        -1  
  #define TFT_DC          9  
#elif defined(ESP32) || defined(CONFIG_IDF_TARGET_ESP32)
  #define TFT_CS         5
  #define TFT_RST        -1
  #define TFT_DC         2
#else
  #define TFT_CS         10
  #define TFT_RST         9
  #define TFT_DC          8
#endif

// =============================================================================
// ПОЛНОЦЕННЫЙ КЛАСС-ОБЕРТКА
// =============================================================================

class TFTDisplay {
public:
    // Цветовая палитра 16-бит (565), доступная во всех ваших проектах
    static const uint16_t COLOR_BLACK   = 0x0000;
    static const uint16_t COLOR_BLUE    = 0x001F;
    static const uint16_t COLOR_RED     = 0xF800;
    static const uint16_t COLOR_GREEN   = 0x07E0;
    static const uint16_t COLOR_CYAN    = 0x07FF;
    static const uint16_t COLOR_MAGENTA = 0xF81F;
    static const uint16_t COLOR_YELLOW  = 0xFFE0;  
    static const uint16_t COLOR_WHITE   = 0xFFFF;

    TFTDisplay();
    
    // Инициализация
    void begin(uint8_t rotation = 1);
    
    // Очистка экрана (по умолчанию черным)
    void clear(uint16_t color = COLOR_BLACK);
    
    // Вывод текста (заголовок, подзаголовок, цвет заголовка)
    void showMessage(const char* title, const char* subtitle, uint16_t titleColor = COLOR_GREEN);
    
    // Рисование рамки
    void drawBorder(uint16_t color);

    /// Линия между двумя точками экрана.
    void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);

    /// Закрашенный круг с центром (x, y) и радиусом radius.
    void fillCircle(int16_t x, int16_t y, int16_t radius, uint16_t color);

    /// Текст в произвольной позиции экрана заданным размером и цветом.
    void printAt(int16_t x, int16_t y, const char* text, uint16_t color, uint8_t textSize = 1);

    /// Ширина экрана в пикселях с учётом текущего поворота (setRotation в begin()).
    int16_t width() const;

    /// Высота экрана в пикселях с учётом текущего поворота.
    int16_t height() const;

private:
    Adafruit_ST7735 _tft;
};

// Экспортируем глобальный объект
extern TFTDisplay Display;

#endif // TFT_DISPLAY_H
