#include "project.h"



extern char szFavorite[];

static void
set (char *to)
{
  FormPtr formptr;
  FieldPtr fieldptr;
  UInt16 index;
  VoidHand mh;
  VoidPtr p;
  int l;

  if (!to)
    return;
  l = StrLen (to);
  l++;


  formptr = FrmGetFormPtr (FavForm);
  if (!formptr)

    formptr = FrmGetFormPtr (FavForm);
  if (!formptr)
    return;

  index = FrmGetObjectIndex (formptr, FavField);
  fieldptr = FrmGetObjectPtr (formptr, index);
  if (!fieldptr)
    return;

  mh = (VoidHand) FldGetTextHandle (fieldptr);
  FldSetTextHandle (fieldptr, NULL);
  if (!mh)
    {
      mh = MemHandleNew (l);
    }

  if (MemHandleSize (mh) < l)
    {
      MemHandleResize (mh, l);
    }

  p = MemHandleLock (mh);

  StrCopy (p, to);

  MemHandleUnlock (mh);

  FldSetTextHandle (fieldptr, (Handle) mh);
  FldDrawField (fieldptr);

}

static void
dodb (char *dbname, int inum, RectangleType * bounds)
{
  LocalID lid;
  DmOpenRef oref;
  VoidHand mh;
  char *p;

  lid = DmFindDatabase (0, dbname);

  if (lid == 0)
    {
      DmCreateDatabase (0, dbname, APPID, 'Data', 0);
      lid = DmFindDatabase (0, dbname);
    }

  if (!lid)
    return;

  oref = DmOpenDatabase (0, lid, dmModeReadWrite);
  if (!oref)
    return;

  mh = NULL;

  if (inum < DmNumRecords (oref))
    mh = DmGetRecord (oref, inum);
  if (mh)
    {
      p = MemHandleLock (mh);

      if (bounds)
        {
          WinDrawChars (p, MemHandleSize (mh) - 1, bounds->topLeft.x,
                        bounds->topLeft.y);
        }
      else
        {
          set (p);
        }

      MemHandleUnlock (mh);
      DmReleaseRecord (oref, inum, 0);
    }

  DmCloseDatabase (oref);

}

static void
favdrawfn (Word inum, RectanglePtr bounds, CharPtr * itemstext)
{
  dodb (szFavorite, inum, bounds);
}

int
favnum ()
{

  LocalID lid;
  DmOpenRef oref;
  int i;
  lid = DmFindDatabase (0, szFavorite);

  if (lid == 0)
    {
      DmCreateDatabase (0, szFavorite, APPID, 'Data', 0);
      lid = DmFindDatabase (0, szFavorite);
    }

  if (!lid)
    return;

  oref = DmOpenDatabase (0, lid, dmModeReadWrite);
  if (!oref)
    return;


  i = DmNumRecords (oref);
  DmCloseDatabase (oref);

  return i;

}



Int16
comp (void *r1, void *r2, Int16 other, SortRecordInfoPtr rec1SortInfo,
      SortRecordInfoPtr rec2SortInfo, VoidHand appInfoH)
{
//void *r1=MemHandleLock((VoidHand) rec1);
//void *r2=MemHandleLock((VoidHand) rec2);
  int i = StrCompare (r1, r2);

//MemHandleUnlock(rec2);
//MemHandleUnlock(rec1);

  return i;
}

static void
favremove (void)
{
  FormPtr formptr;
  FieldPtr fieldptr;
  UInt16 index;
  char buf[256], *wptr = buf;
  int i;

  LocalID lid;
  DmOpenRef oref;
  VoidHand mh, ptr;
  char *p, *p2;


  formptr = FrmGetFormPtr (FavForm);
  if (!formptr)
    return;
  index = FrmGetObjectIndex (formptr, FavField);
  fieldptr = FrmGetObjectPtr (formptr, index);
  if (!fieldptr)
    return;

  ptr = (VoidHand) FldGetTextHandle (fieldptr);
  i = FldGetTextLength (fieldptr);

  p2 = MemHandleLock (ptr);
  while (i--)
    {
      if (*p2 != 10)
        {
          *(wptr++) = *p2;
        }
      *p2++;
    }
  *wptr = 0;

  MemHandleUnlock (ptr);

  lid = DmFindDatabase (0, szFavorite);

  if (lid == 0)
    {
      DmCreateDatabase (0, szFavorite, APPID, 'Data', 0);
      lid = DmFindDatabase (0, szFavorite);
    }

  if (!lid)
    return;

  oref = DmOpenDatabase (0, lid, dmModeReadWrite);
  if (!oref)
    return;


  for (i = 0; i < DmNumRecords (oref); ++i)
    {
      mh = DmGetRecord (oref, i);
      if (mh)
        {
          p = MemHandleLock (mh);

          if (!StrCompare (p, buf))
            {
              MemHandleUnlock (mh);
              DmReleaseRecord (oref, i, 0);

              DmRemoveRecord (oref, i);

            }
          else
            {

              MemHandleUnlock (mh);
              DmReleaseRecord (oref, i, 0);

              i++;

            }
        }
      else
        i++;

    }

  DmCloseDatabase (oref);
}

static void
favadd (void)
{
  FormPtr formptr;
  FieldPtr fieldptr;
  UInt16 index;
  LocalID lid;
  DmOpenRef oref;
  VoidHand mh, ptr;
  UInt16 at = 0;
  char *p, *p2;
  char zero = 0;
  int i;


  formptr = FrmGetFormPtr (FavForm);
  if (!formptr)
    return;
  index = FrmGetObjectIndex (formptr, FavField);
  fieldptr = FrmGetObjectPtr (formptr, index);
  if (!fieldptr)
    return;

  ptr = (VoidHand) FldGetTextHandle (fieldptr);
  i = FldGetTextLength (fieldptr);

  lid = DmFindDatabase (0, szFavorite);

  if (lid == 0)
    {
      DmCreateDatabase (0, szFavorite, APPID, 'Data', 0);
      lid = DmFindDatabase (0, szFavorite);
    }

  if (!lid)
    return;

  oref = DmOpenDatabase (0, lid, dmModeReadWrite);
  if (!oref)
    return;

  mh = NULL;

  mh = DmNewRecord (oref, &at, i + 1);

  if (mh)
    {
      p = MemHandleLock (mh);
      if (p)
        {
          p2 = MemHandleLock (ptr);

          DmWrite (p, 0, p2, i);
          MemHandleUnlock (ptr);
          DmWrite (p, i, &zero, 1);
          MemHandleUnlock (mh);
        }
    }
  DmReleaseRecord (oref, at, 0);
  DmCloseDatabase (oref);


  DmQuickSort (oref, comp, 0);

}


void
favorite_dispatch (EventType * e)
{

  switch (e->eType)
    {
    case popSelectEvent:
      {
        FormPtr formptr;
        ControlPtr ctrl;
        UInt16 index;


        formptr = FrmGetFormPtr (FavForm);
        if (!formptr)
          break;

        index = FrmGetObjectIndex (formptr, e->data.popSelect.controlID);
        ctrl = FrmGetObjectPtr (formptr, index);
        if (!ctrl)
          break;

        switch (e->data.popSelect.listID)
          {
          case FavList:
            dodb (szFavorite, e->data.popSelect.selection, NULL);
            break;
          }

        switch (e->data.popSelect.controlID)
          {
          case FavPop:
            // CtlSetLabel (ctrl, "F");
            e->data.popSelect.selection = 0;
            e->data.popSelect.priorSelection = 0;
            break;
          }

//        FrmDrawForm (formptr);

        FrmHandleEvent (FrmGetActiveForm (), e);


      }
      break;
    case frmOpenEvent:
      {
        FormPtr formptr;
        FieldPtr fieldptr;
        ListPtr listptr;
        UInt16 index;
        UInt16 i;


        formptr = FrmGetFormPtr (FavForm);
        if (!formptr)
          break;

        index = FrmGetObjectIndex (formptr, FavField);
        fieldptr = FrmGetObjectPtr (formptr, index);
        if (!fieldptr)
          break;



        index = FrmGetObjectIndex (formptr, FavList);
        listptr = FrmGetObjectPtr (formptr, index);
        if (!listptr)
          break;
        LstSetDrawFunction (listptr, favdrawfn);
        LstSetListChoices (listptr, NULL, favnum ());

      }

      break;
    case ctlSelectEvent:

      switch (e->data.ctlSelect.controlID)
        {
        case FavDone:
          FrmGotoForm (SystemForm);
          break;
        case FavAdd:
          favadd ();
          break;
        case FavRemove:
          favremove ();
          break;
        }
      {
        FormPtr formptr;
        FieldPtr fieldptr;
        ListPtr listptr;
        UInt16 index;
        UInt16 i;


        formptr = FrmGetFormPtr (FavForm);
        if (!formptr)
          break;

        index = FrmGetObjectIndex (formptr, FavField);
        fieldptr = FrmGetObjectPtr (formptr, index);
        if (!fieldptr)
          break;


        index = FrmGetObjectIndex (formptr, FavList);
        listptr = FrmGetObjectPtr (formptr, index);
        if (!listptr)
          break;
        LstSetDrawFunction (listptr, favdrawfn);
        LstSetListChoices (listptr, listnull, favnum ());

      }

      break;


    }

}
