/////////////////////////////////////////////////////////////////////////////
//  Modulename:     alelogfile.cpp
// ---------------------------------------------------------------------------
//
//  Project:        Silicon DVR
//
//  Subproject:     ALE logfile class
//
// ---------------------------------------------------------------------------
//  Description:    Class for generating and writing headers and clip
//                  information according to the AVID ALE file specification
//
// ---------------------------------------------------------------------------
//  Author:    Andreas Smolle   |  Created on : 2008-08-01
// ---------------------------------------------------------------------------
//
////////////////////////////////////////////////////////////////////////////////


#include "alelogfile.h"
#include "logger.h"
#include <iostream>
#include <Windows.h>
#include "globals.h"
#include "sicmdllensdata.h"

CALELogfile::CALELogfile( char * fileName )
{
	file = fopen( fileName, "at" );

	if ( ! file )
	{
		file = fopen( fileName, "wt" );
	}

	// Write a gLogger entry and bail out if we can't log
	if ( ! file )
	{
		gLogger.info( 1, "%d Creating ALE logfile failed!\n", GetTickCount() );
		return;
	}
}

void CALELogfile::WriteLogfileHeading()
{
	fprintf( file, "Heading\n" );
}
void CALELogfile::WriteLogfileColumn()
{
	fprintf( file, "Column\n" );
}

void CALELogfile::WriteLogfileData()
{
	fprintf( file, "Data\n" );
}

void CALELogfile::WriteGlobalHeading( char * heading, char * value  )
{
	fprintf( file, "%s\t%s\n", heading, value );
}

void CALELogfile::WriteColumnHeading()
{
	fprintf( file, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", 
				   "Name", 
				   "Shoot Date",
				   "Tape",
				   "Start", 
				   "End",
				   "Audio",
				   "Camroll",
				   "FPS",
				   "Scene",
				   "Shot",
				   "Take", 
				   "Camera",
				   "Ink Number",
				   "Ink End",
				   "Tracks",
				   "Filename",
				   "DPX",
				   "LUT",
				   "File Format",
				   "White Balance",
				   "FrameWidth",
				   "FrameHeight",
				   "Description",
				   "Comments",
				   "Director", 
				   "Producer", 
				   "Dir Photography",
				   "Production",
				   "Location",
				   "Script Pages\n" );
}

void CALELogfile::WriteColumn( char * name,
							   char * shootdate,
							   char * tape, 
							   char * start, 
							   char * end, 
							   char * audio,
							   char * camroll,
							   char * fps, 
							   char * scene,
							   char * shot, 
							   char * take,
							   char * camera,
							   char * inkNumber,
							   char * inkEnd,
							   char * tracks,
							   char * filename,
							   char * dpx,
							   char * lut,
							   char * fileFormat,
							   char * whiteBalance,
							   char * frameWidth,
							   char * frameHeight,
							   char * description,
							   char * comments,
							   char * director,
							   char * producer,
							   char * dirPhotography,
							   char * production,
							   char * location,
							   char * scriptPages )
{
	fprintf( file, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", 
				    name,
					shootdate,
					tape, 
					start, 
					end, 
					audio,
					camroll,
					fps, 
					scene,
					shot, 
					take,
					camera,
					inkNumber,
					inkEnd,
					tracks,
					filename,
					dpx,
					lut,
					fileFormat,
					whiteBalance,
					frameWidth,
					frameHeight,
					description,
					comments,
					director,
					producer,
					dirPhotography,
					production,
				    location,
					scriptPages  );

#ifdef LENS_DEVICE
	// If there is a lens device attached, write out CSV separated ALE entries
	if ( Globals.GetLensDevice() )
	{

		
		int res = SI_WriteDLFrameBlock();
		gLogger.info( 1, "SI_WriteDLFrameBlock FINAL returns: %d\n", res );

		Sleep( 150 );

		char dlData[ 2048 ] = "\0";

		sprintf( dlData, "%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s\n", 
				    name,
					shootdate,
					tape, 
					start, 
					end, 
					audio,
					camroll,
					fps, 
					scene,
					shot, 
					take,
					camera,
					inkNumber,
					inkEnd,
					tracks,
					filename,
					dpx,
					lut,
					fileFormat,
					whiteBalance,
					frameWidth,
					frameHeight,
					description,
					comments,
					director,
					producer,
					dirPhotography,
					production,
				    location,
					scriptPages );

		int len = strlen( dlData );
		
		res = SI_WriteDLHeader( dlData, len );

		gLogger.info( 1, "%d SI_WriteDLHeader returns: %d len: %d\n", GetTickCount(), res, len );

		// Close DL file
		res = SI_CloseDLFile();
		gLogger.info(1, "%d   SI_CloseDLFile\n",GetTickCount() );
	}
#endif#

}

void CALELogfile::WriteData( char * tagname, char * value )
{
	fprintf( file, "%s\t%s\n", tagname, value );
}

void CALELogfile::WriteNewline()
{
	fprintf( file, "\n" );
}

bool CALELogfile::Close()
{
	fclose( file );

	return true;
}

