#include "SPISlave.h"
#include "head_oled_i2c.h"
// #include <GyverOLED.h>
// #include <GyverOLED.h>
// GyverOLED<SSD1306_128x64, OLED_NO_BUFFER> oled;


// GyverOLED<SSD1306_128x64, OLED_NO_BUFFER> oled;

static int index = 0;
static int arIndex = 0;
static char data[64];

// void SPI_SlaveInit(void)
// {
//   DDR_SPI = (1 << DD_MISO);
//   SPCR = (1 << SPE) | (1 << SPIE);
// }

// biblary



ISR(SPI_STC_vect)
{
  char received = SPDR;
  data[index] = received;
  index++;
}

void setup()
{
  Serial.begin(9600);
  SPI_SlaveInit();
  initialization();
  
  // oled_init();
  Fill(0);
  SetPage(2);
  update();


  // oled.setScale(3);
  // oled.home();
  
  Serial.println();
  Serial.println("Initialization ");
}

void SetCommand(char *data2, int length){
  char command = data2[0];
  // Отрезать 1 элемент от оставшихся

  Serial.print("\nReceived command: ");
  Serial.println(command, HEX);
  switch (command){
    case 1:
      AllClearCommand(&data2[1], length-1);
      break;
    case 2:
      SetPageCommand(&data2[1], length-1);
      break;

    // case 6:
    //     PrintMassive(&data2[1], length-1);
    //     break;
    case 3:
      AddSymbolCommand(&data2[1], length-1);  
      break;
    case 4:
      DelSymbolCommand(&data2[1], length-1);
      break;
    case 5:
      DrawPixelCommand(&data2[1], length-1);
      PrintMassive(&data2[1], length-1);
      break;
    case 6:
      DrawLineCommand(&data2[1], length-1);
      break;
    case 7:
      DrawCircleCommand(&data2[1], length-1);
      break;
    case 8:
      DrawRectangleCommand(&data2[1], length-1);
      break;

  }
}


void PrintMassive(uint8_t *symbols, int lenght){
  Serial.print("Received Add: ");

 // oled.setScale(3);   // масштаб текста (1..4)
 // oled.home();        // курсор в 0,0
 // oled.print("Привет!");
  for(int i = 0; i < lenght - 1; i++) {
      char str[3];
      sprintf(str, "%01X", symbols[i]);
      Serial.print(str);
      Serial.print(" ");
  }
}

// Another
// command 1
void AllClearCommand(char *symbols, int lenght){
  int param = symbols[0];
  if (param == 0){
    Fill(0);
    update();
    // oled.fill(0);
  }
  else{
    Fill(255);
    update();
    // oled.fill(255);
  }
 }

// command 2
void SetPageCommand(char *symbols, int lenght){
  // int x = symbols[0];
  // int y = symbols[1];
  // oled.setCursor(x,y);
  SetPage(symbols[0]);
  update();
 }

// command 3
void AddSymbolCommand(char *symbols, int lenght) {
  // SetPage(2);
  for (int i = 0; i < lenght - 1; i++) {
    // oled.print(symbols[i]);
    AddSymbol(symbols[i]);
  }
  update();
}

// command 4
void DelSymbolCommand(char *symbols, int lenght) {
  for (int i = 0; i < symbols[0]; i++) {
    DelSymbol();
  }
  
  update();
}

// command 5
void DrawPixelCommand(char *symbols, int lenght){
  int x = symbols[0];
  int y = symbols[1];
  int color = symbols[2];

  DrawPixel(x, y, color);
  update();
  // oled.dot(x,y,color);
}

// command 6
void DrawLineCommand(char *symbols, int lenght){
  int x = symbols[0];
  int y = symbols[1];
  int x1 = symbols[2];
  int y1 = symbols[3];
  int color = symbols[4];

  DrawLine(x, y, x1, y1, color);
  update();
  // oled.line(x,y,x1,y1,color);
}

// command 7
void DrawCircleCommand(char *symbols, int lenght){
  int x = symbols[0];
  int y = symbols[1];
  int r = symbols[2];
  // white on black color=1, fill=0
  int color = symbols[3];
  int fill = symbols[4]; 

  DrawCircle(x, y, r, color, fill);
  update();
  // oled.circle(x,y,r,color);
}

// command 8
void DrawRectangleCommand(char *symbols, int lenght){
  int x = symbols[0];
  int y = symbols[1];
  int width = symbols[2];
  int height = symbols[3];
  // white on black color=1, fill=0
  int color = symbols[4];
  int fill = symbols[5]; 
  DrawRect(x, y, width, height, color, fill);
  update();
  // oled.rect(x,y,x1,y1,color);
}


// Вывод массива
void arrayOut(uint8_t *arr, int size){
  Serial.print("Array: ");
  for(int i = 0; i < size;i++){
    char str[3];
    sprintf(str, "%02X", arr[i]);
    Serial.print(str);
    Serial.print(" ");
  }
  Serial.println(".");
}


void loop() {
  if(PINB & (1 << 2)){
    if(index > 0){

      arrayOut(data, index);
      
      char sum = 0;
      sum = crc8(data, index-1);
      
      char checkNull = 0;
      char last_1 = data[index - 1];
      
      Serial.println(sum, HEX);

      if (last_1 == sum){
        Serial.println();
        Serial.println("Старт вывода массивов");
        Serial.println(sum, HEX);

        SetCommand(data, index);
        // oled.print(data[0]);
        Serial.println("Стоп вывода массивов");
        index = 0;
        return;
      }else{
        Serial.println("Nothing ...");
      
        index = 0;
      }
      
    }
  }
}