#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <ESP32Servo.h>
#include "HX711.h"
#include <iostream>
#include <thread> 
#include <chrono>

//--------------------------------- Variable Setup ---------------------------------------
// Ballance : 
const int LOADCELL_DOUT_PIN = 16;
const int LOADCELL_SCK_PIN = 22;
HX711 scale;
// Écran Initalisation ----------------------------------------------
int height = 240 ;
int width = 320; 
#define TFT_DC 2  
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define flipVertical 1
// Manche boutons | on donne les port -------------------------------
#define btn_plus 27
#define btn_moins 26
#define btn_ok 35
#define btn_R 32
#define btn_L 33
// Bouton dans le calculateur | on donne les port --------------------
#define btn_home 25
#define btn_ballast 14
// pour les ballast ( le servo moteur )-----------------------------
int pos = 0;
int counter = 0;
const int servoPin = 16;
Servo servo;
//--------------------------------- Variable Écrant --------------------------------------- 
int windo = 0;  
String pages[] = {"Mode","Parametres","Ballasts","Reservoirs","Transition","Ascendance"};
//--------------------------------- Variable planeur --------------------------------------- 
int V_ballast = 0;

void page(bool titre_page,String titre, bool haut_page, String text_haut);
void page(bool titre_page,String titre, bool haut_page, String text_haut, bool resteFull) {
  if (resteFull == true){
    tft.fillScreen(ILI9341_BLACK); 
  }else{
    tft.fillRect(0,80,width,height,ILI9341_BLACK);
  }
  if(titre_page == true){
    int taille = titre.length() * ( 6 * 4 ) ;  
    tft.setTextSize(4);
    tft.setCursor((width - taille - 30)/2 , 100);
    tft.setTextColor(ILI9341_WHITE);
    tft.println(titre);
    int epesseur = 5 ;
    int contener = (width - taille - 30)/2; 
    for (int i = 0; i < epesseur; i++){
        tft.drawTriangle(contener - 15 - 0.5*i, 90 + i , contener - 15 - 0.5*i, 140 -i,contener - 55 + i, 115,ILI9341_WHITE);
    }
    for (int i = 0; i < epesseur; i++){
    tft.drawTriangle(contener + taille + 0.5*i +10, 90 + i, contener  +10 + taille + 0.5*i, 140 -i,contener + 40 + taille - i +10, 115,ILI9341_WHITE);
    }
  }if(haut_page == true){
    tft.setTextSize(3);
    tft.setCursor((width - (text_haut.length() * 6.5 * 3))/2 , 10);
    tft.println(text_haut);
    tft.drawLine(0, 45, width, 45, ILI9341_WHITE);
  }
}
void barre_de_selection(int i, bool& stop){
  while (!stop) {
        delay(500);
        tft.fillRect(48 + 60 * i, 180, 50, 8, ILI9341_WHITE);
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
        tft.fillRect(48 + 60 * i, 180, 50, 8, ILI9341_BLACK);
    }
}
void selctor(int V, String uniter);
void selctor(int V, String uniter) {
  int desimale = V /10;
  int uniterNombre = V - desimale ;
  // écrire les chifre 
  // Faire la selection 
}
void setup() {
  Serial.begin(9600);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  servo.attach(servoPin, 500, 2400);
  // Initialiser l'écran LCD
  tft.begin();
  tft.setRotation(1);
  pinMode(btn_home, INPUT_PULLUP);
  // Ballasts
  pinMode(btn_ballast, INPUT_PULLUP);
  // commande manch
  pinMode(btn_plus, INPUT_PULLUP);
  pinMode(btn_moins, INPUT_PULLUP);
  pinMode(btn_L, INPUT_PULLUP);
  pinMode(btn_R, INPUT_PULLUP);
  pinMode(btn_ok, INPUT_PULLUP);
  page(true,"MODE", false, "", true);
}
void loop() {
  if((windo == 0 || windo == 1) && (digitalRead(btn_R) == LOW || digitalRead(btn_L) == LOW)){
    if(windo == 0){
      page(true,"PARAMETRES", false, "", false);
      windo = 1;
    }else{
      page(true,"MODE", false, "", false);
      windo = 0;
    } 
  }if(windo == 1 || windo == 2 || windo == 3 || windo == 6 || windo == 7){
    if(digitalRead(btn_ok) == HIGH && windo == 1){
        page(true,"BALLASTS", true, "PARAMETRES", false);
        windo = 2;
    }else if(digitalRead(btn_ok) == HIGH && windo == 2){
        page(false,"", true, "BALLASTS", true);
        selctor(V_ballast,"ml");
        windo = 6;
    }else if(digitalRead(btn_ok) == HIGH && windo == 3){
        page(false,"", true, "RESERVOIRS", true);
        // page des RESERVOIRS
        windo = 7;
    }else if((windo == 2 || windo == 3) && (digitalRead(btn_R) == LOW || digitalRead(btn_L) == LOW)){
      if(windo == 2){
        page(true,"RESERVOIRS", true, "", false);
        windo = 3;
      }else{
        page(true,"BALLASTS", true, "", false);
        windo = 2;}
    } 
  }
}