#include <vdr/receiver.h>

#include "radioInfoReceiver.h"  
#include "sRadioInfo.h"
#include "tools.h"  

#define SCAN_DELAY 5   // number of seconds between updates



cRadioInfoReceiver::cRadioInfoReceiver(int Pid, int Ca, sRadioInfo* Ri) : cReceiver(Ca, -1, Pid)
{
  DEBUG_MSG("RadioInfoReceiver Created.");
  radioInfo = Ri;
  lastScan = 0;
  iPid = Pid;
  attachedDevice = NULL;
}


void cRadioInfoReceiver::Attach(void)
{
if (!attachedDevice) 
  {
    attachedDevice = getDevice();
    if (attachedDevice) 
    {
      attachedDevice->AttachReceiver(this);
      DEBUG_MSG("RadioInfoReceiver Attached.");
    }
  }
}


void cRadioInfoReceiver::Detach(void)
{
  if (attachedDevice) 
  {
    attachedDevice->Detach(this);
    attachedDevice = NULL;
    DEBUG_MSG("RadioInfoReceiver Detached.");
  }
}


void cRadioInfoReceiver::Receive(uchar *Data, int Length) 
{
  if (time(NULL) - lastScan <= 5)  return;
  
  DEBUG_MSG("Updating Info");
  
  /*fprintf(stderr,">>> DATA length=%d\n", Length);
  for (int i=0; i<Length; i++)
    fprintf(stderr,"%0X ", Data[i]);
    
  fprintf(stderr, "\n");
  
    fprintf(stderr,">>> CHAR\n");
  for (int i=0; i<Length; i++)
    fprintf(stderr,"%c", Data[i]);
    
  fprintf(stderr, "\n");*/
  
  radioInfo->updating = true;
  
  if (Length < 13) return;
  
  int line=0; int p=0;
  int i=13; // Ignore 12 byte header
  
  while (i<Length && Data[i] != 0xFF)
  {
    char c = Data[i];
    if (c == 0x0A)  c = '\0';
    
    switch (line)
    {
      case 0:
        radioInfo->title[p] = c;
        break;
        
      case 1:
        radioInfo->artist[p] = c; 
        break;
         
      case 2:
        radioInfo->extra1[p] = c; 
        break;
         
      case 3:
        radioInfo->extra2[p] = c;
        break;
        
      case 4:
        radioInfo->extra3[p] = c;
        break;
    
    }
    
    p++;
    i++;
  
    if (c == '\0')
    {
      p = 0;
      line++;
    }
  }
  
  radioInfo->updating = false;
  
  lastScan = time(NULL); 
}


cDevice* cRadioInfoReceiver::getDevice(void)
{
  return cDevice::GetDevice(Channels.GetByNumber(cDevice::CurrentChannel()), 0);
}

