#include <WiFi.h>
// #include <DNSServer.h>
// #include <WebServer.h>
// #include <WiFiManager.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// 'modem_3999250', 40x40px
const unsigned char modemIcon [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x01, 0xff, 0x80, 0x00, 0x00,
  0x07, 0x00, 0xe0, 0x00, 0x00, 0x0c, 0x18, 0x30, 0x00, 0x30, 0x08, 0xff, 0x10, 0x0c, 0x78, 0x01,
  0xc3, 0x80, 0x1e, 0xcc, 0x03, 0x00, 0xc0, 0x33, 0xcc, 0x00, 0x3c, 0x00, 0x33, 0xc6, 0x00, 0x66,
  0x00, 0x63, 0x46, 0x00, 0x42, 0x00, 0x62, 0x63, 0x00, 0x00, 0x00, 0xc6, 0x23, 0x00, 0x00, 0x00,
  0xc4, 0x31, 0x00, 0x00, 0x00, 0x8c, 0x31, 0x80, 0x00, 0x01, 0x8c, 0x18, 0x80, 0x00, 0x01, 0x18,
  0x18, 0xc0, 0x00, 0x03, 0x18, 0x0c, 0xc0, 0x00, 0x03, 0x30, 0x0c, 0x60, 0x00, 0x06, 0x30, 0x04,
  0x60, 0x00, 0x06, 0x20, 0x06, 0x20, 0x00, 0x04, 0x60, 0x02, 0x30, 0x00, 0x0c, 0x60, 0x03, 0x10,
  0x00, 0x08, 0xc0, 0x03, 0x18, 0x00, 0x18, 0xc0, 0x7f, 0xff, 0xff, 0xff, 0xfe, 0xc0, 0x00, 0x00,
  0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x03, 0xc3, 0x80, 0x00, 0x00, 0x03, 0xc7, 0xc0, 0x00, 0x00,
  0x03, 0xcc, 0xc0, 0x39, 0xce, 0x73, 0xcc, 0xc0, 0x39, 0xce, 0x73, 0xc7, 0xc0, 0x00, 0x00, 0x03,
  0xc3, 0x80, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x70, 0x00, 0x00, 0x00, 0x0e, 0x3f,
  0xff, 0xff, 0xff, 0xfc, 0x0e, 0x00, 0x00, 0x00, 0x30, 0x04, 0x00, 0x00, 0x00, 0x20, 0x04, 0x00,
  0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00
};

// 'wifi_2983768', 30x30px
const unsigned char wifiIcon [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x3f, 0xf0, 0x00, 0x01, 0xff, 0xfe, 0x00, 0x07, 0xff, 0xff, 0x80, 0x1f, 0xe0, 0x1f, 0xe0,
  0x7f, 0x00, 0x03, 0xf8, 0xfc, 0x3f, 0xf0, 0xfc, 0xf0, 0xff, 0xfc, 0x3c, 0xe3, 0xff, 0xff, 0x1c,
  0x47, 0xf0, 0x3f, 0x88, 0x0f, 0x80, 0x07, 0xc0, 0x0f, 0x1f, 0xe3, 0xe0, 0x0c, 0x7f, 0xf8, 0xc0,
  0x00, 0xff, 0xfc, 0x00, 0x01, 0xff, 0xfe, 0x00, 0x01, 0xe0, 0x1e, 0x00, 0x00, 0x80, 0x04, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x07, 0x80, 0x00,
  0x00, 0x07, 0x80, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

const unsigned char flow2222 [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x00, 0x00, 0x07, 0xff, 0xc0, 0x38, 0x3f, 0xc3, 0xe0, 
  0x3f, 0xfe, 0x00, 0xe0, 0x1f, 0xf8, 0x00, 0x00, 0x07, 0xc0, 0x7e, 0x00, 0x00, 0x03, 0xff, 0x80, 
  0x10, 0x0f, 0xff, 0xc0, 0x3e, 0xff, 0x81, 0xe0, 0x3f, 0xfc, 0x00, 0x40, 0x0f, 0xf0, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char fire [] PROGMEM = {
  0x00, 0x00, 0x06, 0x00, 0x06, 0x00, 0x09, 0x00, 0x11, 0x40, 0x21, 0xc0, 0x20, 0x20, 0x20, 0x20, 
  0x20, 0x20, 0x20, 0x20, 0x10, 0x40, 0x0f, 0x80
};
const unsigned char hum [] PROGMEM = {
  0x00, 0x00, 0x00, 0x08, 0x00, 0x60, 0x1c, 0x00, 0x70, 0x1e, 0x00, 0xf0, 0x3e, 0x10, 0xf8, 0x3e, 
  0x39, 0xf8, 0x3e, 0x39, 0xf8, 0x3e, 0x7c, 0xf0, 0x0c, 0x7c, 0x60, 0x00, 0x7c, 0x00, 0x00, 0x7c, 
  0x00, 0x00, 0x38, 0x00
};

void centerText(String text, int yPos) {
  int textWidth = text.length() * 6; // Estimasi lebar teks dengan huruf monospasi
  int xPos = (SCREEN_WIDTH - textWidth) / 2;
  display.setCursor(xPos, yPos);
  display.println(text);
}

void header() {
  display.setTextSize(1);
  display.setTextColor(WHITE);
  centerText("Ren-Project", 0);
  display.drawLine(0, 9, 128, 9, WHITE);
}

void refresh() {
  display.display();
  delay(1000); // Ubah delay sesuai kebutuhan Anda
  display.clearDisplay();
}

void startAP(const unsigned char* bitmap, int bitmapWidth, int bitmapHeight) {
  header();

  int bitmapX = (SCREEN_WIDTH - bitmapWidth) / 2;
  int bitmapY = 12;
  display.drawBitmap(bitmapX, bitmapY, bitmap, bitmapWidth, bitmapHeight, WHITE);

  centerText("Starting AP ...", 55);

  refresh();
}

void infoAP(String ssid, IPAddress ip) {
  header();
  centerText("Access Point Started", 12);
  // Display SSID on the OLED
  centerText(ssid, 25);

  // Display IP Address on the OLED
  centerText(ip.toString(), 35);

  centerText("Connect for Config !!", 50);
  refresh();
}
float vol=0.0;
float volm=0.0;
String lv="";
String lh="";
String sema="";
String semb="";
int h=0;
float t=0;
volatile int flow_frequency;
float l_hour; // Calculated litres/hour
unsigned char flowsensor = 14; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;

void setup() {
  Serial.begin(115200);
  Serial.println("\nStarting...");

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x64)
  display.clearDisplay();

//   IPAddress ip(192, 168, 4, 1);
//   startAP(modemIcon, 40, 40);
//   delay(5000);
//   display.clearDisplay();
//   infoAP("ESP32_CONFIG", ip);
// }
  display.drawBitmap(5, 24, flow2222, 25, 20, WHITE);
  display.drawBitmap(5, 49, fire, 12, 12, WHITE);
  display.drawBitmap(70, 49, hum, 22, 12, WHITE);
  display.drawRect(0, 0, 128, 22, SSD1306_WHITE);
  display.drawRect(0, 24, 128, 22, SSD1306_WHITE);
  display.drawRect(0, 47, 63, 17, SSD1306_WHITE);
  display.drawRect(65, 47, 63, 17, SSD1306_WHITE);
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
   lv="";  
  double sementara=vol/1000;
  
  //vol=
  if(sementara<10){
    lv+="000000";
  }
  else if(sementara<100){
    lv+="0000";
  }
  else if(sementara<1000){
    lv+="0000";
  }
  else if(sementara<10000){
    lv+="000";
  }
  else if(sementara<100000){
    lv+="00";
  }
  else if(vol<1000000){
    lv+="0";
  }
  
  
  lv+=String(sementara,2);
  //lv+="m3";
  
  display.setCursor(4,4);
  display.print(lv);
  
  //Serial.println(lv);
  sema=lv;
  sema+="m3";
  lv="";
  display.setCursor(40,28);
  
  if(l_hour<10){
    lh+="000";
  }
  else if(l_hour<100){
    lh+="00";
  }
  else if(l_hour<1000){
    lh+="0";
  }
  int bulat=l_hour;
  lh+=String(bulat);
  lh+=" L/m";
  
  display.print(lh);
  semb=lh;
  lh="";
  display.setTextSize(1);
  display.setCursor(23,52);
  String suhu=String(t,1);
  suhu+=" C";
  display.print(suhu);
  display.setCursor(99,52);
  String hum=String(h);
  hum+=" %";
  display.print(hum);
  display.display();   
}

void loop() {
  // Your main loop code (if needed)
}