/*
  ESP32 HTTPClient Jokes API Example

  https://wokwi.com/projects/342032431249883731

  Copyright (C) 2022, Uri Shaked
*/

#include <ArduinoJson.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_GFX.h>
#include <Wire.h>

#define BTN_PIN 5
#define BTN_COL 14
#define BTN_HEL 12
#define PIN 4
#define TFT_DC 2
#define TFT_CS 15
#define NUMPIXELS      16
#define SerialMon Serial
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int wach = 1;
int r;

int red;
int green;
int blue;
int color = 1;
int w = 0;
int hell = 10;
int wh = 0;
int whel =  3;
int rm;


void wakeup() {
      tft.fillRoundRect(80, 180, 140, 20, 10, ILI9341_PINK);
    tft.fillRoundRect(20, 85, 60, 10, 20, ILI9341_PINK);
    tft.fillRoundRect(220, 85, 60, 10, 20, ILI9341_PINK);
    delay(800);
    tft.fillRoundRect(20, 75, 60, 20, 20, ILI9341_PINK);
    tft.fillRoundRect(220, 75, 60, 20, 20, ILI9341_PINK);
    delay(600);
        tft.fillRect(20, 75, 60, 10, ILI9341_BLACK);
        tft.fillRect(220, 75, 60, 10, ILI9341_BLACK);
    delay(500);
    tft.fillRoundRect(20, 75, 60, 20, 20, ILI9341_PINK);
    tft.fillRoundRect(220, 75, 60, 20, 20, ILI9341_PINK);
    delay(600);
        tft.fillRect(20, 75, 60, 10, ILI9341_BLACK);
        tft.fillRect(220, 75, 60, 10, ILI9341_BLACK);
    delay(500);
    tft.fillRoundRect(20, 75, 60, 20, 20, ILI9341_PINK);
    tft.fillRoundRect(220, 75, 60, 20, 20, ILI9341_PINK);
    delay(600);
    tft.fillRoundRect(20, 65, 60, 30, 20, ILI9341_PINK);
    tft.fillRoundRect(220, 65, 60, 30, 20, ILI9341_PINK);
    delay(600);
    tft.fillRoundRect(20, 55, 60, 40, 20, ILI9341_PINK);
    tft.fillRoundRect(220, 55, 60, 40, 20, ILI9341_PINK);
    delay(500);
        tft.fillRect(20, 45, 60, 50, ILI9341_BLACK);
    tft.fillRect(220, 45, 60, 50, ILI9341_BLACK);
    tft.fillRoundRect(20, 45, 60, 50, 20, ILI9341_PINK);
    tft.fillRoundRect(220, 45, 60, 50, 20, ILI9341_PINK);
        for (int i=0; i < NUMPIXELS; i++) {
      pixels.setPixelColor(i, pixels.Color(red/10*hell, green/10*hell, blue/10*hell));

    // This sends the updated pixel color to the hardware.
    pixels.show();

    // Delay for a period of time (in milliseconds).
    delay(100);
    }
}



void blink() {
    tft.fillRect(20, 45, 60, 25, ILI9341_BLACK);
    tft.fillRect(220, 45, 60, 25, ILI9341_BLACK);
      delay(100);
    tft.fillRect(20, 45, 60, 50, ILI9341_BLACK);
    tft.fillRect(220, 45, 60, 50, ILI9341_BLACK);
      delay(200);
        tft.fillRect(20, 45, 60, 50, ILI9341_BLACK);
    tft.fillRect(220, 45, 60, 50, ILI9341_BLACK);
    tft.fillRoundRect(20, 45, 60, 50, 20, ILI9341_PINK);
tft.fillRoundRect(220, 45, 60, 50, 20, ILI9341_PINK);
}

void setup() {
    SerialMon.begin(115200);
  pinMode(BTN_PIN, INPUT_PULLUP);
    pinMode(BTN_COL, INPUT_PULLUP);
    pinMode(BTN_HEL, INPUT_PULLUP);
  tft.begin();
  tft.setRotation(1);
    pixels.begin();
              for (int i=0; i < NUMPIXELS; i++) {
      pixels.setPixelColor(i, pixels.Color(0, 0, 0));

    // This sends the updated pixel color to the hardware.
    pixels.show();
blue = 255;
green = 255;
red = 255;
    // Delay for a period of time (in milliseconds).
    delay(100);
    
    }
        tft.fillScreen(ILI9341_BLACK);
    tft.setCursor(0, 0);
    wakeup();
}

void loop() {
    if (wach == 0) {
  if (digitalRead(BTN_PIN) == LOW) {
    tft.fillScreen(ILI9341_BLACK);
    tft.setCursor(0, 0);
    wakeup();
    wach = 1;
  }
  }else if (wach == 1) {
    if (digitalRead(BTN_PIN) == LOW) {
          tft.fillScreen(ILI9341_BLACK);
      tft.setCursor(0, 0);
          for (int i=0; i < NUMPIXELS; i++) {
      pixels.setPixelColor(i, pixels.Color(0, 0, 0));

    // This sends the updated pixel color to the hardware.
    pixels.show();

    // Delay for a period of time (in milliseconds).
    delay(10);
    }
          wach = 0;
    delay(1000);
     }
     if(wach == 1) {
     if (digitalRead(BTN_COL) == LOW){
                SerialMon.println("working");
      if (w == 0) {
      if (color == 0) {
        red = 255;
        blue = 255;
        green = 255;
        color = 1;
        w = 1;
      }
      }
      if (w == 0) {
      if (color == 1) {
        red = 255;
        blue = 255;
        green = 0;
        color = 2;
        w = 1;
      }
      }
        if (w == 0) {
        if (color == 2) {
        red = 255;
        blue = 0;
        green = 255;
        color = 3;
        w = 1;
      }
      }
        if (w == 0) {
        if (color == 3) {
        red = 0;
        blue = 255;
        green = 255;
        color = 0;
        w = 1;
      }
      }
     }
     w = 0;
             for (int i=0; i < NUMPIXELS; i++) {
      pixels.setPixelColor(i, pixels.Color(red/10*hell, green/10*hell, blue/10*hell));

    // This sends the updated pixel color to the hardware.
    pixels.show();

    // Delay for a period of time (in milliseconds).
    delay(100);
    }
        delay(500);
  }
     if (digitalRead(BTN_HEL) == LOW){
                SerialMon.println(hell);
        if (wh == 0) {
        if (whel == 2) {
        hell = 10;
        whel = 3;
        wh = 1;
      }
      }
        if (wh == 0) {
        if (whel == 3) {
        hell = 6;
        whel = 4;
        wh = 1;
      }
      }
        if (wh == 0) {
        if (whel == 4) {
        hell = 2;
        whel = 2;
        wh = 1;
      }
      }
      wh = 0;
             for (int i=0; i < NUMPIXELS; i++) {
      pixels.setPixelColor(i, pixels.Color(red/10*hell, green/10*hell, blue/10*hell));

    // This sends the updated pixel color to the hardware.
    pixels.show();

    // Delay for a period of time (in milliseconds).
    delay(100);
    }
     }
  }

  delay(100);
    if (wach == 1){
  blink();
  randoms();
  randomsm();
            SerialMon.println(r);
                        SerialMon.println(rm);
        delay(r);
        if(rm == 1){
                tft.fillRoundRect(80, 180, 140, 20, 10, ILI9341_PINK);
        }else if(rm==2){
                tft.fillRoundRect(80, 180, 140, 20, 10, ILI9341_BLACK);
              tft.fillRoundRect(120, 180, 60, 20, 10, ILI9341_PINK);
        }
    }
    }
void randoms() {
      r = random(500, 2000);
}
void randomsm() {
          rm = random(1, 3);
}