/*->c.mess */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>
#include <stdarg.h> 

#include "h.os"
#include "h.wimp"
#include "h.bbc"
#include "h.flex"
#include "h.swis"

#include "h.Drawlevel0"

#include "h.def"

#include "h.wos"
#include "h.main"
#include "h.mym"
#include "h.fsx"
#include "h.timex"

#include "h.strdef"
#include "h.xext"


#include "h.buffer"
#include "h.state"
#include "h.dir"
#include "h.txfax"
#include "h.view"
#include "h.batch"
#include "h.trans"
#include "h.config"
#include "h.sched"
#include "h.log"
#include "h.tw"
#include "h.key"
#include "h.newfax"
#include "h.file"


#include "h.mess"




static void menumeratebatch(void)
{
 int    batch;
 int    n;
 char * p;
 int    id;
 int    status;
 char   temp[4];

 batch=wimpevent.data.msg.data.words[1];
 n=wimpevent.data.msg.data.words[2];

 if(n>=0 && n<vnofiles[batch])
 {
  p=vtable[batch][n].name;
  id=vtable[batch][n].schedule;
  status=vtable[batch][n].new;
 }
 else
 {
  id=0;
  p=temp;
  *p=0;
  status=0;
 }

  /* name, ID, status */

 messageout(BatchEntryInfo,wimpevent.data.msg.hdr.my_ref,p,id,status);
}



static void menumerateschedule(void)
{
 int  schedule;
 int  n;
 char name[64];
 int  status;

 schedule=wimpevent.data.msg.data.words[1];
 n=wimpevent.data.msg.data.words[2];

 if(!getscheduleinfo(schedule,n,name,&status))
 {
  *name=0;
  status=0;
 }

 messageout(ScheduleEntryInfo,wimpevent.data.msg.hdr.my_ref,name,status);
}



static void mdeletescheduleentry(void)
{
 int schedule;
 int n;

 schedule=wimpevent.data.msg.data.words[1];
 n=wimpevent.data.msg.data.words[2];

 delselectionn(schedule,n);
}



static void maddscheduleentry(void)
{
 int schedule;
 char * p;

 schedule=wimpevent.data.msg.data.words[1];
 p=(char*)&wimpevent.data.msg.data.words[2];

 decodeaddnamenumber(p,schedule,&defnewbits);

 closesched(schedule);
}



static void mdeletefax(void)
{
 int batch;
 int n;

 batch=wimpevent.data.msg.data.words[1];
 n=wimpevent.data.msg.data.words[2];

 view=batch;
 deleteentry(n,1);
}



static void maddfax(void) /* MGR */
{
 int      batch;
 int      code;
 char   * name;
 fstat    f;


 batch=wimpevent.data.msg.data.words[1];
 name=(char *)&wimpevent.data.msg.data.words[2];
 stat(name,&f);

 if(batch==TXBFILE)
 {
  view=batch;
  code=batchxload(f.type,name,0);
  messageout(NewFax,0,TXBFILE,code-1,vtable[view][code-1].schedule,
               vtable[view][code-1].name); /* MGR */
 }
 else
 {
  addtorxbatch(TIFF,name,0,0);
 }
}



static void mrequeststatus(void)
{
 messageout(ArcFaxStatus,wimpevent.data.msg.hdr.my_ref,activestate());
}


void messinit(void)
{
 messageout(ArcFaxStatus,0,activestate());
}


void messagein(void)
{
 switch(wimpevent.data.msg.data.words[0])
 {

              case FaxSent:
                           break;

            case FaxFailed:
                           break;

       case EnumerateBatch:
                           menumeratebatch();
                           break;


       case BatchEntryInfo:
                           break;


    case EnumerateSchedule:
                           menumerateschedule();
                           break;

    case ScheduleEntryInfo:
                           break;

  case DeleteScheduleEntry:
                           mdeletescheduleentry();
                           break;

     case AddScheduleEntry:
                           maddscheduleentry();
                           break;

           case  DeleteFax:
                           mdeletefax();
                           break;

               case NewFax:
                           break;

         case ArcFaxStatus:
                           break;

  case ArcFaxRequestStatus:
                           mrequeststatus();
                           break;

               case AddFax: /* MGR */
                           maddfax();
                           break;

 }
}





void messageout(int type,int ref,...)
{
 wimp_msgstr msg;
 int         len;
 va_list     args;
 char      * p;

 va_start(args,ref);


 len=0;

 switch(type)
 {
              case FaxSent: /* ID, entry */
            case FaxFailed:
                           msg.data.words[1]=(int)(va_arg(args,int));
                           msg.data.words[2]=(int)(va_arg(args,int));
                           len=8;
                           break;

       case BatchEntryInfo: /* name, ID, status */
                           p=(char*)(va_arg(args,int));
                           strcpy((char*)&msg.data.words[1],p);
                           msg.data.words[4]=(int)(va_arg(args,int));
                           msg.data.words[5]=(int)(va_arg(args,int));
                           len=12+8;
                           break;


    case ScheduleEntryInfo: /* destination , status */
                           p=(char*)(va_arg(args,int));
                           strcpy((char*)&msg.data.words[1],p);
                           msg.data.words[10]=(int)(va_arg(args,int));
                           len=36+4;
                           break;


               case NewFax: /* batch, index, ID */
                           msg.data.words[1]=(int)(va_arg(args,int));
                           msg.data.words[2]=(int)(va_arg(args,int));
                           msg.data.words[3]=(int)(va_arg(args,int));
                           p=(char*)(va_arg(args,int)); /* MGR */
                           strcpy((char*)&msg.data.words[4],p);
                           len=12+12;
                           break;

         case ArcFaxStatus:
                           msg.data.words[1]=(int)(va_arg(args,int));
                           len=4;
                           break;

 }

 va_end(args);

 msg.hdr.size=((48+8+len+4) & 0xFFFFFFFC);
 msg.hdr.task=taskhandle;
 msg.hdr.your_ref=ref;
 msg.hdr.action=SPARKMESS;
 msg.data.words[0]=type;

 wimp_sendmessage(17,&msg,0);
}
