#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include "button.h"

LiquidCrystal_I2C lcd(0x27, 16, 2);

button_ button(2, INPUT_PULLUP);

const char* ssid = "Wokwi-GUEST";
const char* password = "";

byte networkItem = 0;
byte netsAmount = 0;

void setup() {
  lcd.init();
  lcd.backlight();

  WiFi.begin(ssid, password);
  lcd.clear();
  lcd.print("Connecting to");
  lcd.setCursor(0, 1);
  lcd.print("Wi-Fi");
  while (WiFi.status() != WL_CONNECTED)
  {
    lcd.setCursor(5, 1);
    lcd.print("   ");
    delay(300);
    lcd.setCursor(5, 1);
    lcd.print('.');
    delay(300);
    lcd.print('.');
    delay(300);
    lcd.print('.');
    delay(300);
  }

  lcd.clear();
  lcd.print("Connected");

  netsAmount = WiFi.scanNetworks();
  lcd.clear();
  lcd.print("Amount of");
  lcd.setCursor(0, 1);
  lcd.print("networks: ");
  lcd.print(netsAmount);
  delay(2000);
  lcdWorker();
}

void lcdWorker()
{
  lcd.clear();
  lcd.print("SSID:");
  lcd.print(WiFi.SSID(networkItem));
  lcd.setCursor(0, 1);
  lcd.print("RSSI:");
  lcd.print(WiFi.RSSI(networkItem));
}

void buttonHandler()
{
  if (button.onPressed()) {
    if (networkItem < (netsAmount - 1)) networkItem++;
    else networkItem = 0;

    lcdWorker();
  }
}

void loop() {
  buttonHandler();
}