#include <classes/Exec/Libraries.h>

#include <intuition/intuition.h>

#pragma -
#include <pragma/exec_lib.h>
#include <pragma/dos_lib.h>
#include <pragma/intuition_lib.h>
#include <clib/alib_stdio_protos.h>
#include <string.h>
#pragma +

extern struct Library *IntuitionBase;
extern struct Library *DosBase;

LibraryBaseC::LibraryBaseC(STRPTR name, ULONG version)
{
	if (!(Base = OpenLibrary(name,version)))
		not_open = TRUE;
};

LibraryBaseC::~LibraryBaseC()
{
	if (Base)
	{
		CloseLibrary(Base);
		Base = NULL;
	};
};

UWORD LibraryBaseC::version() const
{
	if (Base)
		return Base->lib_Version;
	return 0;
};

BOOL LibraryBaseC::not_open = FALSE;

// **********************************************************

LibraryBaseErrC::LibraryBaseErrC(STRPTR name, ULONG version)
	: LibraryBaseC(name,version)
{
	if (!isOpen())
	{
		BOOL close_intuition = FALSE;
		if (!IntuitionBase)
		{
			IntuitionBase = OpenLibrary("intuition.library",33);
			close_intuition = TRUE;
		};
		if (IntuitionBase)
		{
			ULONG actual;
			actual = 0;
			struct Library *l = OpenLibrary(name,0);
			if (l)
				actual = l->lib_Version;
			if (IntuitionBase->lib_Version >= 37)
			{
				char programname[80];
				if (DosBase)
				{
					if (!GetProgramName(programname,80))
						sprintf(programname,"Application")
				}
				else {
					sprintf(programname,"Application")
				};
				if (actual)
				{
					struct EasyStruct myES =
						{ sizeof(struct EasyStruct),0,"Application Request",
						  "\"%s\" version %ld expected.\n"
						  "Found only version %ld.\n"
						  "Unable to run \"%s\".",
						  "OK"
						};
					EasyRequest(NULL,&myES,NULL,name,version,actual,programname);
				}
				else {
					struct EasyStruct myES =
						{ sizeof(struct EasyStruct),0,"Application Request",
						  "Cannot open \"%s\".\n"
						  "Unable to run \"%s\".",
						  "OK"
						};
					EasyRequest(NULL,&myES,NULL,name,programname);
				}
			}
			else {
				if (actual)
				{
					char bodytext1[80];
					char bodytext2[80];
					struct TextAttr TopazFont8 =
						{ "topaz.font",8,0,0 };
					struct IntuiText Body3 =
						{ AUTOFRONTPEN,AUTOBACKPEN,AUTODRAWMODE,
						  8,24,&TopazFont8,
						  "Unable to run this application.",NULL };
					struct IntuiText Body2 =
						{ AUTOFRONTPEN,AUTOBACKPEN,AUTODRAWMODE,
						  8,16,&TopazFont8,bodytext2,&Body3 };
					struct IntuiText Body =
						{ AUTOFRONTPEN,AUTOBACKPEN,AUTODRAWMODE,
						  8,8,&TopazFont8,bodytext1,&Body2 };
					struct IntuiText OK =
						{ AUTOFRONTPEN,AUTOBACKPEN,AUTODRAWMODE,
						  AUTOLEFTEDGE,AUTOTOPEDGE,NULL," OK ",NULL };
					sprintf(bodytext1,"\"%s\" version %ld expected.",name,version);
					sprintf(bodytext2,"Found only version %ld.",actual);
					ULONG len = strlen(bodytext1);
					if (len < strlen(bodytext2))
						len = strlen(bodytext2);
					if (len < 32)
						len = 32;
					AutoRequest(NULL,&Body,NULL,&OK,0,0,len*8+48,70);
				}
				else {
					char bodytext[80];
					struct TextAttr TopazFont8 =
						{ "topaz.font",8,0,0 };
					struct IntuiText Body2 =
						{ AUTOFRONTPEN,AUTOBACKPEN,AUTODRAWMODE,
						  8,16,&TopazFont8,
						  "Unable to run this application.",NULL };
					struct IntuiText Body =
						{ AUTOFRONTPEN,AUTOBACKPEN,AUTODRAWMODE,
						  8,8,&TopazFont8,bodytext,&Body2 };
					struct IntuiText OK =
						{ AUTOFRONTPEN,AUTOBACKPEN,AUTODRAWMODE,
						  AUTOLEFTEDGE,AUTOTOPEDGE,NULL," OK ",NULL };
					sprintf(bodytext,"Cannot open \"%s\".",name);
					ULONG len = strlen(bodytext);
					if (len < 32)
						len = 32;
					AutoRequest(NULL,&Body,NULL,&OK,0,0,len*8+48,62);
				};
			};
		};
		if (close_intuition && IntuitionBase)
		{
			CloseLibrary(IntuitionBase);
			IntuitionBase = NULL;
		};
	};
};
