/****************************************************************************/
/* Copyright (c) 2014 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : Interface for the AJA Ki Pro Quad video recorder.             */
/* Filename : KiPro.h                                                       */
/* Author   : Henthorn                                                      */
/* Project  : i2MAP                                                         */
/* Version  : 1.0                                                           */
/* Created  : 11/23/2014                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#ifndef _KIPRO_H
#define _KIPRO_H

#include <curl.h>
#include "ourTypes.h";

class HtmlUtil;
class KiProLog;

// Simple HTML interface for use with the Ki Pro Quad recorder from AJA.
//
class KiPro
{
   friend KiProLog;

public:

   // Create an interface to the server. The connection is only opened
   // when posting and getting.
   //
   KiPro(const char* name, const char* server_ip);
   ~KiPro();

   Boolean ready();

   // Sync the clocks on the host and the Ki Pro Quad.
   // This call may block for up to 59 seconds, so it's best to
   // use this call once during initialization.
   //
   int time_sync();

   // Set the recorder to 'record' mode
   //
   int record_mode();

   // Set the recorder to 'data' mode
   //
   int data_mode();

   // Initialize connection to KiPro
   //
   void connect();

   // Return the timecode currently used by the recorder.
   //
   float get_timecode();

   // Set the timecode of the recorder to the given epoch seconds value.
   // If the value is zero, set the timecode to the current MVC time.
   // Returns the epoch seconds value used for the timecode.
   //
   unsigned long set_timecode(unsigned long epoch_seconds=0);

   // Set the record state on or off. Return the current state
   // of the recorder Transport Command (record, stop, play, ff, rw, etc.)
   // state param == 0 means stop, > 0 means record, < 0 means just return
   // the current recorder Transport Command value without changing it.
   //
   int record_button(int state=-1);

   int transport_state();

   // Set the clip name (recorded file name) to the given string.
   // A null string means to use the default value of the ISO 8601 UTC
   // (e.g., "2015-05-13T10:20:00Z"). Returns success (0) or failure.
   //
   int set_clip_name(const char* name=0);

   // Set the recorder reel number (1-999)
   // A reel value of 0 means to use the default (julian day)
   //
   int set_reel_num(unsigned int reel=0);

   // Return the percentage of available media left on the storage devices.
   // 0 - 100.
   int get_available_media();

   int mode() { return _mode; }

protected:

   /* Interface to the KiPro web server */
   CURL *_curl;
   static char   _response[];
   static int _response_size;
   static size_t read_response(void *buffer, size_t size, size_t nmemb, void *userp);
   int curl_setup();
   int curl_cleanup();
   int curl_execute();

   /* data */
   KiProLog *_log;
   Boolean _recording;
   float   _timecode;
   int     _available_media;
   int     _mode;

   char* _name;
   char* _server_ip;
   HtmlUtil *_server;

   char _buf[1025];
   char _errorbuf[CURL_ERROR_SIZE];
};

#endif
