#include "filedoor_structs_defines.h"
#include "filedoor_protos.h"

#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <proto/dos.h>
#include <proto/exec.h>

char * __asm formatfile(register __a0 struct File *,register __a1 char *datostreng);

/* Generer en filliste over samtlige filer i dooren til en user */
void GenFileList(BOOL amigaguide)
{
struct Areamem *area;
struct Filemem *file;
struct DateTime dt;
static __far char tempstr[40];
static __far char tempstr2[40];
static __far char tempstr3[40];
static __far char tempstr4[100];
char *tempptr;
BPTR handle;

  msg("\xc""Hang on... generating filelist.\n");
  /* Initialiser date-stamp + datetime struktur til dato visning */
  dt.dat_Stamp.ds_Minute=0;
  dt.dat_Stamp.ds_Tick=0;
  dt.dat_Format=FORMAT_DOS;
  dt.dat_Flags=0;
  dt.dat_StrDay=NULL;
  dt.dat_StrDate=tempstr;
  dt.dat_StrTime=NULL;
  strcpy(tempstr2,"cydaltemp:");
  if(stricmp(superarea,"hdfiles")) strcat(tempstr2,superarea); else strcat(tempstr2,"MicroLst");
  strcpy(tempstr3,tempstr2);
  if(!amigaguide) strcat(tempstr2,".txt"); else strcat(tempstr2,".guide");
  strcat(tempstr3,".lha");
  if(handle=Open(tempstr2,MODE_NEWFILE))
  {
    if(amigaguide)
    {
      FPuts(handle,"@DATABASE \"Microfiles\"\n");
      FPuts(handle,"@NODE MAIN \"Microfiles\"\n");
    }
    if(amigaguide)
    {
      FPuts(handle,"                                Nisse's MiCrO\n");
    }
    else
    {
      FPuts(handle,"                                                              ___\n");
      FPuts(handle,"      ____      ____   ____   _______   _______   ________   / _/\\ _______\n");
      FPuts(handle,"     /    \\    /   /\\ /   /\\ /   ___/\\ /   ___/\\ /   ____/\\ /_/ _//   ___/\\ \n");
      FPuts(handle,"    /      \\  /   / //   / //   /___\\//   /___\\//   /____\\/// _/ /   /___\\/ \n");
      FPuts(handle,"   /   /\\   \\/   / //   / / \\__  \\    \\__  \\   /   ___/\\   \\\\/   \\__  \\ \n");
      FPuts(handle,"  /   / /\\      / //   / /____/  /\\ ____/  /\\ /   /___\\/       ____/  /\\ \n");
      FPuts(handle," /___/ /  \\____/ //___/ //______/ //______/ //_______/\\       /______/ / \n");
      FPuts(handle," \\___\\/    \\___\\/ \\___\\/ \\______\\/ \\______\\/ \\_______\\/       \\______\\/ \n");
      FPuts(handle,"    ________    ____   ____   __________   __________    __________\n");
      FPuts(handle,"   /_______/\\  /___/\\ /___/\\ /_________/\\ /_________/\\  /_________/\\ \n");
      FPuts(handle,"   \\       \\ \\/    \\ \\\\   \\ \\\\    __   \\ \\\\    __   \\|_ \\    __   \\ \\ \n");
      FPuts(handle,"    \\    _  \\/      \\ \\\\   \\ \\\\   \\ \\___\\/_\\   \\_\\  |_ \\ \\   \\ \\   \\ \\ \n");
      FPuts(handle,"     \\   \\\\_____/\\   \\ \\\\   \\ \\\\   \\  /___/\\\\    __   \\ \\ \\   \\ \\   \\ \\ \n");
      FPuts(handle,"      \\   \\ \\     \\   \\ \\\\   \\ \\\\   \\_\\   \\ \\\\   \\ \\   \\ \\ \\   \\_\\   \\ \\ \n");
      FPuts(handle,"       \\___\\/      \\___\\/ \\___\\/ \\_________\\/ \\___\\ \\___\\/  \\_________\\/ \n");
      FPuts(handle,"\n");
    }
    FPuts(handle,"                                     -=-\n");
    FPuts(handle,"                                  -= BBS =-\n");
    FPuts(handle,"                               -=           =-\n");
    FPuts(handle,"                            -=  USR-28K8 24HRS  =-\n");
    FPuts(handle,"                        -=                          =-\n");
    FPuts(handle,"                    -=      CALL (+45) 42 39 31 84      =-\n");
    FPuts(handle,"                       --------------------------------\n");
    /* Gennemløb samtlige areas og lav indholdsfortegnelse */
    if(amigaguide)
    {
      area=firstarea;
      while(area!=NULL)
      {
        FindFiles(area);
        if(area->files.mlh_Head->mln_Succ!=NULL && !CheckAreaTag(area))
        {
          FPrintf(handle,"                       @{\" %-30.30s\" LINK %s}\n",area->area.name,area->area.filename);
        }
        area=area->next;
      }
    }
    if(amigaguide) FPuts(handle,"@ENDNODE\n\n");
    /* Gennemløb samtlige areas */
    area=firstarea;
    while(area!=NULL)
    {
      FindFiles(area);
      if(area->files.mlh_Head->mln_Succ!=NULL && !CheckAreaTag(area))
      {
        if(amigaguide)
        {
          FPrintf(handle,"\n@NODE \"%s\" \"%s\"\n",area->area.filename,area->area.name);
        }
        else
        {
          FPuts(handle,"\n-------------------------------------------------------------------------------\n");
          FPuts(handle,"                  Area : ");
          FPuts(handle,area->area.name);
          FPuts(handle,"\n-------------------------------------------------------------------------------\n");
        }
        file=(struct Filemem *)area->files.mlh_Head;
        while(file->n.mln_Succ!=NULL)
        {
          /* Fix datoen -> streng */
          dt.dat_Stamp.ds_Days=file->f.date;
          DateToStr(&dt);
          /* Hitit */
          FPuts(handle,formatfile(&(file->f),tempstr));
          file=(struct Filemem *)file->n.mln_Succ;
        }
      }
      if(amigaguide) FPuts(handle,"\n@ENDNODE\n");
      PurgeFiles(area);
      area=area->next;
    }
    /* geninitialiser rigtige area */
    FindFiles(activearea);
    topfile=(struct Filemem *)activearea->files.mlh_Head;
    current=0;
    Close(handle);
    DeleteFile(tempstr3);
    sprintf(tempstr4,"lha a %s %s",tempstr3,tempstr2);
    Execute(tempstr4,NULL,NULL);
    tempptr=&tempstr3[0];
    DoXprDl(1,&tempptr);
    DeleteFile(tempstr2);
    DeleteFile(tempstr3);
  }
}

/* Lad brugeren vælge hvilke areas der skal i fillisten */
void FileList(void)
{
struct Areamem *currentarea;
struct Areamem **list;
int i;
int oldareanum;
int toparea;
int cursorline;
int cursorside;
char key[4];
BOOL refreshtop=TRUE;
BOOL refreshbottom=TRUE;
BOOL done=FALSE;;

  msg("\xc");
  PurgeFiles(activearea);
  if(list=AllocVec(numareas*sizeof(struct Areamem *),MEMF_ANY))
  {
    /* Find samtlige areas og sorter dem */
    currentarea=firstarea;
    for(i=0;i<numareas;i++)
    {
      list[i]=currentarea;
      currentarea=currentarea->next;
    }
    qsort(list,numareas,sizeof(struct Areamem *),Compareareas);
    /* Find ud af at placere cursor oveni gamle area */
    for(i=0;i<numareas;i++)
    {
      if(list[i]==activearea) oldareanum=i;
    }
    toparea=oldareanum/(visiblelines*2)*visiblelines*2;
    if(oldareanum>=toparea+visiblelines)
    {
      cursorside=1;
    }
    else
    {
      cursorside=0;
    }
    cursorline=oldareanum-cursorside*visiblelines-toparea;
    while(!done)
    {
      if(hetwittedus) break;
      if(refreshtop)
      {
        ClearMenuWindow("Filelist menu");
        msg("\n");
        msg(KNAP RI"<RETURN>"MENU" Include/exclude area under cursor from filelist\n");
        msg(KNAP RI"     (G)"MENU" Download filelist in Amigaguide format\n");
        msg(KNAP RI"     (T)"MENU" Download filelist in ASCII-text format\n");
        msg(KNAP RI"     (Q)"MENU" Quit to previous menu\n");
        refreshtop=FALSE;
      }
      if(refreshbottom)
      {
        ClearBottomWindow();
        if(toparea)
          msgc("(MORE AREAS ABOVE)",FG1,FG7);
        else
          msgc("",FG1,FG7);
        for(i=toparea;i<toparea+visiblelines && i<numareas;i++)
        {
          msg("\n"RI" ");
          if(CheckAreaTag(list[i])) msg(FG1" "); else msg(FG3"*");
          msg(list[i]->area.name);
        }
        MoveCrsr(1,TOPLINE);
        for(;i<toparea+visiblelines*2 && i<numareas;i++)
        {
          msg(ESC"[40C");
          if(CheckAreaTag(list[i])) msg(FG1" "); else msg(FG3"*");
          msg(list[i]->area.name);
          msg("\n");
        }
        MoveCrsr(1,TOPLINE+visiblelines);
        if(i<numareas)
          msgc("(MORE AREAS BELOW)",FG1,FG7);
        else
          msgc("",FG1,FG7);
        refreshbottom=FALSE;
      }
      PutCursorXY(cursorside?40:2,cursorline);
      hotkey("",key);
      if(key[0]=='\x1b')
      {
        hotkey("",key);
        if(key[0]=='[')
        hotkey("",key);
        while(key[0]>='0' && key[0]<='9')
        {
          hotkey("",key);
          if(hetwittedus) break;
        }
      }
      switch(key[0])
      {

        /* Return */
        case '\xd':
        ToggleAreaTag(list[cursorline+toparea+cursorside*visiblelines]);
        MoveCrsr(cursorside?41:3,cursorline+TOPLINE);
        if(CheckAreaTag(list[cursorline+toparea+cursorside*visiblelines]))
          msg(FG1" ");
        else
          msg(FG3"*");
        msg(list[cursorline+toparea+cursorside*visiblelines]->area.name);
        break;

        /* Quit tilbage */
        case 'Q':
        case 'q':
        done=TRUE;
        break;
        
        /* crsr venstre */
        case 'D':
        case '4':
        ZapCursorXY(cursorside?40:2,cursorline);
        cursorside=0;
        break;

        /* crsr højre */
        case 'C':
        case '6':
        ZapCursorXY(cursorside?40:2,cursorline);
        if(numareas>toparea+visiblelines)
        {
          cursorside=1;
          if(cursorline+visiblelines+toparea>=numareas)
            cursorline=numareas-toparea-visiblelines-1;
        }
        break;

        /* crsr op */
        case 'A':
        case '8':
        ZapCursorXY(cursorside?40:2,cursorline);
        if(cursorline==0)
        { /* Side op */
          if(toparea!=0)
          {
            toparea-=visiblelines*2;
            cursorline=visiblelines-1;
            refreshbottom=TRUE;
          }
        }
        else
        { /* Crsr en op */
          cursorline--;
          msg(ESC"[1A");
        }
        break;

        /* side op */
        case '9':
        ZapCursorXY(cursorside?40:2,cursorline);
        if(toparea!=0)
        {
          toparea-=visiblelines*2;
          cursorline=visiblelines-1;
          refreshbottom=TRUE;
        }
        break;

        /* crsr ned */
        case 'B':
        case '2':
        ZapCursorXY(cursorside?40:2,cursorline);
        if(cursorline==visiblelines-1)
        { /* Side ned */
          if(toparea+visiblelines*2<numareas)
          {
            toparea+=visiblelines*2;
            if(visiblelines+toparea>=numareas) cursorside=0;
            cursorline=0;
            refreshbottom=TRUE;
          }
        }
        else
        { /* Crsr en ned */
          if(toparea+cursorside*visiblelines+cursorline+1<numareas)
          {
            cursorline++;
            msg(ESC"[1B");
          }
        }
        break;

        /* side ned */
        case '3':
        ZapCursorXY(cursorside?40:2,cursorline);
        if(toparea+visiblelines*2<numareas)
        {
          toparea+=visiblelines*2;
          if(visiblelines+toparea>=numareas) cursorside=0;
          cursorline=0;
          refreshbottom=TRUE;
        }
        break;

        /* Refresh */
        case ' ':
        refreshbottom=refreshtop=TRUE;
        break;

        /* Amigaguide */
        case 'G':
        case 'g':
        GenFileList(TRUE);
        refreshbottom=refreshtop=TRUE;
        break;

        /* Tekstfil */
        case 'T':
        case 't':
        GenFileList(FALSE);
        refreshbottom=refreshtop=TRUE;
        break;

        /* En tast vi ikke kan bruge til noget */
        default:
        break;
      }
    }
    FreeVec(list);
  }
}
