/***************************************************************************
 *   Copyright (C) 2005 by David Saxton                                    *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it 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.                                   *
 ***************************************************************************/

#ifndef LANGUAGE_H
#define LANGUAGE_H

#include <tqobject.h>
#include <tqstringlist.h>

class FlowCodeDocument;
class KTechlab;
class LogView;
class MessageInfo;
class MicroSettings;
class OutputMethodInfo;
class ProcessChain;
class ProcessOptions;
class TextDocument;
class TQProcess;

typedef TQValueList<ProcessOptions> ProcessOptionsList;

class ProcessOptionsSpecial
{
	public:
		ProcessOptionsSpecial();
		
		bool b_addToProject;
		bool b_forceList;
		TQString m_picID;
		FlowCodeDocument * p_flowCodeDocument;
		
		// Linking
		TQString m_hexFormat;
		bool m_bOutputMapFile;
		TQString m_libraryDir;
		TQString m_linkerScript;
		TQStringList m_linkLibraries;
		TQString m_linkOther;
		
		// Programming
		TQString m_port;
		TQString m_program;
};


class ProcessOptionsHelper : public TQObject
{
	Q_OBJECT
  
#define protected public
	signals:
#undef protected
		void textOutputtedTo( TextDocument * outputtedTo );
};


class ProcessOptions : public ProcessOptionsSpecial
{
	public:
		ProcessOptions();
		ProcessOptions( OutputMethodInfo info );
		
		class ProcessPath { public:
			enum MediaType
			{
				AssemblyAbsolute,
				AssemblyRelocatable,
				C,
				Disassembly,
				FlowCode,
				Library,
				Microbe,
				Object,
				Pic,
				Program,
				
				Unknown // Used for guessing the media type
			};
				
			enum Path // From_To				// processor that will be invoked first
			{
				AssemblyAbsolute_PIC,			// gpasm (indirect)
				AssemblyAbsolute_Program,		// gpasm (direct)
				
				AssemblyRelocatable_Library,	// gpasm (indirect)
				AssemblyRelocatable_Object,		// gpasm (direct)
				AssemblyRelocatable_PIC,		// gpasm (indirect)
				AssemblyRelocatable_Program,	// gpasm (indirect)
				                                          
				C_AssemblyRelocatable,			// sdcc (direct)
				C_Library,						// sdcc (indirect)
				C_Object,						// sdcc (indirect)
				C_PIC,							// sdcc (indirect)
				C_Program,						// sdcc (indirect)
				
				FlowCode_AssemblyAbsolute,		// flowcode (indirect)
				FlowCode_Microbe,				// flowcode (direct)
				FlowCode_PIC,					// flowcode (indirect)
				FlowCode_Program,				// flowcode (indirect)
				
				Microbe_AssemblyAbsolute,		// microbe (direct)
				Microbe_PIC,					// microbe (indirect)
				Microbe_Program,				// microbe (indirect)
				
				Object_Disassembly,				// gpdasm (direct)
				Object_Library,					// gplib (direct)
				Object_PIC,						// gplink (indirect)
				Object_Program,					// gplink (direct)
				
				PIC_AssemblyAbsolute,			// download from pic (direct)
				
				Program_Disassembly,			// gpdasm (direct)
				Program_PIC,					// upload to pic (direct)
				
				Invalid, // From and to types are incompatible
				None // From and to types are the same
			};
				
			static Path path( MediaType from, MediaType to );
			static MediaType from( Path path );
			static MediaType to( Path path );
		};
		
		class Method
		{
			public: enum type
			{
				Forget, // Don't do anything after processing successfully
				LoadAsNew, // Load the output as a new file
				Load // Load the output file
			};
		};
	
		/**
		 * Tries to guess the media type from the url (and possible the contents
		 * of the file as well).
		 */
		static ProcessPath::MediaType guessMediaType( const TQString & url );
		/**
		 * The *final* target file (not any intermediatary ones)
		 */
		TQString targetFile() const { return m_targetFile; }
		/**
		 * This sets the final target file, as well as the initial intermediatary one
		 */
		void setTargetFile( const TQString &file );
		
		void setIntermediaryOutput( const TQString &file ) { m_intermediaryFile = file; }
		TQString intermediaryOutput() const { return m_intermediaryFile; }
		
		void setInputFiles( const TQStringList & files ) { m_inputFiles = files; }
		TQStringList inputFiles() const { return m_inputFiles; }
		
		void setMethod( Method::type method ) { m_method = method; }
		Method::type method() const { return m_method; }
		
		void setProcessPath( ProcessPath::Path path ) { m_processPath = path; }
		ProcessPath::Path processPath() const { return m_processPath; }
	
		/**
		 * If the output is text; If the user has selected (in config options)
		 * ReuseSameViewForOutput, then the given TextDocument will have its
		 * text set to the output if the TextDocument is not modified and has
		 * an empty url. Otherwise a new TextDocument will be created. Either
		 * way, once the the processing has finished, a signal will be emitted
		 * to the given receiver passing a TextDocument * as an argument. This
		 * is not to be confused with setTextOutputtedTo, which is called once
		 * the processing has finished, and will call-back to the slot given.
		 */
		void setTextOutputTarget( TextDocument * target, TQObject * receiver, const char * slot );
		/**
		 * @see setTextOutputTarget
		 */
		TextDocument * textOutputTarget() const { return m_pTextOutputTarget; }
		/**
		 * @see setTextOuputTarget
		 */
		void setTextOutputtedTo( TextDocument * outputtedTo );
		
	protected:
		TextDocument * m_pTextOutputTarget;
		ProcessOptionsHelper * m_pHelper;
		bool b_targetFileSet;
		TQStringList m_inputFiles;
		TQString m_targetFile;
		TQString m_intermediaryFile;
		Method::type m_method;
		ProcessPath::Path m_processPath;
};


/**
@author Daniel Clarke
@author David Saxton
*/
class Language : public TQObject
{
	Q_OBJECT
  
	public:
		Language( ProcessChain *processChain, KTechlab *parent, const TQString &name );
		~Language();
	
		/**
		 * Compile / assemble / dissassembly / whatever the given input.
		 * @returns true if processing was started succesfully (this is different to finishing successfuly).
		 */
		virtual void processInput( ProcessOptions options ) = 0;
		/**
		 * Return the ProcessOptions object current state
		 */
		ProcessOptions processOptions() const { return m_processOptions; }
		/**
		 * Return the output path from the given input path. Will return None
		 * if we've done processing.
		 */
		virtual ProcessOptions::ProcessPath::Path outputPath( ProcessOptions::ProcessPath::Path inputPath ) const = 0;
	
	signals:
		/**
		 * Emitted when the processing was successful.
		 * @param language Pointer to this class
		 */
		void processSucceeded( Language *language );
		/**
		 * Emitted when the processing failed.
		 * @param language Pointer to this class
		 */
		void processFailed( Language *language );
	
	protected:
		/**
		 * Examines the string for the line number if applicable, and creates a new
		 * MessageInfo for it.
		 */
		virtual MessageInfo extractMessageInfo( const TQString &text );
	
		/**
		 * Reset the error count
		 */
		void reset();
		void outputMessage( const TQString &message );
		void outputWarning( const TQString &message );
		void outputError( const TQString &error );
		void finish( bool successful );
	
		int m_errorCount;
		KTechlab *p_ktechlab;
		ProcessOptions m_processOptions;
		ProcessChain *p_processChain;
	
		/**
		 * A message appropriate to the language's success after compilation or similar.
		 */
		TQString m_successfulMessage;
		/**
		 * A message appropriate to the language's failure after compilation or similar.
		 */
		TQString m_failedMessage;
};

#endif
