#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

// U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);

#define FONT u8g2_font_wqy14_t_gb2312b

static const char *const tickers[] = {
  "BBAS3", "PETR4",
  "WEGE3", "HAPV3",
  "MGLU3", "NTCO3",
  "VIIA3", "ABEV3",
  "FNAM11", "BBDC4",
  "B3SA3", "LREN3",
  "ASAI3", "COGN3"
};

uint16_t total_lines = 0;

void tela_variacao_acoes()
{
  u8g2.setFont(u8g2_font_profont12_tf);
  u8g2.setFontMode(1);
  u8g2.setDrawColor(1);

  // Calculate the total height of the scrolling text
  int16_t totalHeight = u8g2.getDisplayHeight();
  u8g2.setFontPosTop();
  u8g2.clearBuffer();
  int linha = 1;
  uint8_t charHeight = u8g2.getMaxCharHeight();

  for (int yOffset = totalHeight / 2; yOffset > -totalHeight; yOffset++) {
    u8g2.clearBuffer();
    for (int i = 0; i < total_lines; i += 2) // draw visible lines
    {
      u8g2.drawUTF8(0, (linha * charHeight - charHeight) - (yOffset), tickers[i]);
      u8g2.drawUTF8(69, (linha * charHeight - charHeight) - (yOffset), tickers[i + 1]);
      linha++;
    }
    u8g2.sendBuffer();
    delay(1000);

    Serial.println((linha * charHeight - charHeight) - (yOffset));
    if (yOffset == totalHeight) {
      yOffset = totalHeight / 2;
    }
  }
}

void setup(void)
{
  Serial.begin(115200);
  u8g2.begin();
  total_lines = sizeof(tickers) / sizeof(tickers[0]);
}

void loop(void)
{
  tela_variacao_acoes();
}