/*
 *  Source generated with GadToolsBox V1.3
 *  which is (c) Copyright 1991,92 Jaba Development
 */

#include <exec/types.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 <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/gadtools.h>
#include <proto/graphics.h>
#include <string.h>
#include <time.h>

#include "edit_appt.h"
#include "ct.h"

extern struct dayslot  *slots;
extern APTR            VisualInfo;
extern struct TextAttr topaz8;
extern int             lastSlot;

struct Window        *Project0Wnd = NULL;
struct Gadget        *Project0GList = NULL;
struct Gadget        *Project0Gadgets[10];
UWORD                 Project0Left = 88;
UWORD                 Project0Top = 47;
UWORD                 Project0Width = 365;
UWORD                 Project0Height = 142;
UBYTE                *Project0Wdt = (UBYTE *)"Edit Appointment";

long  ey, em, ew, ed, emf; 

struct IntuiText  Project0IText[] = {
    1, 0, JAM1,245, 60,  &topaz8, (UBYTE *)"days", &Project0IText[1],
    1, 0, JAM1,245, 75,  &topaz8, (UBYTE *)"days", &Project0IText[2],
    1, 0, JAM1,245, 105, &topaz8, (UBYTE *)"minutes before", &Project0IText[3],
    1, 0, JAM1,245, 120, &topaz8, (UBYTE *)"times", &Project0IText[4],
    1, 0, JAM1,245, 90,  &topaz8, (UBYTE *)"half hours", NULL };


int OpenEditWindow(void)
  {
    struct NewGadget    ng;
    struct Gadget       *g;
    UWORD               offx, offy;
    int                 val;

    offx = Scr->WBorLeft;
    offy = Scr->WBorTop + Scr->RastPort.TxHeight + 1;

    if ( ! ( g = CreateContext( &Project0GList )))
        return( 1L );

    ng.ng_LeftEdge        =    offx + 10;
    ng.ng_TopEdge         =    offy + 9;
    ng.ng_Width           =    26;
    ng.ng_Height          =    11;
    ng.ng_GadgetText      =    (UBYTE *)"every _year";
    ng.ng_TextAttr        =    &topaz8;
    ng.ng_GadgetID        =    GD_Gadget00;
    ng.ng_Flags           =    PLACETEXT_RIGHT;


    ng.ng_VisualInfo      =    VisualInfo;

	 ey = (slots[lastSlot].active && (slots[lastSlot].first->flags & ALL_YEARS))  ? TRUE : FALSE;
	 em = (slots[lastSlot].active && (slots[lastSlot].first->flags & ALL_MONTHS)) ? TRUE : FALSE;
	 ed = (slots[lastSlot].active && (slots[lastSlot].first->flags & ALL_DAYS))   ? TRUE : FALSE;
	 ew = (slots[lastSlot].active && (slots[lastSlot].first->flags & ALL_WEEKS))  ? TRUE : FALSE;
	 emf =(slots[lastSlot].active && (slots[lastSlot].first->flags & EVERY_MON_FRI)) ? TRUE : FALSE;
//	 printf("Testflags: mem: Y%d, M%d, D%d, W%d, MF%d\n", ey, em, ed, ew, emf);

    g = CreateGadget(CHECKBOX_KIND, g, &ng, 
                     GTCB_Checked, ey, 
                     GT_Underscore, '_', 
                     TAG_DONE);

    Project0Gadgets[ 0 ] = g;

    ng.ng_TopEdge         =    offy + 25;
    ng.ng_GadgetText      =    (UBYTE *)"every _month";
    ng.ng_GadgetID        =    GD_Gadget10;

    g = CreateGadget(CHECKBOX_KIND, g, &ng, 
                     GTCB_Checked, em, 
                     GT_Underscore, '_', 
                     TAG_DONE);

    Project0Gadgets[ 1 ] = g;

    ng.ng_LeftEdge        =    offx + 185;
    ng.ng_TopEdge         =    offy + 9;
    ng.ng_GadgetText      =    (UBYTE *)"every _day";
    ng.ng_GadgetID        =    GD_Gadget20;

    g = CreateGadget(CHECKBOX_KIND, g, &ng, 
                     GTCB_Checked, ed, 
                     GT_Underscore, '_', 
                     TAG_DONE);

    Project0Gadgets[ 2 ] = g;

    ng.ng_TopEdge         =    offy + 25;
    ng.ng_GadgetText      =    (UBYTE *)"every _week";
    ng.ng_GadgetID        =    GD_Gadget30;

    g = CreateGadget(CHECKBOX_KIND, g, &ng, 
                     GTCB_Checked, ew, 
                     GT_Underscore, '_', 
                     TAG_DONE);

    Project0Gadgets[ 3 ] = g;

    ng.ng_LeftEdge        =    offx + 10;
    ng.ng_TopEdge         =    offy + 40;
    ng.ng_GadgetText      =    (UBYTE *)"Monday-_Friday";
    ng.ng_GadgetID        =    GD_Gadget40;

    g = CreateGadget(CHECKBOX_KIND, g, &ng, 
                     GTCB_Checked, emf, 
                     GT_Underscore, '_', 
                     TAG_DONE);

    Project0Gadgets[ 4 ] = g;

    ng.ng_LeftEdge        =    offx + 200;
    ng.ng_TopEdge         =    offy + 60;
    ng.ng_Width           =    40;
    ng.ng_Height          =    12;
    ng.ng_GadgetText      =    (UBYTE *)"Warn in advance";
    ng.ng_GadgetID        =    GD_Gadget50;
    ng.ng_Flags           =    PLACETEXT_LEFT;

	 val = slots[lastSlot].active ? slots[lastSlot].first->lookahead : 0;
    g = CreateGadget( INTEGER_KIND, g, &ng, GTIN_Number, val, GTIN_MaxChars, 10, TAG_DONE );

    Project0Gadgets[ 5 ] = g;

//    ng.ng_LeftEdge        =    offx + 213;
    ng.ng_TopEdge         =    offy + 75;
    ng.ng_GadgetText      =    (UBYTE *)"Duration of appointment";
    ng.ng_GadgetID        =    GD_Gadget60;

	 val = slots[lastSlot].active ? slots[lastSlot].first->runlength : 0;
    g = CreateGadget( INTEGER_KIND, g, &ng, GTIN_Number, val, GTIN_MaxChars, 10, TAG_DONE );

    Project0Gadgets[ 6 ] = g;

//    ng.ng_LeftEdge        =    offx + 200;  // arrows (half hours)
    ng.ng_TopEdge         =    offy + 90;
    ng.ng_GadgetText      =    (UBYTE *)"Duration of appointment";
    ng.ng_GadgetID        =    GD_Gadget90;

	 val = slots[lastSlot].active ? slots[lastSlot].first->arrows : 0;
    g = CreateGadget( INTEGER_KIND, g, &ng, GTIN_Number, val, GTIN_MaxChars, 10, TAG_DONE );

    Project0Gadgets[ 7 ] = g;

    ng.ng_TopEdge         =    offy + 105;
    ng.ng_GadgetText      =    (UBYTE *)"Repeat";
    ng.ng_GadgetID        =    GD_Gadget80;

	 val = slots[lastSlot].active ? slots[lastSlot].first->repeat : 0;
    g = CreateGadget( INTEGER_KIND, g, &ng, GTIN_Number, val, GTIN_MaxChars, 10, TAG_DONE );

    Project0Gadgets[ 8 ] = g;

//    ng.ng_LeftEdge        =    offx + 81;
    ng.ng_TopEdge         =    offy + 120;
    ng.ng_GadgetText      =    (UBYTE *)"Remind";  // warning time
    ng.ng_GadgetID        =    GD_Gadget70;

	 val = slots[lastSlot].active ? slots[lastSlot].first->warn : 10;
    g = CreateGadget( INTEGER_KIND, g, &ng, GTIN_Number, val, GTIN_MaxChars, 10, TAG_DONE );

    Project0Gadgets[ 9 ] = g;

    if ( ! g )
        return( 2L );

    if ( ! ( Project0Wnd = OpenWindowTags( NULL,
                    WA_Left,          Project0Left,
                    WA_Top,           Project0Top,
                    WA_Width,         Project0Width,
                    WA_Height,        Project0Height + offy,
                    WA_IDCMP,         IDCMP_VANILLAKEY|CHECKBOXIDCMP|INTEGERIDCMP|IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW,
                    WA_Flags,         WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH,
                    WA_Gadgets,       Project0GList,
                    WA_Title,         Project0Wdt,
                    WA_ScreenTitle,   "ACalentool",
//                     WA_PubScreen,     Scr,
                    TAG_DONE )))
        return( 4L );

    GT_RefreshWindow( Project0Wnd, NULL );

    offx = Project0Wnd->BorderLeft;
    offy = Project0Wnd->BorderTop;

    PrintIText( Project0Wnd->RPort, Project0IText, offx, offy );

    return( 0L );
  }

void CloseProject0Window(void)
{
    if ( Project0Wnd        ) {
        CloseWindow( Project0Wnd );
        Project0Wnd = NULL;
    }

    if ( Project0GList      ) {
        FreeGadgets( Project0GList );
        Project0GList = NULL;
    }
}

int edit_appt(int actSlot)
  {
	 int                 xFlag = 1;
	 register long       i;
    struct IntuiMessage *Msg;
    unsigned            class, code, id;
    APTR                iadr;

	 if (OpenEditWindow())
		 return -1;

    while (xFlag)
		{
		  WaitPort(Project0Wnd->UserPort);
		  while (Msg = GT_GetIMsg(Project0Wnd->UserPort))
			 {
				class= Msg->Class;
				code = Msg->Code;
				iadr = Msg->IAddress;
   			                      
   			switch (class)
   			  {
				    case IDCMP_REFRESHWINDOW:
   			        GT_BeginRefresh(Project0Wnd);
   			        GT_EndRefresh(Project0Wnd, TRUE);
   			       break;
			 
		    	    case IDCMP_GADGETUP:  
                  id = ((struct Gadget *)iadr)->GadgetID;
                  switch (id)
                    {
                      case GD_Gadget00:   ey = !ey;   break;
                      case GD_Gadget10:   em = !em;   break;
                      case GD_Gadget20:   ed = !ed;   break;
                      case GD_Gadget30:   ew = !ew;   break;
                      case GD_Gadget40:   emf= !emf;  break;
                    }
			         break;
			    	 case IDCMP_CLOSEWINDOW:  
			            xFlag = FALSE;
			 	        break;
                case IDCMP_VANILLAKEY:
                   switch ((char)code)
                     {
                       case 'M':
                       case 'm':
                          em = !em;
                          GT_SetGadgetAttrs(Project0Gadgets[GD_Gadget10], Project0Wnd,
                                             NULL, GTCB_Checked, em, TAG_DONE);
                          break;
                       case 'W':
                       case 'w':
                          ew = !ew;
                          GT_SetGadgetAttrs(Project0Gadgets[GD_Gadget30], Project0Wnd,
                                             NULL, GTCB_Checked, ew, TAG_DONE);
                          break;
                       case 'Y':
                       case 'y':
                          ey = !ey;
                          GT_SetGadgetAttrs(Project0Gadgets[GD_Gadget00], Project0Wnd,
                                             NULL, GTCB_Checked, ey, TAG_DONE);
                          break;
                       case 'D':
                       case 'd':
                          ed = !ed;
                          GT_SetGadgetAttrs(Project0Gadgets[GD_Gadget20], Project0Wnd,
                                             NULL, GTCB_Checked, ed, TAG_DONE);
                          break;
                       case 'F':
                       case 'f':
                          emf = !emf;
                          GT_SetGadgetAttrs(Project0Gadgets[GD_Gadget40], Project0Wnd,
                                             NULL, GTCB_Checked, emf, TAG_DONE);
                          break;
                     }

               }			    
           	GT_ReplyIMsg(Msg);
		     }
		 }
	 /* first read the selected data */
	if (slots[actSlot].first == NULL)
    {
		slots[actSlot].first = (struct appt_entry *)calloc(sizeof(struct appt_entry), 1);
		if (slots[actSlot].first == NULL)
		 {
			fprintf("ACalentool: out of mem\n");
			CloseACalentoolWindow();
			CloseDownScreen();
			exit(5);
		 }
	   slots[actSlot].active = TRUE;
		slots[actSlot].cur_appt = slots[actSlot].first;
		fillAppt(actSlot, NULL);
    }
	 slots[actSlot].first->warn      = atoi(((struct StringInfo *)Project0Gadgets[GD_Gadget90]->SpecialInfo)->Buffer);
	 slots[actSlot].first->repeat    = atoi(((struct StringInfo *)Project0Gadgets[GD_Gadget80]->SpecialInfo)->Buffer);
	 slots[actSlot].first->lookahead = atoi(((struct StringInfo *)Project0Gadgets[GD_Gadget50]->SpecialInfo)->Buffer);
	 slots[actSlot].first->sindex    = 0; // atoi(((struct StringInfo *)Project0Gadgets[GD_Gadgetxx]->SpecialInfo)->Buffer);
	 slots[actSlot].first->runlength = atoi(((struct StringInfo *)Project0Gadgets[GD_Gadget60]->SpecialInfo)->Buffer);
	 slots[actSlot].first->arrows    = atoi(((struct StringInfo *)Project0Gadgets[GD_Gadget70]->SpecialInfo)->Buffer);
//    printf("Arrows: %d\n", slots[actSlot].first->arrows);
//    printf("repeat: %d\n", slots[actSlot].first->repeat);
//    printf("lookah: %d\n", slots[actSlot].first->lookahead);
//    printf("sindex: %d\n", slots[actSlot].first->sindex);
//    printf("runlen: %d\n", slots[actSlot].first->runlength);
//    printf("warn  : %d\n", slots[actSlot].first->warn);

//	 printf("Testflags: gad: Y%d, M%d, D%d, W%d, MF%d\n", ey, em, ed, ew, emf);
    i = 0;
//	 if (ew)                    /* week uses the same bits! */
//		 i |= ALL_WEEKS;
    if (em)
       i |= ALL_MONTHS;
    if (ey)
       i |= ALL_YEARS;
    if (ed)
       i |= ALL_DAYS;
    if (emf)
       i |= EVERY_MON_FRI;

	 slots[actSlot].first->flags     = i;
//	 for(i = GD_Gadget00; i <= 4; i++)
//       printf("gadget: %d, val: %d\n", i, *((struct BoolInfo *)Project0Gadgets[i]->SpecialInfo)->Mask);
    CloseProject0Window();
    return 0;	
  }

