
// webserver stuff  * * * * * * * * * * * * * * * 
#include <WiFi.h>
#include <WebServer.h>
#include "webpage.h"

// ==== your WiFI data ====
const char* ssid = "****";
const char* password = "****";

// ==== webserver on port 80 ====
WebServer server(80);


// VGA  + + + + + + + + + + + + + + + + + 

#include <Arduino.h>
#include <VGA.h>
#include "Font8x8.h"   // load font8x8_basic[96][8]
#include "text_helpers.h"

// resolution
static const int SCREEN_W = 400;
static const int SCREEN_H = 300;

VGA vga;

// colors (RGB565, only 1-bit per chanel → 8 colors)
#define COL_BLACK   0x0000
#define COL_RED     0xF800
#define COL_GREEN   0x07E0
#define COL_BLUE    0x001F
#define COL_CYAN    0x07FF
#define COL_MAGENTA 0xF81F
#define COL_YELLOW  0xFFE0
#define COL_WHITE   0xFFFF

void fillScreen(uint16_t c)
{
  for (int y = 0; y < SCREEN_H; ++y)
    for (int x = 0; x < SCREEN_W; ++x)
      vga.dotFast(x, y, c);
}



// ==== subscirpts ====
void action1() {
  Serial.println("no 1 started");
  arrowLeft();delay(3000);
  arrowRight();delay(2000);

}

void action2() {
  Serial.println("no 2 started");
  allDemo();

}

void action3() {
  Serial.println("no 3 started");
  smiley();

}
void action4() {
  Serial.println("Prices");
  fillScreen(COL_BLACK);
  vga.rect(10, 10, 380, 40, COL_YELLOW);
  vga.rect(10, 50, 380, 240, COL_WHITE);
  drawText8x8(vga, 170, 30, "prices", COL_YELLOW); //headline
  drawText8x8(vga, 30, 80, "water", COL_CYAN);drawText8x8(vga, 250, 80, "€ 1,80", COL_WHITE);
  drawText8x8(vga, 30, 100, "o-juice", COL_CYAN);drawText8x8(vga, 250, 100, "€ 2,40", COL_WHITE);
  drawText8x8(vga, 30, 120, "tonic", COL_CYAN);drawText8x8(vga, 250, 120, "€ 2,80", COL_WHITE);

}

void action5() {
  Serial.println("Clear screen");
  fillScreen(COL_BLACK);
}

// ==== handler for root ====
void handleRoot() {
  server.send_P(200, "text/html", MAIN_page);
}

// ==== handler for buttons ====
void handleCommand() {
  if (server.hasArg("btn")) {
    String btn = server.arg("btn");
    Serial.println("button pressed: " + btn);

    if (btn == "btn1") {
      action1();
    }
    else if (btn == "btn2") {
      action2();
    }
    else if (btn == "btn3") {
      action3();
    }
    else if (btn == "btn4") {
      action4();
    }
    else if (btn == "btn5") {
      action5();
    }

    server.send(200, "text/plain", "OK " + btn);
  } else {
    server.send(400, "text/plain", "Bad Request");
  }
}

// ==== handler for text input ====
void handleText() {
  if (server.hasArg("msg")) {
    String msg = server.arg("msg");
    Serial.println("received text: " + msg);
    server.send(200, "text/plain", "text received: " + msg);
      fillScreen(COL_BLACK);
      drawCenteredText8x8(vga, msg.c_str(), COL_YELLOW, SCREEN_W, SCREEN_H);
  } else {
    server.send(400, "text/plain", "Bad Request");
  }
}

void setup() {
  Serial.begin(115200);

// WIFI-webserver setup # # # # # # # # # # # # 
  WiFi.begin(ssid, password);

  Serial.print("connecting to WIFI ");
  Serial.println(ssid);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("\nWIFI connected!");
  Serial.print("IP Adresse: ");
  Serial.println(WiFi.localIP());

  server.on("/", handleRoot);
  server.on("/command", handleCommand);
  server.on("/sendtext", handleText);

  server.begin();
  Serial.println("webserver started");

// VGA setup  x x x x x x x x x x x x x x x
  // *** PinConfig for XIAO ESP32-S3 with only 1 bit per chanel ***
  const PinConfig pins(
    -1, -1, -1, -1, 5,        // R (1 bit on GPIO3)
    -1, -1, -1, -1, -1, 4,    // G (1 bit on GPIO4)
    -1, -1, -1, -1, 3,        // B (1 bit on GPIO5)
    8, 7                      // HSync=GPIO8, VSync=GPIO7
  );

  // 400x300@60Hz, 16 bpp Framebuffer, 2 Buffers
  vga.init(pins, Mode::MODE_400x300x60, 16, 2);
  vga.start();

  // clear background
  fillScreen(COL_BLACK);

}

void loop() {
  server.handleClient();
}

void arrowLeft() {
  fillScreen(COL_BLACK);
  vga.fillTri(20, 150, 120, 70, 120, 230, COL_GREEN);
  vga.fillRect(120, 100, 250, 100, COL_GREEN);
  delay(10);
}

void arrowRight() {
  fillScreen(COL_BLACK);
  vga.fillTri(380, 150, 280, 70, 280, 230, COL_WHITE);
  vga.fillRect(30, 100, 250, 100, COL_WHITE);
  delay(10);
}

void smiley() { 
  fillScreen(COL_BLACK);
  vga.fillCircle(200, 150, 140, COL_YELLOW);    //head
  vga.fillCircle(130, 120, 15, COL_BLACK);vga.fillCircle(270, 120, 15, COL_BLACK); //eyes
  vga.ellipse(200, 190, 100, 50, COL_BLACK);    //smile
  vga.fillEllipse(200, 180, 105, 50, COL_YELLOW);
  delay(10);
}

void allDemo() {
  // lines
  fillScreen(COL_BLACK);
  vga.xLineFast(10, 20, 200, COL_WHITE);
  vga.line(20, 40, 320, 200, COL_RED);
  delay(2000);

  // triangle
  fillScreen(COL_BLACK);
  vga.tri(30, 50, 100, 220, 110, 90, COL_GREEN);
  vga.fillTri(190, 160, 200, 290, 300, 90, COL_BLUE);
  delay(2000);

  // rectangle
  fillScreen(COL_BLACK);
  vga.rect(20, 10, 220, 90, COL_CYAN);
  vga.fillRect(80, 190, 190, 160, COL_RED);
  delay(2000);


  // circle
  fillScreen(COL_BLACK);
  vga.circle(30, 150, 140, COL_MAGENTA);
  vga.fillCircle(280, 280, 110, COL_YELLOW);
  delay(2000);

  // ellipse
  fillScreen(COL_BLACK);
  vga.ellipse(90, 140, 80, 60, COL_WHITE);
  vga.fillEllipse(220, 200, 120, 40, COL_BLUE);
  delay(2000);

    //fillScreen(COL_BLACK);
}

/*
VGA COMMANDS
=============

	void xLineFast(int x0, int x1, int y, int rgb);
	void line(int x1, int y1, int x2, int y2, int rgb);
	void tri(int x1, int y1, int x2, int y2, int x3, int y3, int rgb);
	void fillTri(int x1, int y1, int x2, int y2, int x3, int y3, int rgb);
	void squircle(int center_x, int center_y, int a, int b, float n, int rgb);
	void fillSquircle(int center_x, int center_y, int a, int b, float n, int rgb);
	void fillRect(int x, int y, int w, int h, int rgb);
	void rect(int x, int y, int w, int h, int rgb);
	void circle(int x, int y, int r, int rgb);
	void fillCircle(int x, int y, int r, int rgb);
	void ellipse(int x, int y, int rx, int ry, int rgb);
	void fillEllipse(int x, int y, int rx, int ry, int rgb);
	void mouse(int x1, int y1);
	
	void scrollText(int dy);
	void get(int x, int y);
	void dotAdd(int x, int y, int rgb);
	void scrollWindow(int x, int y, int w, int h, int dx, int dy);	
	void scrollFastWindow(int dx, int dy);

  */