#include <stdio.h>
#include <clib/intuition_protos.h>
#include <clib/dos_protos.h>

void IDCMPHandler( void );       //Handles The IDCMP
void PlotVectors( char *, int); //Well It Plots Vectors
void Cls( void);               //Clears The Screen
void LoadVect( void );        //Good Routine!
void SaveVect( void);        //Not Yet In!
void DrawLine( int );

#include "Screen.c"
#include <clib/asl_protos.h>

       FILE *Vectors;      
int counter=0;
WORD Start[200][2];
char    PathofFile[200];
int     Fcount;
int  Frame=0;
struct VectorFile
    {
    WORD   xCord;
    WORD   yCord;
    };
    
struct  BTVectorFile
    {
     char  YCord;
     char  XCord;
    };

    WORD StartPoint[2],EndPoint[2],z[200][200];
    int Fish[200];
   //WORD *Points[];

struct VectorFile *VectorPTR, Vector;
struct IntuiText MyText;


main()
{  
    SetupScreen();
    OpenVectorWindow();
    Cls();  
    IDCMPHandler();
    CloseVectorWindow();
    CloseDownScreen();
} 

void Cls( void )
{
    SetAPen(VectorWnd->RPort,1);
    RectFill(VectorWnd->RPort,8,15,488,221); 
}

void PlotVectors( char Path[200],int s)
{
    int x=0;
    int points;
    BOOL dead=FALSE,VectFile=TRUE;
    char buffer[200];
    VectorPTR = &Vector;
sprintf(buffer,Path,s);   
Vectors = fopen(buffer,"rb");
if(Vectors==0)
  {
    VectFile=FALSE;
    dead=TRUE;
    printf("Raaaaa! I Had Trouble With the file!\n");
    DisplayBeep(0);
 }
while(!dead)
    {      
      x++;
      points = fread(VectorPTR,4,1,Vectors);
       if(Vector.yCord=='\xFF' &Vector.xCord=='\xFF')
         {
            printf("YO! End of The Vector File Reached!\n");
            dead=TRUE;
            break;
         }
        if(x<2)
        {
            printf("This is The First Point in the Vector",counter);
            Start[Frame][0]=Vector.yCord;
            counter++;
            Start[Frame][1]=Vector.xCord;
            counter++;
            Move(VectorWnd->RPort,Start[Frame][1],Start[Frame][0]);
        }
        z[Frame][counter]=Vector.xCord;
        z[Frame][counter+1]=Vector.yCord;
        Fish[Frame]++;
        counter++;
        counter++;
        }   
if(VectFile)
{     
  Move(VectorWnd->RPort,Start[Frame][1],Start[Frame][0]);
   SetAPen(VectorWnd->RPort,0); 
   PolyDraw(VectorWnd->RPort,(counter-2)/2,z[Frame]);
} 
    counter=0;           
    Frame++;
    fclose(Vectors);
}

void DrawLine(int h)
{
    Move(VectorWnd->RPort,Start[h][1],Start[h][0]);
    SetAPen(VectorWnd->RPort,0);
    printf("PolyDraw Has Started\n");
    printf("In Frame - %d There Are %d points\n",h,Fish[h]);
    PolyDraw(VectorWnd->RPort,Fish[h]-1,z[h]);
   // PolyDraw(VectorWnd->RPort,(counter-2),z[1]);
    printf("PolyDraw Has Finished\n");
}

void IDCMPHandler( void )
{
 struct IntuiMessage *imsg;
 struct Gadget       *gad;
 int frame,w,x,y,point=0,mode,click=0,clicked=0;
 char buffer[200];
 WORD origin[2];
 BOOL   terminated = FALSE;
 BOOL   pressed = FALSE;
 BOOL   first = TRUE;
 BOOL   PENUP = TRUE;
 while(!terminated)
       {
        Wait(1 <<VectorWnd->UserPort->mp_SigBit);
        while((!terminated) && (imsg = GT_GetIMsg(VectorWnd->UserPort)))
             {

              x=imsg->MouseX;
              y=imsg->MouseY;
              sprintf(buffer,"x-%d,y-%d\0",x,y);
              if( x>8 && y>16 && x<488 && y<221)SetWindowTitles(VectorWnd,buffer,buffer);
              if( x>8 && y>16 && x<488 && y<221)
               {

                  if(imsg->Class==IDCMP_MOUSEBUTTONS && imsg->Code==SELECTDOWN)
                    {
                        pressed=TRUE;
                    }
               
                  if(pressed) /* We The Mouse Moved or The Button Pressed */
                    {
                        printf("You pressed the Mouse Button\n");
                      if(first) /* Is This The First Point */
                         {
                           StartPoint[0]=x;
                           StartPoint[1]=y;
                           Move(VectorWnd->RPort,StartPoint[0],StartPoint[1]);
                           printf("This is the First Ever point\n");
                           first=FALSE;
                           pressed=FALSE;
                           break;
                         }

                       
                       if(clicked>499) /* Have We Too Many Vectors */
                         {
                           SetWindowTitles(VectorWnd,"Sorry Over 500 Points Not Allowed","Sorry Over 500 Points Not Allowed");
                           pressed=FALSE;
                           break;
                         }
                         
                
                   if(!first) /* If We Are Not On The First Point */
                      { 
                       z[0][clicked]=x;
                       z[0][clicked+1]=y;
                       SetAPen(VectorWnd->RPort,0);
                       printf("Now Drawing x%d-%d , and y%d-%d and we are at %d\n",clicked,z[clicked],clicked+1,z[clicked+1]);
                       Draw(VectorWnd->RPort,z[0][clicked],z[0][clicked+1]); 
                       point++;
                       GT_SetGadgetAttrs(VectorGadgets[7],
                       VectorWnd, NULL,
                       GTNM_Number, point,
                       TAG_END); 
                       clicked=clicked+2;
                      }  
                      pressed=FALSE;                      
                    }
             }

               switch(imsg->Class)
                     {                 
                           
                        case IDCMP_GADGETUP:
                              gad = (struct Gadget *)imsg->IAddress;
                               switch(gad->GadgetID)
                                     {
                                        case GD_CUSTOM_1:  
                                        Cls();                                
                                        DrawLine(5);
                                        break;
                                        
                                        case GD_CUSTOM_2:
                                        for(w=0;w<100;w++)
                                        {
                                            Cls();   
                                            PlotVectors("sources:vectors/gavin/gav.%d",w);
                                        }
                                        //for(w=0;w<100;w++)
                                       // {
                                        //    DrawLine(w);
                                       // }
                                        break;
                                     
                                        case GD_CUSTOM_3:
                                        Debug(0);
                                        break;
                                        
                                   
                                        
                                        case GD_CUSTOM_4:
                                        Cls();
                                        frame = imsg->Code;
                                        PlotVectors("sources:vectors/gavin/gav.%d.v",frame);
                                        break;

                                        case GD_CUSTOM_6:
                                        terminated=TRUE;
                                        break;
                                    }
                                          
                         
            
                         case IDCMP_REFRESHWINDOW:
                              GT_BeginRefresh(VectorWnd);
                              GT_EndRefresh(VectorWnd,TRUE);
                              break;
                         
                         case IDCMP_MENUPICK:
                              switch(imsg->Code)
                                    {
                                    case 63680:
                                    terminated=TRUE;
                                    break;
                                    
                                    case 6145:
                                    LoadVect();
                                    break;
                                    
                                    case 33:
                                    DisplayBeep(0);
                                    break;
                                    }
                                     break;
                              
                         case IDCMP_CLOSEWINDOW:
                               terminated=TRUE;
                               break;

                     }

             GT_ReplyIMsg(imsg);
             }
       }
}   

void LoadVect( void )
{
    int x=0,w;
    unsigned char null;
    BOOL dead=FALSE;
   struct FileRequester *Requester;
   struct WBArg *Argum;
   struct TagItem MyTags[6]; 
   MyTags[0].ti_Tag=ASL_Hail;
   MyTags[0].ti_Data=(unsigned long)"Please Select Cobras Vectors\0";
   MyTags[1].ti_Tag=ASLFR_PositiveText;
   MyTags[1].ti_Data=(unsigned long)"Open..\0";
   MyTags[2].ti_Tag=ASLFR_NegativeText;
   MyTags[2].ti_Data=(unsigned long)"Abort..\0";;
   MyTags[5].ti_Tag=TAG_DONE;
   MyTags[3].ti_Tag=ASL_FuncFlags;
   MyTags[3].ti_Data=FILF_MULTISELECT;
   MyTags[4].ti_Tag=ASL_Window;
   MyTags[4].ti_Data=(unsigned long)VectorWnd;
    Requester = AllocAslRequest(ASL_FileRequest,MyTags);
    if(!AslRequest(Requester,NULL))
           {
               DisplayBeep(0);
           }
   Fcount=Requester->rf_NumArgs;
  // for(x=0;x<Fcount;x++)
  // {
    //Vectors = fopen(Requester->rf_ArgList[x].wa_Name,"r");
//    Vectors = fopen("vectors:access.v","r");
   
   FreeAslRequest(Requester);
   for(w=0;w<Fcount;w++)
   {
        Cls();
        PlotVectors(PathofFile,w);
        }
}
