#include "FlashingAlarmText.h"
#include "TiburonApp.h"

#define dprintf if (debug) fprintf


FlashingAlarmText::FlashingAlarmText(const char *name, Widget parent, 
				     Alarm *alarm, char *format)
  : AlarmText(name, parent, alarm, format)
{
  _flashTimer = 0;
  _flashPeriod = 500;

  updateGui();
}


FlashingAlarmText::~FlashingAlarmText()
{
}


void FlashingAlarmText::updateGui(DM_Item handle)
{
  if (!_alarm)
    return;
  
  // Remove flash timer 
  if (_flashTimer)
  {
    XtRemoveTimeOut(_flashTimer);
    _flashTimer = 0;
  }
  
  AlarmText::updateGui(handle);
  
  if (_priority != NoPriority)
  {
    // Install flash timer
      _flashTimer = XtAppAddTimeOut(theApplication->appContext(), _flashPeriod,
				    &FlashingAlarmText::timerCallback, 
				    (XtPointer )this);
  }
}


void FlashingAlarmText::timerCallback(XtPointer clientData, XtIntervalId *id)
{
  Boolean debug = False;
  
  FlashingAlarmText *obj = (FlashingAlarmText *)clientData; 
  obj->_flashTimer = XtAppAddTimeOut(theApplication->appContext(), 
				     obj->_flashPeriod, 
				     &FlashingAlarmText::timerCallback, 
				     (XtPointer )obj);

  Pixel foreground, background;
  
  if (obj->_phase)
  {
    dprintf(stderr, "color text...\n");
    
    XtVaSetValues(obj->_valueText,
		  XmNforeground, obj->_foreground,
		  XmNbackground, obj->_background,
		  NULL);

    obj->_phase = 0;
  }
  else
  {
    dprintf(stderr, "darken text...\n");
    
    Pixel foreground, background;
    TiburonApp *app = (TiburonApp *)theApplication;
    app->priorityColors(NoPriority, &foreground, &background);
    
    XtVaSetValues(obj->_valueText,
		  XmNforeground, foreground, 
		  XmNbackground, background, 
		  NULL);

    obj->_phase = 1;
  }
}
