/*
	Author:	J.v.d.Loo

	NOTE:	I have currently not installed the required header files.
			I copied the whole archive to RAM: and compiled all there.

	USAGE:	1> example TimesNewRoman.font 15
*/

#include "RAM:Catharsis/include/catharsis.h"		/* Standard include file */
#include "RAM:Catharsis/include/catharsisbase.h"	/* Standard include file */
#include "RAM:Catharsis/clib/catharsis_protos.h"	/* Prototype */
#include "RAM:Catharsis/pragma/catharsis_lib.h"		/* Pragma */

#include <graphics/view.h>
#include <intuition/intuition.h>
#include <utility/tagitem.h>

#include <pragma/exec_lib.h>
#include <clib/exec_protos.h>

#include <pragma/graphics_lib.h>
#include <clib/graphics_protos.h>

#include <pragma/intuition_lib.h>
#include <clib/intuition_protos.h>

#include <pragma/diskfont_lib.h>
#include <clib/diskfont_protos.h>

struct CatharsisBase *CatharsisBase;
struct GfxBase *GfxBase;
struct IntuitionBase *IntuitionBase;
struct Library *DiskfontBase;

struct TextAttr TextFontAttr;
struct TextFont *TextFont;
struct TextFont *CatharsisFont;

struct NewWindow WinRec =
{
	0,0,
	640,240,
	-1,-1,
	IDCMP_CLOSEWINDOW|IDCMP_INTUITICKS,
	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_RMBTRAP,
	NULL,
	NULL,
	"Catharsis Example",
	NULL,
	NULL,
	640,240,
	-1,-1,
	WBENCHSCREEN
};

struct Window *Window;

UBYTE StrTxt1[] = "Going to create Catharsis-font, that takes its time...";
UBYTE StrTxt2[] = "Done!";

UBYTE *StrTxts[3] =
{
	"The quick brown fox jumped over the lazy dog  ",	// until the hunter shot
	"0 1 2 3 4 5 6 7 8 9   ",
	"% & ! § å ° ® þ ¤ ¡ ø £ ð æ   "
};

unsigned long ReadPensCol( ULONG pen)
{
	ULONG table[3], val, val1, help;
	UBYTE ms;

	if (GfxBase->LibNode.lib_Version > 38)	// OS 3 or better?
	{
		GetRGB32( IntuitionBase->ActiveScreen->ViewPort.ColorMap, pen, 1, (ULONG *) &table);
		ms = table[0] >> 24;
		val = ms << 16;

		ms = table[1] >> 24;
		val |= ms << 8;

		ms = table[2] >> 24;
		val |= ms;
	}
	else
	{
		val = GetRGB4( IntuitionBase->ActiveScreen->ViewPort.ColorMap, pen);	// OS 1.2 through 2.1 compatible...
		help = ((val & 0xF00) >> 8);
		help |= (help << 4);
		val1 = help << 16;

		help = ((val & 0xF0) >> 4);
		help |= (help << 4);
		val1 |= (help << 8);

		help = (val & 0xF);
		help |= (help << 4);
		val1 |= help;

		val = val1;
	}

	return val;
}


int main( unsigned long argc, unsigned char **argv)
{
	ULONG bg, fg, cts, os[3], pen, penreserved, cycle, imsgclass, status;
	struct IntuiMessage *imsg;
	struct TagItem tags[2];
	

	if ( (CatharsisBase = (struct CatharsisBase *) OpenLibrary( "catharsis.library", 1)) )
	{
		GfxBase = CatharsisBase->cts_GfxBase;
		IntuitionBase = CatharsisBase->cts_IntuitionBase;

		bg  = ReadPensCol( 0);
		fg  = ReadPensCol( 1);
		cts = CatharsisColorValueA( bg, fg, &os[0], &os[1], &os[2]);
		printf("BG: 0x%06lX, FG: 0x%06lX, Catharsis-Text-Color: 0x%06lX or RGB32: 0x%08lX, 0x%08lX, 0x%08lX\n", bg, fg, cts, os[0], os[1], os[2]);

		if ( (DiskfontBase = (struct Library *) OpenLibrary( "diskfont.library", 33)) )
		{
			if (argc > 2)
			{
				TextFontAttr.ta_Name = argv[1];
				status = atoi( argv[2]);
				TextFontAttr.ta_YSize = status;
				TextFontAttr.ta_Style = NULL;
				TextFontAttr.ta_Flags = FPF_ROMFONT;
				if ( !(TextFont = OpenFont( &TextFontAttr)) || status != TextFont->tf_YSize)
				{
					if (TextFont)
						CloseFont( TextFont);
					TextFontAttr.ta_Flags = FPF_DISKFONT;
					TextFont = OpenDiskFont( &TextFontAttr)
				}

				if (TextFont)
				{
					if ((Window = OpenWindow( &WinRec)) )
					{
						penreserved = -1L;

						if (GfxBase->LibNode.lib_Version > 38)
						{
							tags[0].ti_Tag = OBP_Precision;
							tags[0].ti_Data = PRECISION_EXACT;
							tags[1].ti_Tag = TAG_DONE;

							pen = ObtainBestPenA( Window->WScreen->ViewPort.ColorMap, os[0], os[1], os[2], tags);
							if (pen == -1L)
							{
								pen = FindColor( Window->WScreen->ViewPort.ColorMap, os[0], os[1], os[2], -1);
							}
							else
							{
								penreserved = pen;
							}
						}
						else
						{
							pen = 3;
						}

						SetFont( Window->RPort, TextFont);
						SetAPen( Window->RPort, 1);
						SetDrMd( Window->RPort, JAM2);
						Move( Window->RPort, 10, Window->BorderTop + 8 + TextFont->tf_Baseline);
						Text( Window->RPort, StrTxt1, strlen (StrTxt1) );

						if ( (CatharsisFont = OpenCatharsisFontA( TextFont)) )
						{
							Move( Window->RPort, 10, Window->BorderTop + 8 + TextFont->tf_Baseline + TextFont->tf_YSize + 4);
							Text( Window->RPort, StrTxt2, strlen (StrTxt2) );

							cycle = NULL;
							status = -1;
							imsgclass = NULL;
							imsg = NULL;
							while (imsgclass != IDCMP_CLOSEWINDOW)
							{
								WaitPort( Window->UserPort);
								while ( (imsg = (struct IntuiMessage *) GetMsg( Window->UserPort)) )
								{
									imsgclass = imsg->Class;
									ReplyMsg( (struct Message *) imsg);

									if (imsgclass == IDCMP_INTUITICKS)
										cycle ++;
									if ( cycle == 50)
									{
										cycle = 1;
										status ++;
										if (status > 2)
											status = 0;
										SetDrMd( Window->RPort, JAM2);
										Move( Window->RPort, 10, Window->BorderTop + 8 + TextFont->tf_Baseline);
										Text( Window->RPort, StrTxts[status], strlen (StrTxts[status]) );
										Move( Window->RPort, 10, Window->BorderTop + 8 + TextFont->tf_Baseline + TextFont->tf_YSize + 4);
										CatharsisTextA( Window->RPort, StrTxts[status], strlen (StrTxts[status]), pen, CatharsisFont);
									}
								}
							}
							CloseCatharsisFontA( CatharsisFont);
						}
						else
						{
							printf("Cannot open/create font!\n");
						}

						if (penreserved != -1L)
							ReleasePen( Window->WScreen->ViewPort.ColorMap, penreserved);
						CloseWindow( Window);
					}
					else
					{
						printf("Cannot open a window of size 640x240 pixels!\n");
					}
					CloseFont( TextFont);
				}
				else
				{
					printf("Cannot open specified font »%s«, size %ld!\n", argv[1], status);
				}

			}

			CloseLibrary( DiskfontBase);
		}
		CloseLibrary( (struct Library *) CatharsisBase);
	}
	return 0;
}
