#include "bbs.h"
#include <graphics/displayinfo.h>
#include <intuition/intuition.h>
#include <graphics/gfxbase.h>
#include <intuition/imageclass.h>
#include <clib/graphics_protos.h>
#include <clib/intuition_protos.h>
void	StripIntuiMessages( struct MsgPort *mp, struct Window *win );
WORD DIM[5]= { 1,1,270,11 };
void PrintAddress(ULONG t);
void CloseAEStats(void);
extern __far struct Library *DOSBase;
static int InitZmodemCon(void)
{
 ZmodemWritePort=CreatePort("zmwriteport",0l);
 ZmodemWriteReq=CreateStdIO(ZmodemWritePort);
 ZmodemWriteReq->io_Data=(APTR)ZmodemStatsWin;
 //ZmodemWriteReq->io_Length=sizeof(*ZmodemStatsWin);
 return((int)OpenDevice("console.device",3L,ZmodemWriteReq,0L));
}
int OpenZDisplay(void)
{

 if(!ICON) {
	        ZmodemStats.Title=(UBYTE *)&ZModemInfo.TitleBar;
	        ZmodemStatsWin = (struct Window *)OpenWindow(&ZmodemStats);
	        InitZmodemCon();
	        ZmodemStatPrint("[H[J[\x30\x20\x70[H\n FileName:\n FileSize: 0\n APX Time:\n Position: 0\n Complete: 0%\n Resume P: 0\n\n LastTime: 0 Mins 0 Secs\n      CPS: 0\n\n Z Status: Awaiting ACK\n ErrorPos: 0");
	 }
  return(1);
}

void UpdateZDisplay(void)
{

    /* transfer window not open */
 if(!ICON)
 {
 ZmodemStatPrint("[H[J[\x30\x20\x70");
 sprintf(GSTR1,"[H\n FileName: %s\n",ZModemInfo.FileName);
 ZmodemStatPrint(GSTR1);
 sprintf(GSTR1," FileSize: %ld\n",ZModemInfo.Filesize);
 ZmodemStatPrint(GSTR1);
 sprintf(GSTR1," APX Time: %lu Mins %lu Secs\n",ZModemInfo.ApxTime/60L,ZModemInfo.ApxTime%60L);
 ZmodemStatPrint(GSTR1);
 ZmodemStatPrint(" Position: \n");
 ZmodemStatPrint(" Complete: \n");
 sprintf(GSTR1," Resume P: %ld\n\n",ZModemInfo.ResumePos);
 ZmodemStatPrint(GSTR1);
 sprintf(GSTR1," LastTime: %lu Mins %lu Secs\n",ZModemInfo.LastTime/60L,ZModemInfo.LastTime%60L);
 ZmodemStatPrint(GSTR1);
 sprintf(GSTR1,"      CPS: %ld Efficency %lu\n\n",ZModemInfo.Cps,ZModemInfo.Eff);
 ZmodemStatPrint(GSTR1);
 sprintf(GSTR1," Z Status: %s\n",ZModemInfo.ZStat);
 ZmodemStatPrint(GSTR1);
 sprintf(GSTR1," ErrorPos: %ld ",ZModemInfo.ErrorPos);
 ZmodemStatPrint(GSTR1);
 }
}

static void CloseZmodemConsole(void)
{
     CloseDevice(ZmodemWriteReq);
	DeleteStdIO(ZmodemWriteReq);
     DeletePort(ZmodemWritePort);
}
void CloseAEStats(void)
{
    CloseDevice(StatWriteReq);
    DeleteStdIO(StatWriteReq);
    DeletePort(StatWritePort);
}
void CloseZDisplay(void)
{

 if(!ICON) {
       /* transfer window is open */
	        CloseZmodemConsole();
	        CloseWindow(ZmodemStatsWin);
	}
}

void InitStatCon(void)
{
 StatWritePort=CreatePort("statwriteport",0l);
 StatWriteReq=CreateStdIO(StatWritePort);
 StatWriteReq->io_Data=(APTR)StatusPane;
 OpenDevice("console.device",3L,StatWriteReq,0l);
}


static int OpenConsole(void)
{
 WriteConReq->io_Data   = (APTR)MYwindow;
 WriteConReq->io_Length = sizeof(*MYwindow);
 GI1 = OpenDevice("console.device",3L,WriteConReq,0L);
 ReadConReq->io_Device  = WriteConReq->io_Device;
 ReadConReq->io_Unit    = WriteConReq->io_Unit;
 return(GI1);
}

static int InitConsole(void)
{
 extern UBYTE conin[];

 if(DStatBar)
 	InitStatCon();

 if (!(WriteConPort = CreatePort("con.write",0L))) return(FAILURE);
 if (!(WriteConReq  = CreateStdIO(WriteConPort))) return(FAILURE);

 if (!(ReadConPort  = CreatePort("con.read",0L)))  return(FAILURE);
 if (!(ReadConReq   = CreateStdIO(ReadConPort)))  return(FAILURE);
 if (OpenConsole())  return(FAILURE);

 WriteConReq->io_Command = CMD_WRITE;

 ReadConReq->io_Command  = CMD_READ;
 ReadConReq->io_Length   = 1;
 ReadConReq->io_Data     = (APTR)conin;
 SendIO(ReadConReq);
 return(FALSE);
}


void OpenDisplay(void)
{
 MYNewScreen.Depth=BitPlanes;
 if(BitPlanes!=0) {
       
         
	    if ((MYScreen = (struct Screen *)OpenScreen(&MYNewScreen)) == NULL)
          ShutDown("Can't Open Screen");    // was EXIT();
         }
	

  PaneStatus.Flags=SIMPLE_REFRESH;


 if(BitPlanes!=0)	{
	        MYNewWindow.Flags = SIMPLE_REFRESH | BACKDROP | BORDERLESS;

     MYNewWindow.Screen=MYScreen;
	    PaneStatus.Screen=MYScreen;
	    CloseIt.Screen=MYScreen;
	    ZmodemStats.Screen=MYScreen;
	} else {
              MYNewWindow.IDCMPFlags=CLOSEWINDOW;
		    MYNewWindow.Flags = SIMPLE_REFRESH | ACTIVATE | WINDOWSIZING | WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE ;
         ZmodemStats.IDCMPFlags=CLOSEWINDOW;
         ZmodemStats.Type=WBENCHSCREEN;
	    ZmodemStats.Flags = WINDOWCLOSE | SIMPLE_REFRESH | WINDOWDEPTH | WINDOWDRAG;
	    PaneStatus.Flags = SIMPLE_REFRESH | WINDOWDRAG | WINDOWDEPTH;
	}
if(DStatBar) {
	    if (( StatusPane = (struct Window *)OpenWindow(&PaneStatus)) == NULL)
         ShutDown("Cant open windows");
	}else StatusPane=NULL;


	if(BitPlanes!=0)
     {drinfo=GetScreenDrawInfo(MYScreen);
	  closeimage=(struct Image *) NewObject(NULL, "sysiclass",SYSIA_Size, 0,
	  SYSIA_Which, CLOSEIMAGE,SYSIA_DrawInfo, drinfo,TAG_END);
     
     
	MG.Width=closeimage->Width;
	MG.Height=closeimage->Height;
	MG.GadgetRender=(APTR)closeimage;
	CloseIt.FirstGadget=(APTR)&MG;
	CloseIt.Width=MG.Width-1;
     }
     else
     {
        closeimage=NULL;
        drinfo=NULL;
     }
 
if(BitPlanes!=0) if (( Iconify = (struct Window *)OpenWindow(&CloseIt)) == NULL)
	    ShutDown("Cant open windows");
        if (( MYwindow = (struct Window *)OpenWindow(&MYNewWindow)) == NULL)
	    ShutDown("Cant open windows");
 if(BitPlanes!=0) {
         if(!Sopt->Toggles[6])
         {
	      SetRGB4(&MYScreen->ViewPort,0,0,0,0);
           SetRGB4(&MYScreen->ViewPort,1,0xf,0xf,0xf);
	      SetRGB4(&MYScreen->ViewPort,2,0,0xf,0);
	      SetRGB4(&MYScreen->ViewPort,3,0xf,0xf,0);
	      SetRGB4(&MYScreen->ViewPort,4,0,0,0xf);
	      SetRGB4(&MYScreen->ViewPort,5,0xf,0,0xf);
	      SetRGB4(&MYScreen->ViewPort,6,0,0xf,0xf);
	      SetRGB4(&MYScreen->ViewPort,7,0xf,0,0);
	    }
         else
         {
 	      SetRGB4(&MYScreen->ViewPort,0,0,0,0);
           SetRGB4(&MYScreen->ViewPort,1,0xf,0,0);
           SetRGB4(&MYScreen->ViewPort,2,0,0xf,0);
	      SetRGB4(&MYScreen->ViewPort,3,0xf,0xf,0);
	      SetRGB4(&MYScreen->ViewPort,4,0,0,0xf);
	      SetRGB4(&MYScreen->ViewPort,5,0xf,0,0xf);
	      SetRGB4(&MYScreen->ViewPort,6,0,0xf,0xf);
           SetRGB4(&MYScreen->ViewPort,7,0xf,0xf,0xf);
         }
     }

if(BitPlanes==0)
{
 IconifySig=1L<<MYwindow->UserPort->mp_SigBit;
}else IconifySig=1L<<Iconify->UserPort->mp_SigBit;
 InitConsole();
 ConCharSig=1L<<ReadConPort->mp_SigBit;

 if(DStatBar && BitPlanes) {
	    
	    SizeWindow(MYwindow,0,-28);
         MoveWindow(MYwindow,0,28);
	}
  else if(DStatBar)
   {
     SizeWindow(MYwindow,0,-38);
     MoveWindow(MYwindow,0,38);
   }

 

}

