#include <Arduino.h>
#include <WebServer.h>

//#include "Chassis.h"
//#include "ServerWeb.h"

int Web_nouvelle_entree;
int type_requete;

struct chassis_emission_t chassis_emission_web;

#define FORM \
  "<!DOCTYPE html>\n" \
  "<html>\n" \
  "<head>\n" \
  "<meta charset=\"UTF-8\">\n" \
  "<title>A simple form</title>\n" \
  "</head>\n" \
  "<body>\n" \
  "<form action=\"/form\" method=\"post\">\n" \
  "<br>" \
  "<label for=\"X\">X : en mm</label>\n" \
  "<br>" \
  "<input type=\"text\" id=\"X\" name=\"X\">\n" \
  "<br>" \
  "<label for=\"Y\">Y : en mm</label>\n" \
  "<br>" \
  "<input type=\"text\" id=\"Y\" name=\"Y\">\n" \
  "<br>" \
  "<label for=\"R\">Rotation : en degres</label>\n" \
  "<br>" \
  "<input type=\"text\" id=\"R\" name=\"R\">\n" \
  "<br>" \
  "<label for=\"V\">Vitesse : </label>\n" \
  "<br>" \
  "<input type=\"text\" id=\"V\" name=\"V\" value=\"2000\">\n" \
  "<br>" \
  "<br>" \
  "<label for=\"A\">Accel   : </label>\n" \
  "<br>" \
  "<input type=\"text\" id=\"A\" name=\"A\"value=\"1500\">\n" \
  "<br>" \
  "<br>" \
  "<input type=\"submit\" value=\" Vas-Y \">\n" \
  "<br>" \
  "<br>" \
  "<input type=\"reset\" value=\"Reset\">\n" \
  "<br>" \
  "<br>" \
  "<input type=\"submit\" value=\" STOP \">\n" \
  "</form>\n" \
  "</body>\n" \
  "</html>\n"

WebServer server(80);

void Web_init(){
  server.begin();
  server.on("/form", handleForm);
  server.on("/statu", showStatus);
}

bool Web_nouveau_message(){
  if(Web_nouvelle_entree){
    Web_nouvelle_entree = 0;
    return 1;
  }
  return 0;
}

int Web_type_requete(){
  return type_requete;
}

void Web_gestion(){
  server.handleClient();
}

struct chassis_emission_t Web_get_donnees(){
  return chassis_emission_web;
}

void handleForm() {
  String message;
  type_requete = WEB_DEPLACEMENT_RELATIF;
  message += FORM;
  server.send(200, "text/html", message);
  String myString0 = server.arg("X"); //positon de cmd en X mm
  // x= myString0.toInt() * coef_mvt/10;
  chassis_emission_web.translation_x_mm = myString0.toInt();
  String myString1 = server.arg("Y"); //positon de cmd en Y mm
  // y= myString1.toInt() * coef_mvt/10;
  chassis_emission_web.translation_y_mm = myString1.toInt();
  String myString2 = server.arg("R"); //positon de cmd en Rotation Deg °
  chassis_emission_web.rotation_z_rad = myString2.toInt() / 180 * M_PI;
  String myString3 = server.arg("V"); // Vitesse de cmd en 
  chassis_emission_web.vitesse = myString3.toInt();
  String myString4 = server.arg("A"); // Acceleration de cmd
  chassis_emission_web.acceleration = myString4.toInt();
  Web_nouvelle_entree=1;
}

void showStatus() {
  char chassis[50], gradin[50], triangulation[50], detection_adversaire[50];
  char message_statu[500];
  const char message_statu_tmplt[] = "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>A simple form</title></head>\
  <body>\n<h1>Statu du robot</h1>\n\
  <p>Chassis: %s</p><p>Gradin: %d</p><p>Triangulation: %d</p><p>Detection_adversaire: %d</p>";
  struct chassis_reception_t chassis_reception;

  statu_to_string(chassis, !Scan_chassis(&chassis_reception) );

  sprintf(message_statu, message_statu_tmplt, chassis, 0, 0, 0);
  server.send(200, "text/html", message_statu);
  
}

/// @brief convertir un statu boolean en message web.
/// @param statu 0 NOK, 1 OK
void statu_to_string(char * statu_str, bool statu){
  if(!statu){
    strcpy(statu_str, "<span style=\"color:red;\">NOK</span>");
  }else{
    strcpy(statu_str, "<span style=\"color:green;\">OK</span>");
  }
}
