

#include <SPI.h>
#include <LiquidCrystal.h>
#include <PubSubClient.h>
#include "MqttDefines.h"
#include "MqttUtiles.h"
#include "MqttVarManager.h"

#include "LcdMQTTViewer.h"

void LcdMQTTViewer::envia(char* txt, int pos, int line)
{
    char buf[MAXTOPICVAR];
	
	int l = strlen(txt);
    if (l > nchar)
        l = nchar;
    

	
    for (int i = 0; i < nchar; i++)
        if (i < l)
            buf[i] = txt[i];
        else
            buf[i] = ' ';

    buf[nchar] = 0;
    lcd->setCursor(pos, line);
	lcd->print(buf);
}
LcdMQTTViewer::LcdMQTTViewer()
{
    panActual = 0;
    ivarBuzzer=-1;
    topic[0]=0;
	maxpan = 0;
	tiempo = 0;
	incre_t = 10 * 1000;
	for (int i = 0; i < MAXINFOLCD; i++)
	{
		LcdConf[i].ivar=-1;
		LcdConf[i].formatLCD[0] = 0;
		LcdConf[i].pan = 255;
	}
}
void LcdMQTTViewer::setTimer(int seg)
{
	incre_t = seg * 1000;
}

void LcdMQTTViewer::envia(int pan)
{
  char val[16];
	char buf[MAXTOPICVAR];
	for (int i = 0; i < maxtInfo; i++)
	{
		if (LcdConf[i].pan != pan)
			continue;
     if(LcdConf[i].ivar>=0)
     {
         MqttVar *v=vars->Get(LcdConf[i].ivar);
         if(v==NULL)
          continue;
         //pilla valor
         if(v->flags & MqttVar::FLOAT_VAR)
            dtostrf(v->val.f,3, 2, val);
         else
            sprintf(val, "%d", (int)v->val.i);
        sprintf(buf, LcdConf[i].formatLCD, val);
      }
      else
        strcpy( buf, LcdConf[i].formatLCD);

		envia(buf,0, LcdConf[i].line);
	}
}

void LcdMQTTViewer::inicia(MqttVarManager *v, LiquidCrystal* lcdEx, int digi, int lines,int ibuz)
{
  ivarBuzzer=(byte)ibuz;
  vars=v;
  lcd =lcdEx;
  nchar = digi;
  lcd->begin(nchar, lines);
  nlineslcd=lines;
  for(int i=0; i<lines; i++)
  {
    envia("Iniciando",0,i);
  }
}
void LcdMQTTViewer::loop()
{
  if (!MqttUtiles::pasa_incre(&tiempo, incre_t))
    return;
  #ifdef DEBUG_PS
     Serial.println("Refres lcd");
     #endif
  envia(panActual);
  panActual = (panActual + 1) % (maxpan+1);
 
}

void LcdMQTTViewer::add(byte ivar, char* formatLCD, int line, int pan)
{
	if (maxtInfo >= MAXINFOLCD)
		return;
	LcdConf[maxtInfo].ivar=ivar;
	strcpy(LcdConf[maxtInfo].formatLCD, formatLCD);
	LcdConf[maxtInfo].line = line;
	LcdConf[maxtInfo].pan = pan;
	maxtInfo++;
 //recalcula numero de pantallas
	if (maxpan < pan)
		maxpan = pan;
}

int LcdMQTTViewer::OnMqtt(char* top, char* payload)
{
  if(strcmp(top,topic))
    return -1;

  int i=0;
  int lin=0;
  int n=0;
  char buf[MAXTOPICVAR*2];
  char imelod[12];
  int nmelod=0;
  bool melodia=false;
  while(payload[i])
  {
    if(payload[i]=='\n')
    {
      i++;
      buf[n]=0;
      envia(buf,0,lin);
      lin++;
      n=0;
    }
    else if(payload[i]=='$')
    {
      if(melodia)
      {
        imelod[nmelod]=0;
        melodia=false;
        MQTTvalor v;
        v.i=atoi(imelod);
        
        if(ivarBuzzer>=0 && nmelod>0)
          vars->SetVal(ivarBuzzer,v);
      }
      else
      {
        nmelod=0;
        melodia=true;
      }
      i++;
    }
    else
    {
      if(melodia)
        imelod[nmelod++]=payload[i++];
      else
        buf[n++]=payload[i++];
    }
  }
  buf[n]=0;
  envia(buf,0,lin);
  lin++;
  for(; lin<nlineslcd; lin++)
  {
     envia("",0,lin);
  }
  
 tiempo=millis();
 return -1;
}
void LcdMQTTViewer::suscribe(PubSubClient *client_qqtt)
{
    if(!topic[0])
      return;
    client_qqtt->subscribe(topic);
}
