/*
** This Is Going To Be The Main GUI.. Or Should That Be MUI! ;) For The InterFace.
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/execbase.h>
#include <exec/libraries.h>
#include <exec/libraries.h>

#include <dos/dos.h>

#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>

#include <libraries/gadtools.h>
#include <libraries/commodities.h>
#include <libraries/mui.h>

#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <graphics/gfxmacros.h>

#include <workbench/workbench.h>
#include <workbench/startup.h>

#include <clib/alib_protos.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/icon_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <clib/commodities_protos.h>
#include <clib/dos_protos.h>
#include <clib/diskfont_protos.h>
#include <clib/wb_protos.h>
#include <clib/dos_protos.h>
#include <clib/asl_protos.h>
#include <clib/muimaster_protos.h>

#include <pragmas/exec_sysbase_pragmas.h>
#include <pragmas/muimaster_pragmas.h>



struct Library *MUIMasterBase;			// For The MUI Library
struct Remember *General_Memory;
extern struct Library *SysBase;
#define LIST_VIEW 0
#define ID_CANCEL 1

APTR  	Main_Object,
        Main_Window,
        ListView_Object,
        Task_Address,
        Window_Address,
        Ports_Address,
        Vector_Address,
	Semaphores_Address,
        Universal_1,
        Universal_2,
        Universal_3,
        Universal_4,
        Universal_5,
        Main_Child,
        Pages,
        Name,
        Universal_6;

struct MailStruct
{
     char MessageStat;   /* This is A simple ASCII Read Statis */
     char DontKnow[4];   /* Dont Know Might be a Space         */
     char MessageNumber; /* The Message Number                 */
     char ToName[30];    /* The person the Mail is TO          */
     char Space1[1];     /* Some Kind of Space Again           */
     char FromName[30];  /* The Person The Mail is From        */
     char Space2[1];     /* Another Space                      */
     char Subject[30];   /* The Subject of The Mail            */
     char Space3[1];     /* Another Space                      */
     char Date[11];      /* SomeHow This is The Date!!!!       */
};


/* ProtoTypes In This File */

void Open_Librarys( void );      /* Open Up The Current Version Of The MUI Master Library */
void Shut_Down(APTR ,char *);	 /* Shut Down The Current Application Because of Failure  */
int  Start_GUI( void );          /* Start The Graphical User Interface Run By MUI.        */
void Input_Handler( void );
BOOL GetIconTooltype(char *, char *, char *);
static char *Hand_ler[40];
void main( void )
{
    Open_Librarys();
    Start_GUI();
    Child, Name = VGroup,
       Child, SimpleButton("Fred"),
    End;
    
    DoMethod(Pages,OM_ADDMEMBER,Name);
    Input_Handler();
    FreeRemember(&General_Memory,TRUE);
   
}



int Start_GUI( void )
{
   char *Buffer;
   char *String;
   int x=0;
   int NumbConfs=0;
   Buffer = AllocRemember(&General_Memory,200,MEMF_CLEAR);
   String = AllocRemember(&General_Memory,200,MEMF_CLEAR);
   Open_Librarys(); 
   GetIconTooltype("BBS:ConfConfig","NCONFS",Buffer);     
   NumbConfs = atoi(Buffer);

   
     for(x=0; x<NumbConfs+1; x++)
      {
       sprintf(Buffer,"NAME.%d",x+1);
       Hand_ler[x]=AllocRemember(&General_Memory,50,MEMF_CLEAR);
       GetIconTooltype("BBS:ConfConfig", Buffer, Hand_ler[x]);
      }
      //sprintf(Hand_ler[x+1],"\0");
      Hand_ler[x]=NULL;
   
  

   printf("Actually Get Here Before We Crash\n");
   Main_Object = ApplicationObject,
      MUIA_Application_Title      , "MUI_DEMO",
      MUIA_Application_Version    , "$VER: Mui Demo 1.3 (20.04.95)",
      MUIA_Application_Copyright  , "1995/96, Cray - 1",
      MUIA_Application_Author     , "A((eSS",
      MUIA_Application_Description, "Just For Testting Out MUI",
      MUIA_Application_Base       , "MUID", 
       SubWindow, Main_Window = WindowObject,
        MUIA_Window_Title      , "MUI Demo Application",
        MUIA_Window_ID         , "CRAY",    
                                
        WindowContents, VGroup,GroupFrameT("Main Display"),
          Child, Pages = RegisterGroup(Hand_ler),
                  Child, SimpleButton("FISH"),
                        End,
        End,
        
      End,
    End;
   
                                                                                      
                  if(!Main_Object) Shut_Down(Main_Object,"Problem Starting Up Application.");
                  if(!Main_Window) Shut_Down(Main_Window,"Problem With The Window.");                   
                         
                SetAttrs(Main_Window,MUIA_Window_Open,TRUE,TAG_DONE);  /* Open The Window */
           

       

   DoMethod(Main_Window,MUIM_Notify,				/* Tell CloseWindow To Inform You */
             MUIA_Window_CloseRequest,TRUE,Main_Object,2,
                       MUIM_Application_ReturnID,ID_CANCEL);

  return(TRUE);
}

BOOL GetIconTooltype(char *IconName, char *text, char *buffer)
{
    char  *tooltype;
    char  **toolarray;
    struct DiskObject *dobj;
       dobj = GetDiskObject(IconName);
       if (dobj==NULL) 
          { 
            printf("Something Up With The Disk Object");
            return(0);
           
          }
       toolarray = dobj->do_ToolTypes;
       if (tooltype=FindToolType(toolarray, text))
          {
           strcpy(buffer,tooltype);
           FreeDiskObject(dobj);
           return(1);
          }
       else
          {
           buffer=NULL;
           FreeDiskObject(dobj);
           return(0);
          }
}



void Input_Handler( void )
{
  ULONG signals;
  BOOL running = TRUE;
   while (running)
      {
          switch (DoMethod(Main_Object,MUIM_Application_Input,&signals))
              {
                  case MUIV_Application_ReturnID_Quit:
                          running = FALSE;
                         break;
                        
                  case LIST_VIEW:
                        /* This is the main List View Object */
                         break;
              }
        if (running && signals) Wait(signals);
      }                 
           
                 
       	Shut_Down(Main_Object,NULL);
}




void Open_Librarys( void )
{
 
         if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
              Shut_Down(NULL,"Failed to open "MUIMASTER_NAME"."); 
}
 
 
 
void Shut_Down(APTR app,char *str)
{
        if (app)
                MUI_DisposeObject(app);
       if (MUIMasterBase)
              CloseLibrary(MUIMasterBase);
        if (str)
            {
                puts(str);
                exit(20);
            }
        exit(0);
}
