#include <Xm/Form.h>
#include <Xm/RowColumn.h>
#include <Xm/Frame.h>
#include <Xm/Separator.h>
#include <Xm/PushB.h>
#include <Vk/VkErrorDialog.h>
#include <Vk/VkQuestionDialog.h>
#include "ViewingPage.h"
#include "ViewingMap.h"
#include "MainCamera.h"
#include "LightSwitch.h"
#include "TiburonApp.h"
#include "camera.h"
#include "tiburon.h"
#include "DmErrno.h"

#define dprintf if (debug) fprintf

#define CoincidentPortItemName "coincidentPortMaster"
#define CoincidentStbdItemName "coincidentStbdMaster"
#define PanoramicItemName "panoramic"
#define IndependentItemName "independent"

ViewingPage::ViewingPage(const char *name, Widget parent)
  : VkComponent(name)
{
  _baseWidget = XtVaCreateWidget(_name, xmFormWidgetClass, parent, NULL);
  installDestroyHandler();

  const int lightSwitchWidth = 120;
  const int lightSwitchHeight = 50;
  
  LightSwitch *portFixedLight =
    new LightSwitch("portFixedLight", _baseWidget,
		    "TIBURON.POWER.LIGHTING_DCON_48V",
		    "TIBURON.POWER.LIGHTING_DCON_240V",
		    "TIBURON.POWER.LIGHTS.BALLAST2",
		    "TIBURON.POWER.LIGHTS.LIGHT4",
		    "Port Fixed Light",
		    lightSwitchWidth, lightSwitchHeight,
		    500);


  XtVaSetValues(portFixedLight->baseWidget(),
		XmNleftAttachment, XmATTACH_FORM,
		XmNtopAttachment, XmATTACH_FORM,
		NULL);
  
  portFixedLight->show();

  LightSwitch *portDirLight =
    new LightSwitch("portDirLight", _baseWidget,
		    "TIBURON.POWER.LIGHTING_DCON_48V",
		    "TIBURON.POWER.LIGHTING_DCON_240V",
		    "TIBURON.POWER.LIGHTS.BALLAST2",
		    "TIBURON.POWER.LIGHTS.LIGHT5",
		    "Port Dir Light",
		    lightSwitchWidth, lightSwitchHeight,
		    500);


  XtVaSetValues(portDirLight->baseWidget(),
		XmNleftAttachment, XmATTACH_FORM,
		XmNtopAttachment, XmATTACH_WIDGET,
		XmNtopWidget, portFixedLight->baseWidget(),
		NULL);
  
  portDirLight->show();


  LightSwitch *portAuxLight =
    new LightSwitch("portAuxLight", _baseWidget,
		    "TIBURON.POWER.LIGHTING_DCON_48V",
		    "TIBURON.POWER.LIGHTING_DCON_240V",
		    "TIBURON.POWER.LIGHTS.BALLAST0",
		    "TIBURON.POWER.LIGHTS.LIGHT1",
		    "Port Aux Light",
		    lightSwitchWidth, lightSwitchHeight,
		    500);


  XtVaSetValues(portAuxLight->baseWidget(),
		XmNleftAttachment, XmATTACH_FORM,
		XmNtopAttachment, XmATTACH_WIDGET,
		XmNtopWidget, portDirLight->baseWidget(),
		NULL);
  
  portAuxLight->show();


  LightSwitch *stbdFixedLight =
    new LightSwitch("stbdFixedLight", _baseWidget,
		    "TIBURON.POWER.LIGHTING_DCON_48V",
		    "TIBURON.POWER.LIGHTING_DCON_240V",
		    "TIBURON.POWER.LIGHTS.BALLAST1",
		    "TIBURON.POWER.LIGHTS.LIGHT3",
		    "Stbd Fixed Light",
		    lightSwitchWidth, lightSwitchHeight,
		    500);


  XtVaSetValues(stbdFixedLight->baseWidget(),
		XmNrightAttachment, XmATTACH_FORM,
		XmNtopAttachment, XmATTACH_FORM,
		NULL);
  
  stbdFixedLight->show();

  LightSwitch *stbdDirLight =
    new LightSwitch("stbdDirLight", _baseWidget,
		    "TIBURON.POWER.LIGHTING_DCON_48V",
		    "TIBURON.POWER.LIGHTING_DCON_240V",
		    "TIBURON.POWER.LIGHTS.BALLAST1",
		    "TIBURON.POWER.LIGHTS.LIGHT2",
		    "Stbd Dir Light",
		    lightSwitchWidth, lightSwitchHeight,
		    500);

  XtVaSetValues(stbdDirLight->baseWidget(),
		XmNrightAttachment, XmATTACH_FORM,
		XmNtopAttachment, XmATTACH_WIDGET,
		XmNtopWidget, stbdFixedLight->baseWidget(),
		NULL);
  
  stbdDirLight->show();

  LightSwitch *stbdAuxLight =
    new LightSwitch("stbdAuxLight", _baseWidget,
		    "TIBURON.POWER.LIGHTING_DCON_48V",
		    "TIBURON.POWER.LIGHTING_DCON_240V",
		    "TIBURON.POWER.LIGHTS.BALLAST0",
		    "TIBURON.POWER.LIGHTS.LIGHT0",
		    "Stbd Aux Light",
		    lightSwitchWidth, lightSwitchHeight,
		    500);

  XtVaSetValues(stbdAuxLight->baseWidget(),
		XmNrightAttachment, XmATTACH_FORM,
		XmNtopAttachment, XmATTACH_WIDGET,
		XmNtopWidget, stbdDirLight->baseWidget(),
		NULL);
  
  stbdAuxLight->show();

  ViewingMap *viewMap = new ViewingMap("viewMap", _baseWidget);
  XtVaSetValues(viewMap->baseWidget(),
		XmNtopAttachment, XmATTACH_FORM,
		XmNleftAttachment, XmATTACH_WIDGET,
		XmNleftWidget, portFixedLight->baseWidget(),
		XmNrightAttachment, XmATTACH_WIDGET,
		XmNrightWidget, stbdFixedLight->baseWidget(),
		NULL);
  
  viewMap->show();
  
  Widget brightnessButton =
    XtVaCreateManagedWidget("brightness", xmPushButtonWidgetClass,
			    _baseWidget,
			    XmNrightAttachment, XmATTACH_POSITION,
			    XmNrightPosition, 50,
			    XmNtopAttachment, XmATTACH_WIDGET,
			    XmNtopWidget, viewMap->baseWidget(),
			    NULL);
  
  XtAddCallback(brightnessButton, XmNactivateCallback,
		&ViewingPage::brightButtonCallback, (XtPointer )this);
  
  Widget videoSetupButton = 
    XtVaCreateManagedWidget("videoSetup", xmPushButtonWidgetClass,
			    _baseWidget,
			    XmNleftAttachment, XmATTACH_WIDGET,
			    XmNleftWidget, brightnessButton,
			    XmNtopAttachment, XmATTACH_WIDGET,
			    XmNtopWidget, viewMap->baseWidget(),
			    NULL);

  XtAddCallback(videoSetupButton, XmNactivateCallback,
		&ViewingPage::videoSetupButtonCallback, (XtPointer )this);

  Widget frame = 
    XtVaCreateWidget("mainCameraFrame", xmFrameWidgetClass, _baseWidget, 
		     XmNleftAttachment, XmATTACH_WIDGET,
		     XmNleftWidget, portFixedLight->baseWidget(),
		     XmNrightAttachment, XmATTACH_WIDGET,
		     XmNrightWidget, stbdFixedLight->baseWidget(),
		     XmNtopAttachment, XmATTACH_WIDGET,
		     XmNtopWidget, brightnessButton,
		     NULL);
  
  Widget mgr = XtVaCreateWidget("mgr", xmFormWidgetClass, frame,
				NULL);
  
  _monitor = new IviewMonitor();
  
  _portCamera = 
    new MainCamera("mainPortCamera", mgr, MainCamera::PanasonicWV,
		   PORT_CAMERA_DM_PREFIX, 
		   IviewPortCamera, 100, _monitor);

  if (_portCamera->error())
  {
    _portCamera->prtErrorMsg();
    ((TiburonApp *)theApplication)->exit(1);
  }
  
  XtVaSetValues(_portCamera->baseWidget(),
		XmNleftAttachment, XmATTACH_FORM,
		XmNtopAttachment, XmATTACH_FORM,
		XmNbottomAttachment, XmATTACH_FORM,
		NULL);
  
  _portCamera->show();

  Widget separator = 
    XtVaCreateManagedWidget("separator", xmSeparatorWidgetClass, mgr,
			    XmNorientation, XmVERTICAL,
			    XmNtopAttachment, XmATTACH_FORM,
			    XmNbottomAttachment, XmATTACH_FORM,
			    XmNleftAttachment, XmATTACH_POSITION,
			    XmNleftPosition, 50,
			    NULL);
  
  _stbdCamera = 
    new MainCamera("mainStbdCamera", mgr, MainCamera::PanasonicWV,
		   STBD_CAMERA_DM_PREFIX, 
		   IviewStbdCamera, 100, _monitor);

  if (_stbdCamera->error())
  {
    _stbdCamera->prtErrorMsg();
    ((TiburonApp *)theApplication)->exit(1);
  }

  XtVaSetValues(_stbdCamera->baseWidget(),
		XmNrightAttachment, XmATTACH_FORM,
		XmNtopAttachment, XmATTACH_FORM,
		XmNbottomAttachment, XmATTACH_FORM,
		NULL);

  _stbdCamera->show();
  
  XtManageChild(mgr);
  
  XtManageChild(frame);


  _joystick1 = 
    new PanTiltControl("joystick1", _baseWidget, _monitor);
  
  if (_joystick1->error())
  {
    _joystick1->prtErrorMsg();
    ((TiburonApp *)theApplication)->exit(1);
  }
  
  XtVaSetValues(_joystick1->baseWidget(), 
		XmNleftAttachment, XmATTACH_FORM,
		XmNbottomAttachment, XmATTACH_POSITION,
		XmNbottomPosition, 100,
		NULL);

  _joystick1->show();


  _joystick2 = 
    new PanTiltControl("joystick2", _baseWidget, _monitor);
  
  if (_joystick2->error())
  {
    _joystick2->prtErrorMsg();
    ((TiburonApp *)theApplication)->exit(1);
  }

  XtVaSetValues(_joystick2->baseWidget(), 
		XmNrightAttachment, XmATTACH_FORM,
		XmNbottomAttachment, XmATTACH_POSITION,
		XmNbottomPosition, 100,
		NULL);

  _joystick2->show();

  Widget resetIview = 
    XtVaCreateManagedWidget("resetIview", xmPushButtonWidgetClass,
			    _baseWidget, 
			    XmNleftAttachment, XmATTACH_WIDGET,
			    XmNleftWidget, _joystick1->baseWidget(),
			    XmNrightAttachment, XmATTACH_WIDGET,
			    XmNrightWidget, _joystick2->baseWidget(),
			    XmNbottomAttachment, XmATTACH_POSITION,
			    XmNbottomPosition, 90,			    
			    NULL);
  
  XtAddCallback(resetIview, XmNactivateCallback,
		&ViewingPage::resetIviewCallback, (XtPointer )this);

  _lightDialog = new LightDialog("lightDialog");

  _videoSetupDialog = new VideoSetupDialog("videoSetup");
}


ViewingPage::~ViewingPage()
{
  delete _joystick1;
  delete _joystick2;
  delete _portCamera;
  delete _stbdCamera;
}


void ViewingPage::brightButtonCallback(Widget w, 
				       XtPointer clientData,
				       XtPointer callData)
{
  ViewingPage *obj = (ViewingPage *)clientData;
  obj->_lightDialog->setTitle("brightness");
  obj->_lightDialog->post(NULL, NULL, NULL, NULL,
			  (XtPointer )obj->_lightDialog);
  
}



void ViewingPage::videoSetupButtonCallback(Widget w, 
					   XtPointer clientData,
					   XtPointer callData)
{
  ViewingPage *obj = (ViewingPage *)clientData;
  
  obj->_videoSetupDialog->setTitle("videoSetupTitle");
  
  obj->_videoSetupDialog->post(NULL, NULL, NULL, 
			       NULL,
			       (XtPointer )obj->_videoSetupDialog);
  
}




void ViewingPage::resetIviewCallback(Widget w, 
				     XtPointer clientData,
				     XtPointer callData)
{
  theQuestionDialog->post("Restart Iview?",
			  &ViewingPage::resetIviewOkCallback,
			  clientData);
}


void ViewingPage::resetIviewOkCallback(Widget w, 
				       XtPointer clientData,
				       XtPointer callData)
{
  ViewingPage *obj = (ViewingPage *)clientData;

  char itemName[MaxDmNameLen];
  sprintf(itemName, "%s%s", TIBURON_DM_PREFIX, RESTART_IVIEW_DM);
  DmEmptyObject *dmObject = new DmEmptyObject(itemName);

  Errno err;
  char errorBuf[errorMsgSize];
  
  if ((err = dmObject->startProvide(DM_STATIC)) != SUCCESS)
  {
    sprintDmError(err, "ViewingPage::resetIviewCallback()", errorBuf);
    theErrorDialog->postAndWait(errorBuf);
  }

  DM_Time t;
  gettimeofday(&t, NULL);

  if ((err = dmObject->write(&t)) != SUCCESS)
  {
    sprintDmError(err, "ViewingPage::resetIviewCallback()", errorBuf);
    theErrorDialog->postAndWait(errorBuf);
  }

  delete dmObject;
}
