/****************************************************************************/
/* Copyright (c) 2014 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : Menu-driven deck test program for the Cathx system            */
/* Filename : _cathxTest.cc                                                 */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 09/14/2015                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
#include "CathxIF.h"
#include "TimeIF.h"
#include "Syslog.h"
#include "System.h"

enum CARL_CMDS {
  EXIT = 0,
  START,
  STOP,
  PROFILE
};


struct cmd_s
{
  char *name;
  char code;
};

static struct cmd_s cmds[] = {
  {"Exit", EXIT},             {"Start Capture", START},           \
  {"Stop Capture", STOP},     {"Set a Profile", PROFILE}
};

void banner();
int menu_choice();
int start_Cathx();

int main( int argc, char **argv )
{

   // First, let's kick-off the Cathx server and driver
   // KiPro and Modtronix drivers
   //
   int pid_c;
   if ( (0 > ( pid_c = start_Cathx() )) )
      return(pid_c);
   else
      printf("cathxServer: %d\n", pid_c);



   // Connect to the server
   //
   CathxIF *cathx;
   int error = 0;

   try {
      cathx = new CathxIF("CathxServer");
   }
   catch (Exception e) {
      fprintf(stderr, "Caught exception: %s\n", e.msg);
      error = 1;
   }
   catch (...) {
      fprintf(stderr, "Caught some exception...\n");
      error = 1;
   }

   if (error)
    return error;

   printf("Cathx Server initializing. Please hold...\n");
   sleep(2);

   banner();

   // Run the menu-loop until exit requested
   //
   int keep_going = 1;
   while (keep_going)
   {
      int ans, cmd = menu_choice();
      char response[20];
      CathxIF::Actions actions;
      actions.imageCapture = -1;
      actions.profileNumber = -1;
      switch (cmd)
      {
         case EXIT:
            printf("\nUse Ctrl-C to exit\n\n");
            kill(pid_c, SIGKILL);
            keep_going = 0;
            break;

         case START:
            printf("| Start image capture...   \n");
            actions.imageCapture = 1;
            cathx->actions(&actions);
            break;

         case STOP:
            printf("| Stop image capture...   \n");
            actions.imageCapture = 0;
            cathx->actions(&actions);
            break;

         case PROFILE:
            printf("| Set Camera profile...   \n");
            printf("|\n| Enter a profile number (from 0 to 100) > " );
            gets(response);
            printf("%s\n", response);
            actions.profileNumber = atoi(response);
            cathx->actions(&actions);
            break;

         default:
            printf("| Unrecognized command!    \n|\n");
            break;
      }
   }

   return 0;

   kill(0, SIGTERM);
   system("slay cathxServer");
   system("slay cathxm12");

   return 0;
}

void banner()
{
  printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  printf("|             Cathx Test App\n");
  printf("|\n");
}

int menu_choice()
{
  printf("| Choose from the following: \n");

  for (int jj = 0; jj < sizeof(cmds)/sizeof(struct cmd_s); jj++)
  {
    printf("|  %2d) %s\n", jj+1, cmds[jj].name);
  }
  printf("|\n| Your menu choice > " );

  char response[32];
  char *c = gets(response);

  // Got a response?
  //
  if (strlen(response) > 0)
  {
    int choice = atoi(response);

    // Valid response? Return the code
    // 
    if (choice <= sizeof(cmds)/sizeof(struct cmd_s) && choice > 0)
      return cmds[choice-1].code;
  }

  return -1;
}

int start_Cathx()
{
   printf("Starting CathxServer...\n");

   char *program = "cathxServer";

   char errorBuf[256];
   char buf[256];
 
   pid_t pid;
 
   switch ((pid = fork())) {
     
   case 0:
      // In child
      // Execute driver program
      execlp(program, program, (char *)0);

      sprintf(errorBuf, 
         "Task::spawnServer() - exec() of \"%s\" failed", program);

      perror(errorBuf);
 
      break;

   case -1:
      perror("Task::spawnServer() - fork() failed");
      return -1;
   }

   return pid;
}

