#include <Arduino.h>
#include "xtensa/core-macros.h"
#include <ESP32-HUB75-MatrixPanel-I2S-DMA.h>

#include "main.h"

#include "caveatbrush9pt7b.h"
#include "overlay.h"

#define R1 42
#define G1 40
#define BL1 41
#define R2 38
#define G2 37
#define BL2 39
#define CH_A 45
#define CH_B 36
#define CH_C 48
#define CH_D 35
#define CH_E 21
#define CLK 2
#define LAT 47
#define OE 14

#define PIN_E 21
#define PANEL_WIDTH 64
#define PANEL_HEIGHT 32         // Panel height of 64 will required PIN_E to be defined.

#define PANELS_NUMBER 1

#define PANE_WIDTH PANEL_WIDTH * PANELS_NUMBER
#define PANE_HEIGHT PANEL_HEIGHT
#define NUM_LEDS PANE_WIDTH*PANE_HEIGHT

#define ONBOARD_LED 13
#define BACK_BUTTON 6


MatrixPanel_I2S_DMA *matrix = nullptr;

int line_pos[PANEL_WIDTH]; // where a rain marker is (y coordinate) 
int line_length[PANEL_WIDTH]; // how long a line is for a given x coord
int line_speed[PANEL_WIDTH]; // how fast each line moves
unsigned long line_last_moved[PANEL_WIDTH];

typedef struct {
    bool reached_bottom = false;
    int y;
    int length;
    int speed;
    unsigned long last_moved;
} Raindrop;

Raindrop raindrops[PANEL_WIDTH];

enum Mode {
  Stealth,
  LowVis,
  HighVis
};

Mode mode = Stealth;
void setup(){
  Serial.begin(BAUD_RATE);
  pinMode(ONBOARD_LED, OUTPUT);
  pinMode(BACK_BUTTON, INPUT_PULLUP);

  for (int i = 0; i<PANEL_WIDTH; i++) {
    raindrops[i].y = 0 - random(24);
    raindrops[i].length = random(20, 28);
    raindrops[i].speed = random(25, 100);
    raindrops[i].last_moved = millis();
    //raindrops[i].reached_bottom = false;
  }
  // redefine pins if required
  HUB75_I2S_CFG::i2s_pins _pins={R1, G1, BL1, R2, G2, BL2, CH_A, CH_B, CH_C, CH_D, CH_E, LAT, OE, CLK};
  HUB75_I2S_CFG mxconfig(PANEL_WIDTH, PANEL_HEIGHT, PANELS_NUMBER, _pins);

  mxconfig.gpio.e = PIN_E;
  mxconfig.driver = HUB75_I2S_CFG::FM6126A;   // for panels using FM6126A chips

  mxconfig.latch_blanking = 4;
  mxconfig.i2sspeed = HUB75_I2S_CFG::HZ_10M;
  mxconfig.clkphase = false;

  mxconfig.double_buff = true;

  matrix = new MatrixPanel_I2S_DMA(mxconfig);
  matrix->begin();
  matrix->setBrightness8(64);
  matrix->fillScreenRGB888(0, 0, 0);
  matrix->setFont(&CaveatBrush_Regular9pt7b);
}

void loop() {
  matrix ->flipDMABuffer();
  matrix->clearScreen();
  delay(25);

  if(!digitalRead(BACK_BUTTON)) {
    switch(mode) {
      case Stealth:
        mode = LowVis;
        break;
      case LowVis:
        mode = HighVis;
        break;
      case HighVis:
        mode = Stealth;
        break;
    }
    while(!digitalRead(BACK_BUTTON)); // Wait for release
  }

  //matrix ->drawBitmap(0, 0, overlay, 64, 32, matrix->color565(64, 0, 0));
  unsigned long timestamp = millis();
  for (int x = 0; x < PANEL_WIDTH; x++) {
    if ((timestamp - raindrops[x].last_moved) > raindrops[x].speed) {
      // step down a pixel
      raindrops[x].y++;
      raindrops[x].last_moved = timestamp;
    }
    if (raindrops[x].y > PANEL_HEIGHT) {
      raindrops[x].y = 0;
      raindrops[x].reached_bottom = true;
    }
    if (raindrops[x].y >= 0) {
      //draw our pixel
      matrix->drawPixel(x, raindrops[x].y, matrix->color565(128, 255, 128));
      // draw our trail
      for (int trail_offset = 1; trail_offset < raindrops[x].length; trail_offset++) {
        int trail_y = raindrops[x].y - trail_offset;
        if (trail_y >= 0) {
          int pixel_num = (trail_y * PANEL_WIDTH) + x;
          if (overlay[ pixel_num / 8 ] & (1 << (7 - (pixel_num % 8)) ) and mode != Stealth) {
            if (mode == HighVis) {
              matrix->drawPixel(x, trail_y, matrix->color565(128, 255, 128));
            } else {
              matrix->drawPixel(x, trail_y, matrix->color565(
                      (128 / raindrops[x].length) * (raindrops[x].length - trail_offset), 
                      (255 / raindrops[x].length) * (raindrops[x].length - trail_offset),
                      (128 / raindrops[x].length) * (raindrops[x].length - trail_offset))
              );
            }
          } else {
            matrix->drawPixel(x, trail_y, matrix->color565(0, (255 / raindrops[x].length) * (raindrops[x].length - trail_offset), 0));
          }
        } else if (raindrops[x].reached_bottom) {
          int pixel_num = ((PANEL_HEIGHT + trail_y) * PANEL_WIDTH) + x;
          if (line_pos[x] >= 0) {

            if (overlay[ pixel_num / 8 ] & (1 << (7 - (pixel_num % 8)) ) and mode != Stealth) {
              if (mode == HighVis) {
                matrix->drawPixel(x, PANEL_HEIGHT + trail_y, matrix->color565(128, 255, 128));
              } else {
                matrix->drawPixel(x, PANEL_HEIGHT + trail_y, matrix->color565(
                      (128 / raindrops[x].length) * (raindrops[x].length - trail_offset), 
                      (255 / raindrops[x].length) * (raindrops[x].length - trail_offset),
                      (128 / raindrops[x].length) * (raindrops[x].length - trail_offset))
                );
              }
            } else {
              matrix->drawPixel(x, PANEL_HEIGHT + trail_y, matrix->color565(0,(255 / raindrops[x].length) * (raindrops[x].length - trail_offset), 0));
            }
          } else {
            break;
          }
        }
      }
    }
  }
}

