#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>
#include <ThingerESP8266.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32

// Wi-Fi settings
const char* ssid = "Beeline_2G_A7F18A"; 
const char* password = "iD2tAXJN3e"; 

// OTA password
const char* otaPassword = "35539900";

// Thinger.io credentials
#define THINGER_USERNAME "PavelMy"
#define THINGER_DEVICE_ID "ESPPROGECT1"
#define THINGER_DEVICE_CREDENTIAL "Asus35539900"

// Buzzer pin
#define BUZZER_PIN D5 // Replace D5 with the pin you're using

// Button and LED pins
#define BUTTON_PIN D6      // Pin connected to the button
#define LED_PIN D7         // Pin connected to the LED

// Create an instance of the display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// NTP client settings
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 10800, 60000); // Time offset in seconds (3 hours)

// Web server
ESP8266WebServer server(80);

// Thinger.io instance
ThingerESP8266 thing(THINGER_USERNAME, THINGER_DEVICE_ID, THINGER_DEVICE_CREDENTIAL);

// Variables
String alarmTime = "";
bool alarmSet = false;
bool alarmTriggered = false; // Flag to prevent re-triggering in the same minute

String receivedMessage = ""; // To store the message received from Thinger.io

// Variables for scrolling text
int textXPosition = SCREEN_WIDTH; // Starting X position
String scrollMessage = "";

// Variables to track text sizes
int timeTextSize = 2;    // Text size for time
int otherTextSize = 1;   // Text size for alarm and messages

// LED and button state variables
bool ledBlinking = false; // Flag to control LED blinking

// Debounce variables
unsigned long lastButtonPressTime = 0; // Time of the last button press
const unsigned long debounceDelay = 300; // Debounce delay in milliseconds

void handleRoot() {
    String html = 
    "<!DOCTYPE html>"
    "<html>"
    "<head>"
    "<meta name='viewport' content='width=device-width, initial-scale=1.0'/>"
    "<style>"
    "body { font-family: Arial, sans-serif; text-align: center; }"
    "h1 { font-size: 1.5em; }"
    "form { display: inline-block; margin-top: 20px; }"
    "input[type='number'] {"
    "  width: 60px;"
    "  height: 40px;"
    "  font-size: 1.5em;"
    "  text-align: center;"
    "  margin: 5px;"
    "}"
    "input[type='submit'] {"
    "  width: 100%;"
    "  padding: 10px;"
    "  font-size: 1.2em;"
    "}"
    "</style>"
    "</head>"
    "<body>"
    "<h1>Set Alarm</h1>"
    "<form action='/set_alarm' method='POST'>"
    "<div>"
    "Hour: <input type='number' name='hour' min='0' max='23' required>"
    "</div>"
    "<div>"
    "Minute: <input type='number' name='minute' min='0' max='59' required>"
    "</div>"
    "<input type='submit' value='Set Alarm'>"
    "</form>"
    "</body>"
    "</html>";
    server.send(200, "text/html", html);
}

void handleSetAlarm() {
    if (server.method() == HTTP_POST) {
        if (server.hasArg("hour") && server.hasArg("minute")) {
            String hour = server.arg("hour");
            String minute = server.arg("minute");

            // Format hour and minute to ensure two digits
            if (hour.length() < 2) hour = "0" + hour;
            if (minute.length() < 2) minute = "0" + minute;

            alarmTime = hour + ":" + minute;
            alarmSet = true;
            alarmTriggered = false; // Reset the trigger flag

            String html = 
            "<!DOCTYPE html>"
            "<html>"
            "<head>"
            "<meta name='viewport' content='width=device-width, initial-scale=1.0'/>"
            "<style>"
            "body { font-family: Arial, sans-serif; text-align: center; }"
            "h1 { font-size: 1.5em; }"
            "a { display: inline-block; margin-top: 20px; font-size: 1.2em; }"
            "</style>"
            "</head>"
            "<body>"
            "<h1>Alarm set for " + alarmTime + "</h1>"
            "<a href='/'>Back</a>"
            "</body>"
            "</html>";
            server.send(200, "text/html", html);

            Serial.print("Alarm set for: ");
            Serial.println(alarmTime);
        } else {
            server.send(400, "text/html", "Error: Hour and minute not provided.");
        }
    }
}

void setup() {
    Serial.begin(115200);
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
    display.clearDisplay();
    display.setTextWrap(false); // Disable auto line wrapping

    pinMode(BUZZER_PIN, OUTPUT);
    pinMode(BUTTON_PIN, INPUT_PULLUP); // Configure button with pull-up resistor
    pinMode(LED_PIN, OUTPUT);
    digitalWrite(BUZZER_PIN, LOW); // Turn off buzzer
    digitalWrite(LED_PIN, LOW);   // Turn off LED

    Serial.println("Connecting to Wi-Fi...");
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("\nConnected to Wi-Fi");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

    timeClient.begin();
    ArduinoOTA.setPassword(otaPassword);
    ArduinoOTA.begin();

    server.on("/", HTTP_GET, handleRoot);
    server.on("/set_alarm", HTTP_POST, handleSetAlarm);
    server.begin();
    Serial.println("Web server started");

    thing.add_wifi(ssid, password);

    // Resource for receiving messages
    thing["display_message"] << [](pson& in) {
        if (in.is_empty()) {
            in = "Enter your message";
        } else if (in.is_string()) {
            receivedMessage = (const char*) in;  // Save the received message
            scrollMessage = receivedMessage;     // Set the message for scrolling
            textXPosition = SCREEN_WIDTH;        // Reset text position
            Serial.print("Received message: ");
            Serial.println(receivedMessage);
            ledBlinking = true; // Start LED blinking when a new message is received
            digitalWrite(LED_PIN, HIGH); // Turn on LED
            playDoubleBeep(); // Play double beep when a new message is received
        }
    };

    // Resource for setting the alarm
    thing["set_alarm"] << [](pson& in) {
        if (in.is_empty()) {
            in["time"] = alarmTime; // Return the current alarm time
        } else {
            if (!in["time"].is_empty()) {
                alarmTime = (const char*) in["time"];
                alarmSet = true;
                alarmTriggered = false; // Reset the trigger flag
                Serial.print("Alarm set for: ");
                Serial.println(alarmTime);
            }
        }
    };
}

void loop() {
    ArduinoOTA.handle();
    server.handleClient();
    timeClient.update();
    thing.handle();  // Handle incoming messages

    String formattedTime = timeClient.getFormattedTime();

    // Alarm triggering check
    if (alarmSet && formattedTime.substring(0, 5) == alarmTime) {
        if (!alarmTriggered) {
            playAlarmBeep(); // Play the alarm buzzer
            alarmTriggered = true; // Prevent re-triggering in the same minute
            Serial.println("Alarm Triggered!"); // Debug message
        }
    }

    // Handle button press for stopping the LED blinking
    if (digitalRead(BUTTON_PIN) == LOW) { // Button is pressed
        unsigned long currentTime = millis(); // Get the current time
        // Check if sufficient time has passed since the last press
        if (currentTime - lastButtonPressTime > debounceDelay) {
            ledBlinking = false;                // Stop the LED blinking
            digitalWrite(LED_PIN, LOW);        // Ensure the LED is turned off
            scrollMessage = "";                // Clear the scrolling message
            Serial.println("Button pressed, resetting state."); // Debug message
            lastButtonPressTime = currentTime; // Update last button press time
        }
    }

    // LED blinking logic
    if (ledBlinking) {
        static unsigned long lastBlinkTime = 0;
        static bool ledState = LOW; // LED state

        if (millis() - lastBlinkTime > 500) { // Blink every 500 ms
            ledState = !ledState; // Toggle LED state
            digitalWrite(LED_PIN, ledState); // Set LED state
            lastBlinkTime = millis(); // Update last blink time
        }
    }

    display.clearDisplay();

    // Display current time
    display.setTextSize(timeTextSize); // Text size 2
    display.setFont(); // Use default font
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 0); // Cursor position (X, Y)
    display.print(formattedTime); // Use print instead of println

    int timeTextHeight = 8 * timeTextSize; // 8 pixels * text size (2)
    int yPosition = timeTextHeight;  // Start right after the time (Y = 16)

    // Display alarm time if set
    if (alarmSet) {
        display.setTextSize(otherTextSize); // Text size 1
        display.setCursor(0, yPosition);
        display.print("Alarm: ");
        display.print(alarmTime); // Display alarm time
        yPosition += 8 * otherTextSize; // Y increases by 8
    }

    // Display scrolling message
    if (scrollMessage != "") {
        display.setFont(); // Use default font
        display.setTextSize(otherTextSize); // Text size 1

        // Update text position for scrolling
        textXPosition -= 2; // Scrolling speed

        if (textXPosition < -display.width()) {
            textXPosition = SCREEN_WIDTH;
        }

        display.setCursor(textXPosition, yPosition);
        display.print(scrollMessage); // Display scrolling message
    }

    display.display(); // Update the display
    delay(30); // Delay for smooth scrolling
}

void playDoubleBeep() {
    for (int i = 0; i < 2; i++) { // Play two beeps
        digitalWrite(BUZZER_PIN, HIGH);
        delay(100); // Duration of beep
        digitalWrite(BUZZER_PIN, LOW);
        delay(100); // Pause between beeps
    }
}

void playAlarmBeep() {
    unsigned long alarmDuration = 5000; // Alarm duration
    unsigned long startTime = millis();
    Serial.println("Playing alarm beep."); // Debug message

    while (millis() - startTime < alarmDuration) {
        digitalWrite(BUZZER_PIN, HIGH);
        delay(200); // On duration
        digitalWrite(BUZZER_PIN, LOW);
        delay(200); // Off duration
        ArduinoOTA.handle();
        server.handleClient();
        thing.handle();
    }
} добавь в код алгоритм который на сервере организет кнопку которая будет выключать зуммер