#ifndef Sim808Manager_define

#define Sim808Manager_define 1
#include "Utiles.h"
#define LEN_BUFFER_DATA_SIMMANAGER 1024

enum ProtocolSimManager {
    SIM808_CLOSED = 0,
    SIM808_TCP = 1,
    SIM808_UDP = 2,
};

class DataGPSSimManager
{
  public:
    uint16_t year;
    uint8_t month;
    uint8_t day;
    uint8_t hour;
    uint8_t minute;
    uint8_t second;
    uint8_t centisecond;
    float lat;
    float lon;
    float speed_kmh;
    float heading;
    float altitude;
    uint8_t nSatelites;
    bool IsValid;
    //precision------------
    float pdop;//precision posicion  (position Dilution of precision)
    float hdop;//precision horizontal
    float vdop;//precision vertical
    DataGPSSimManager();

    char* getPrecision(char*buf);
    char* getFecha(char*buf);
    char* getTimeStamp(char*buf);
    void initValues();
};

class Sim808Manager
{
    char buffer[LEN_BUFFER_DATA_SIMMANAGER];
    bool simIniciada;
    bool gpsIniciada;
    int pk;
    DataGPSSimManager dataGPS;
    HardwareSerial *pSeria;
    int AjusteHora;
  public:
    Sim808Manager();

    bool inicia(HardwareSerial *mySerial, int powerKey = -1, int baud=0);

    bool EnviaSMS(char* tlf, char *sms);

    DataGPSSimManager* GetGPSValid();

    char* GetFecha(char *buf);
    DataGPSSimManager* GetUltGPS();

    DataGPSSimManager* GetGPS();
    
    //envio de datos
   bool IniciaNetwork(const char* apn = 0, const char* userName = 0, const char* passWord = 0);
   bool  connect(ProtocolSimManager ptl, const char* host, int port);
   bool send(char* str, int len);
   char* recibeDatos();
   void Desconecta();
   bool Cierra();
    //funciones auxiliares
   char* EnviaComando(char* at_command);
   bool httpGet(char* uri);
  private:
    bool checkeaConfig(int baud);
    bool iniciaGPS();
    bool iniciaSIM();

    char* recibe();
    void envia(char* dat);
    void enviaSolo(char* dat, int  ndata);
    bool CheckAt();
    void enciende();
    bool reciveOK();
    //traza nmea gprmc
    bool recibeGPRMC();
    bool recibeCGPSINF_0();
    bool recibeGPGSA();
    bool checkea();
    bool configBaud(int baud);

    uint32_t str_to_ip(const char* str);

    bool ISConectado();
};
#endif
