/****************************************************************************
 *   Copyright (C) 2004 by Arboretum Systems, Inc.                          *
 *   Technical administrator: Alain Mazy, hav@mazy.be                       *
 *   Lead program designer: Georges Jaroslaw, gjaro@arboretum.com           *
 *                                                                          *
 *   HyperEngine-AV is a video, audio and text editor that can be used to   *
 *   record, import, edit, process and arrange together video, sound and    *
 *   text. These are all achieved in the program's free-form document       *
 *   window, with no limitations to the number of tracks.                   *
 *   The purpose of this program is to provide to users at all levels of    *
 *   expertise a readily accessible tool enabling them to create movies,    *
 *   slide shows or presentations.                                          *
 *                                                                          *
 *   HyperEngine-AV itself is free software. However, plug-ins that can be  *
 *   used as add-ons to HyperEngine-AV may or may not be free software, at  *
 *   the discretion of each plug-in developer and as specified by these     *
 *   plug-ins' respective licenses.                                         *
 *   You can redistribute HyperEngine-AV itself and/or modify it under the  *
 *   terms of the GNU General Public License as published by the Free       *
 *   Software Foundation; either version 2 of the License, or (at your      *
 *   option) any later version.                                             *
 *                                                                          *
 *   HyperEngine-AV is distributed in the hope that it will be useful, but  *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of             *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *   GNU General Public License for all applicable details.                 *
 *                                                                          *
 *   You should have received a copy of the GNU General Public License      *
 *   along with this program; if not, write to:                             *
 *   Free Software Foundation, Inc.,                                        *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
 ****************************************************************************/

#ifndef TArmeTextFileHandler_h
#include "TArmeTextFileHandler.h"
#endif //TArmeTextFileHandler_h

#ifndef TArmeDoc_h
#include "TArmeDoc.h"
#endif //TArmeDoc_h

#ifndef TArmeMediaEngine_h
#include "TArmeMediaEngine.h"
#endif //TArmeMediaEngine_h

#ifndef TArmeDoc_h
#include "TArmeDoc.h"
#endif //TArmeDoc_h

#ifndef ZzHelpers_h
#include "ZzHelpers.h"
#endif //ZzHelpers_h

#ifndef ZzHeapWalker_h
#include "ZzHeapWalker.h"
#endif ZzHeapWalker_h

#ifndef MIProgressBarDlgManager_h
#include "MIProgressBarDlgManager.h"
#endif //MIProgressBarDlgManager_h

#ifndef TArmeTextClip_h
#include "TArmeTextClip.h"
#endif //TArmeTextClip_h

#ifndef Zz_PathHelpers_h
#include "Zz_PathHelpers.h"
#endif //Zz_PathHelpers_h

bool TArmeTextFileHandler::CanHandleTextFileByLookingAtExtension( const ZzString* zzPath )
{
	char ext[10];
	
	ZzGetUpperCaseFileExtensionFromPath(ext, 10, zzPath->getUTF8String());
	if (!strcmp(ext, "TXT"))
		return true;
	else 
		return false;
}

bool TArmeTextFileHandler::CanHandleTextFileByLookingAtType( const ZzString* zzPath )
{
	OSType typeCode;
	OSType creatorCode;
	OSStatus st;
	
	st = ZzGetCreatorTypeFromPath( *zzPath, &creatorCode, &typeCode );
	if(st != noErr)
		return false;
	if ( ( typeCode == 'TEXT' ) || ( typeCode == 'TXT ' ) ) 
		return true;
	return false;
}

bool TArmeTextFileHandler::CanHandleRTFFileByLookingAtExtension( const ZzString* zzPath )
{
	char ext[10];
	
	ZzGetUpperCaseFileExtensionFromPath(ext, 10, zzPath->getUTF8String());
	if (!strcmp(ext, "RTF"))
		return true;
	else 
		return false;
}

bool TArmeTextFileHandler::CanHandleRTFFileByLookingAtType( const ZzString* zzPath )
{
	OSType typeCode;
	OSType creatorCode;
	OSStatus st;
	
	st = ZzGetCreatorTypeFromPath( *zzPath, &creatorCode, &typeCode );
	if(st != noErr)
		return false;
	if ( ( typeCode == 'RTF ' ) ) 
		return true;
	return false;
}

bool TArmeTextFileHandler::CanHandleFileByLookingAtExtension( const ZzString* zzPath )
{
	if ( this->CanHandleTextFileByLookingAtExtension(zzPath) || this->CanHandleRTFFileByLookingAtExtension(zzPath) ) return true;
	return false;
}

bool TArmeTextFileHandler::CanHandleFileByLookingAtType( const ZzString* zzPath )
{
	if ( this->CanHandleTextFileByLookingAtType(zzPath) || this->CanHandleRTFFileByLookingAtType(zzPath) ) return true;
	return false;
}

bool TArmeTextFileHandler::CanHandleTextFile( const ZzString* zzPath )
{
	if ( this->CanHandleTextFileByLookingAtExtension(zzPath) || this->CanHandleTextFileByLookingAtExtension(zzPath) ) return true;
	return false;
}

bool TArmeTextFileHandler::CanHandleRTFFile( const ZzString* zzPath )
{
	if ( this->CanHandleRTFFileByLookingAtExtension(zzPath) || this->CanHandleRTFFileByLookingAtExtension(zzPath) ) return true;
	return false;
}


ArmeErr TArmeTextFileHandler::CreateWSObjsFromFile( 
	TArmeDoc* 					pDoc, 
	ZzArchPtrArray*				theObjects,
	const ZzString*				zzPath,
	ArmeDataFileRef*			fileRef,
	const TArmeWSObjPos&		pos, 
	MIProgressBarDlgManager*	progressMngr)
{
	ArmeErr err;
	TArmeWSObjPos newArmePos(pos);
	#pragma unused (temp,temp)
//	char temp[1024];
	TArmeTextClip* newClip = NULL;

	progressMngr->SetNbSubTasks(1); 

	//all dropped files should start on video frames boundaries
	// if the movie contains one video track, round the drop position to the nearest video frame
	pDoc->RoundArmeTimeToNearestVideoFrame(newArmePos.fStartTime);

	if (this->CanHandleRTFFile(zzPath))
		{
		// get the text in the rtf (with style attributes)
		NSAttributedString *	theText = [[NSAttributedString alloc] initWithPath:zzPath->getNSString() documentAttributes:NULL];
		if (theText == nil)
			err.type = kArmeErrMediaHandlerCannotImport;
		else
			if ([theText length] == 0)
				err.type = kArmeErrMediaHandlerCannotImport;
			else
				{
				progressMngr->EnterSubTask();

				ZzString zzFileName = ZzGetFileNameFromPath(*zzPath);

				// call the TVideoClip constructor (the one you have build for rtf)
				newClip = new TArmeTextClip(theText, zzFileName.getUTF8String() , pDoc, newArmePos);
	
				theObjects->AddItem((ArmeArchivable*)newClip); // add the object to the dest array
				progressMngr->LeaveSubTask();	
				}
		
		}
	else if (this->CanHandleTextFile(zzPath))
		{
		// get the text in the txt file
		NSString *	theText = [[NSString alloc] initWithContentsOfFile:zzPath->getNSString()];
		if (theText == nil)
			err.type = kArmeErrMediaHandlerCannotImport;
		else
			if ([theText length] == 0)
				err.type = kArmeErrMediaHandlerCannotImport;
			else
				{
				progressMngr->EnterSubTask();

				ZzString zzFileName = ZzGetFileNameFromPath(*zzPath);
				
				// call the TVideoClip constructor (the new one you have build for txt)	
				newClip = new TArmeTextClip(theText, zzFileName.getUTF8String(), pDoc, newArmePos);
	
				theObjects->AddItem((ArmeArchivable*)newClip); // add the object to the dest array
				progressMngr->LeaveSubTask();
				}
		}
	else
		err.type = kArmeErrNoSuitableMediaHandlerFound;

        
    // figure out what to return
    return err;
}

/*
TArmeWSObj*	TArmeTextFileHandler::CreateWSObjFromFile( 
							TArmeDoc* 					pDoc, 
						//	const char*					path, 
							const ZzString*				cstrPath,
							const TArmeWSObjPos&		pos,
							MIProgressBarDlgManager*	progressMngr,
							ArmeErr*					err)
{
	ZzAssert(0);  //should not be used anymore

}
*/

int32 TArmeTextFileHandler::GetSupportedExtensionsCount() {
	return 6;
}

char* TArmeTextFileHandler::GetSupportedExtension(int32 i) {
	switch (i) {
		case 0:
			return "rtf";
		case 1:
			return "txt";
		case 2:
			return "RTF";
		case 3:
			return "TXT";
		case 4:
			return "Rtf";
		case 5:
			return "Txt";
	}
	return "???";
}

int32 TArmeTextFileHandler::GetSupportedTypesCount() {
	return 3;
}

char* TArmeTextFileHandler::GetSupportedType(int32 i) {
	switch (i) {
		case 0:
			return "RTF ";
		case 1:
			return "TEXT";
		case 2:
			return "TXT ";
	}	
	return "???";
}


