#include <Xm/Label.h>
#include <Xm/RowColumn.h>
#include <Xm/Form.h>
#include "AlarmPanel.h"
#include "TiburonApp.h"

AlarmPanel::AlarmPanel(const char *name, Widget parent)
{
  _alarmLight = 0;
  
  for (int p = 0; p < NPriorityLevels; p++)  
    _counts[p] = 0;
  
  _baseWidget = XtVaCreateWidget(name, xmFormWidgetClass, parent, NULL);
  installDestroyHandler();
  
  _alarmLight = new AlarmLight("alarmLight", _baseWidget);

  XtVaSetValues(_alarmLight->baseWidget(),
		XmNleftAttachment, XmATTACH_FORM,
		XmNrightAttachment, XmATTACH_FORM,
		XmNtopAttachment, XmATTACH_FORM,
		NULL);
  
  _alarmLight->show();

  Widget label = 
    XtVaCreateManagedWidget("nActive", xmLabelWidgetClass, _baseWidget, 
			    XmNleftAttachment, XmATTACH_FORM,
			    XmNtopAttachment, XmATTACH_WIDGET,
			    XmNtopWidget, _alarmLight->baseWidget(),
			    NULL);

  Widget countMgr = 
    XtVaCreateWidget("countMgr", xmRowColumnWidgetClass, _baseWidget, 
		     XmNrightAttachment, XmATTACH_FORM,
		     XmNtopAttachment, XmATTACH_WIDGET,
		     XmNtopWidget, _alarmLight->baseWidget(),
		     XmNorientation, XmHORIZONTAL, 
		     XmNpacking, XmPACK_COLUMN,
		     NULL);


  
  Pixel foreground, background;
  TiburonApp *app = (TiburonApp *)theApplication;
  char wName[100];
  
  for (int p = 0; p < NPriorityLevels; p++)
  {
    app->priorityColors((Priority )(p + 1), &foreground, &background);

    sprintf(wName, "counts%d", p + 1);
    
    _counts[p] = new DynamicLabel(name, countMgr);

    _counts[p]->setColors(foreground, background);

    XtSetMappedWhenManaged(_counts[p]->baseWidget(), False);

    _counts[p]->show();
  }
  
  XtManageChild(countMgr);

  for (p = 0; p < NPriorityLevels; p++)
    setCount((Priority )(p + 1), 0);
  
}


AlarmPanel::~AlarmPanel()
{
  delete _alarmLight;

  for (int p = 0; p < NPriorityLevels; p++)  
    delete _counts[p];
}


void AlarmPanel::flash()
{
  _alarmLight->flash();
}


void AlarmPanel::shine()
{
  _alarmLight->shine();
}


void AlarmPanel::darken()
{
  _alarmLight->darken();
}


void AlarmPanel::setCount(Priority priority, int count)
{
  int p = priority - 1;
  if (p < 0 && p >= NPriorityLevels)
  {
    return;
  }
  
  if (count != 0)
  {
    char buf[100];
    sprintf(buf, "%d", count);
  
    _counts[p]->setText(buf); 

    if (XtIsRealized(_counts[p]->baseWidget()))
      XtMapWidget(_counts[p]->baseWidget());
  }
  else
  {
    // Zero counts, so hide label
    if (XtIsRealized(_counts[p]->baseWidget()))
      XtUnmapWidget(_counts[p]->baseWidget());
  }
}



void AlarmPanel::setLightLabel(char *text)
{
  _alarmLight->setLabel(text);
}


Priority AlarmPanel::lightPriority()
{
  return _alarmLight->priority();
}


void AlarmPanel::setLightPriority(Priority priority)
{
  _alarmLight->setPriority(priority);
}



void AlarmPanel::setButtonCallback(XtCallbackProc proc, XtPointer clientData)
{
  _alarmLight->setButtonCallback(proc, clientData);
}

