#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Vk/VkInfoDialog.h>
#include "ToolsledPage.h"
#include "TiburonApp.h"
#include "ProtoBenthicGui.h"
#include "MidwaterGui.h"
#include "BenthicBioGui.h"
#include "GeologyGui.h"
#include "DrillSledGui.h"

ToolsledPage::ToolsledPage(const char *name, Widget parent) 
{
  _releaseControl = 0;
  
  _baseWidget = XtVaCreateWidget(name, xmFormWidgetClass, parent, 
				 NULL);
  
  installDestroyHandler();

  char errorBuf[errorMsgSize];
  
  VkComponent *sledGui;

  ToolsledComponent *toolsled = 
    ((TiburonApp *)theApplication)->vehicleConfig->toolsled;

  Boolean error = False;

  char fileName[256];
  TiburonApp *app = (TiburonApp *)theApplication;

  app->vehicleConfigFile(fileName);

  if (!toolsled)
  {
    error = True;
    sprintf(errorBuf, 
	    "Toolsled not specified in configuration file %s", fileName);
  }
  else
  {
    switch (toolsled->sledType())
    {
    case ToolsledComponent::ProtoBenthic:
      sledGui = new ProtoBenthicGui("protoBenthic", _baseWidget,
				    app->platformName());
      break;
    
    case ToolsledComponent::Midwater:
      sledGui = new MidwaterGui("midwater", _baseWidget,
				app->platformName());
      break;

    case ToolsledComponent::Benthic:
      sledGui = new BenthicBioGui("benthicBio", _baseWidget, 
				  app->platformName());
      break;

    case ToolsledComponent::Geology:
      sledGui = new GeologyGui("geology", _baseWidget, 
			       app->platformName());
      break;

    case ToolsledComponent::DrillSled:
      sledGui = new DrillSledGui("drillSled", _baseWidget, 
				 app->platformName());
      break;

    default:
      error = True;
      sprintf(errorBuf, "GUI for Toolsled type \"%s\" not yet implemented",
	      ToolsledComponent::typeMnem(toolsled->sledType()));
    }
  }
  
  if (!error)
  {
    XtVaSetValues(sledGui->baseWidget(),
		  XmNtopAttachment, XmATTACH_FORM,
		  XmNleftAttachment, XmATTACH_FORM,
		  XmNrightAttachment, XmATTACH_FORM,
		  NULL);
  
    sledGui->show();
  }
  else
  {
    XmString xmstr = XmStringCreateSimple(errorBuf);
      
    Widget notice = 
      XtVaCreateManagedWidget("label", xmLabelWidgetClass, _baseWidget,
			      XmNlabelString, xmstr, 
			      XmNleftAttachment, XmATTACH_FORM,
			      XmNtopAttachment, XmATTACH_FORM,
			      NULL);
      
    XmStringFree(xmstr);

    theInfoDialog->postAndWait(errorBuf);
  }

  return;  
}


ToolsledPage::~ToolsledPage()
{
  delete _releaseControl;
}

