#include "filedoor_structs_defines.h"
#include "filedoor_protos.h"
#include <proto/exec.h>
#include <proto/dos.h>
#include <exec/memory.h>
#include <utility/tagitem.h>
#include <strings.h>
#include <stdio.h>
#include <ctype.h>

int current=0;
struct Filemem *topfile;

/* Lad brugeren markere nogle filer */
void MainLoop(void)
{
struct Filemem *temp;
int filesonscreen;
BOOL done=FALSE;
BOOL refreshmenu=FALSE;
BOOL refreshfiles=FALSE;
BOOL refreshtotal=TRUE;
static __far char key[10];
static __far char tempstr[200];
static __far char tempstr2[200];
BPTR handle;

  FindFiles(activearea);
  topfile=(struct Filemem *)activearea->files.mlh_Head;
  while(activearea->next!=NULL && topfile->n.mln_Succ==NULL)
  {
    activearea=activearea->next;
    FindFiles(activearea);
    topfile=(struct Filemem *)activearea->files.mlh_Head;
  }
  if(topfile->n.mln_Succ!=NULL)
  {
    while(!done)
    {
      /* Genopfrisk menuerne */
      if(refreshmenu)
      {
        MenuFileTag();
        refreshmenu=FALSE;
      }
      /* Genopfrisk filerne */
      if(refreshfiles)
      {
        filesonscreen=PrintFiles(activearea,topfile);
        refreshfiles=FALSE;
      }
      /* Genopfrisk alting! Tegner også rammen udenom filerne, ikke kun selve filerne */
      if(refreshtotal)
      {
        msg("\xc");
        MenuFileTag();
        ClearBottomWindow();
        filesonscreen=PrintFiles(activearea,topfile);
        refreshtotal=FALSE;
      }
      PutCursor(current);
      hotkey("",key);
      if(hetwittedus) break;
      switch(key[0])
      {
        /** Crsr op/ned **/
        case 0x1b:
        hotkey("",key);
        if(key[0]=='[')
        {
          hotkey("",key);
          while(key[0]>='0' && key[0]<='9') hotkey("",key);
          switch(key[0])
          {
            case 'A': /* ansi crsr up */
            ZapCursor(current);
            if(current==0)
            {
              temp=MoveBack(topfile,visiblelines);
              if(temp->n.mln_Pred!=NULL)
              {
                /* Side op */
                topfile=temp;
                current=visiblelines-1;
                refreshfiles=TRUE;
              }
            }
            else current--;
            break;
            case 'B': /* ansi crsr down */
            ZapCursor(current);
            if(current==visiblelines-1)
            {
              temp=MoveForward(topfile,visiblelines);
              if(temp->n.mln_Succ!=NULL)
              {
                /* Side ned */
                topfile=temp;
                current=0;
                refreshfiles=TRUE;
              }
            }
            else if(current+1!=filesonscreen) current++;
            break;
            default:
            break;
          }
        }
        break;

        /* Numeric keypad cursor up */
        case '8':
        ZapCursor(current);
        if(current==0)
        {
          temp=MoveBack(topfile,visiblelines);
          if(temp->n.mln_Pred!=NULL)
          {
            /* Side op */
            topfile=temp;
            current=visiblelines-1;
            refreshfiles=TRUE;
          }
        }
        else current--;
        break;

        /* Numeric keypad cursor down */
        case '2':
        ZapCursor(current);
        if(current==visiblelines-1)
        {
          temp=MoveForward(topfile,visiblelines);
          if(temp->n.mln_Succ!=NULL)
          {
            /* Side ned */
            topfile=temp;
            current=0;
            refreshfiles=TRUE;
          }
        }
        else if(current+1!=filesonscreen) current++;
        break;

        /** Marker/umarker en fil **/
        case '\xd':
        temp=MoveForward(topfile,current);
        if(ToggleTag(activearea,temp))
        {
          msg(FG3" *"FG7);
        }
        else
        {
          msg("  ");
        }
        break;

        /** Vis lidt hjælp **/
        case 'H':
        case 'h':
        GeneralHelp();
        refreshtotal=TRUE;
        break;

        /** Gå til forrige side **/
        case '9':
        temp=MoveBack(topfile,visiblelines);
        if(temp->n.mln_Pred!=NULL)
        {
          ZapCursor(current);
          topfile=temp;
          current=0;
          refreshfiles=TRUE;
        }
        break;

        /** Gå til næste side **/
        case '3':
        temp=MoveForward(topfile,visiblelines);
        if(temp->n.mln_Succ!=NULL)
        {
          ZapCursor(current);
          topfile=temp;
          current=0;
          refreshfiles=TRUE;
        }
        break;

        /** Vis/skjul korte beskrivelser **/
        case 'S':
        case 's':
        showshort=!showshort;
        refreshfiles=TRUE;
        break;

        /** Vis lang beskrivelse for fil under markør **/
        case 'L':
        case 'l':
        msg("\xc"FG7);
        temp=MoveForward(topfile,current);
        switch(temp->f.flags&(FILEFLAG_LONGFILE|FILEFLAG_LONGARCHIVE))
        {
        
          /* Lang beskrivelse i separat fil */
          case FILEFLAG_LONGFILE:
          CopyFile(temp->f.longdesc,"ram:tempfile");
          showfile("ram:tempfile");
          DeleteFile("ram:tempfile");
          break;
          /* Lang beskrivelse inden i arkiv */
          case FILEFLAG_LONGARCHIVE:
          strcpy(tempstr,activearea->area.path);
          if(!AddPart(tempstr,temp->f.name,200)) break;
          strcpy(tempstr2,"echo \"Unknown file extension.. No can do.\"");
          if(!stricmp(".lzx",temp->f.name+strlen(temp->f.name)-4))
            sprintf(tempstr2,"lzx -q p %s %s",tempstr,temp->f.longdesc);
          if(!stricmp(".lha",temp->f.name+strlen(temp->f.name)-4))
            sprintf(tempstr2,"lha -q p %s %s",tempstr,temp->f.longdesc);
          if(!stricmp(".lzh",temp->f.name+strlen(temp->f.name)-4))
            sprintf(tempstr2,"lha -q p %s %s",tempstr,temp->f.longdesc);
          if(!stricmp(".dms",temp->f.name+strlen(temp->f.name)-4))
            sprintf(tempstr2,"dms viewdiz %s",tempstr);
          if(handle=Open("ram:tempfile",MODE_NEWFILE))
          {
            Execute(tempstr2,NULL,handle);
            Close(handle);
            showfile("ram:tempfile");
            DeleteFile("ram:tempfile");
          }
          break;
          /* Ingen lang beskrivelse */
          default:
          msg("No long description exists for this file\n");
          break;
        }
        AnyKey();
        refreshtotal=TRUE;
        break;

        /** Vis indhold af arkiv **/
        case 'V':
        case 'v':
        msg(FG7"\xc""Processing..\n");
        temp=MoveForward(topfile,current);
        strcpy(tempstr,activearea->area.path);
        if(!AddPart(tempstr,temp->f.name,200)) break;
        strcpy(tempstr2,"echo \"Unknown file extension.. No can do.\"");
        if(!stricmp(".lzx",temp->f.name+strlen(temp->f.name)-4)) sprintf(tempstr2,"lzx -q l %s",tempstr);
        if(!stricmp(".lha",temp->f.name+strlen(temp->f.name)-4)) sprintf(tempstr2,"lha -q v %s",tempstr);
        if(!stricmp(".lzh",temp->f.name+strlen(temp->f.name)-4)) sprintf(tempstr2,"lha -q v %s",tempstr);
        if(!stricmp(".arj",temp->f.name+strlen(temp->f.name)-4)) sprintf(tempstr2,"unarj %s",tempstr);
        if(handle=Open("ram:tempfile",MODE_NEWFILE))
        {
          Execute(tempstr2,NULL,handle);
          Close(handle);
          showfile("ram:tempfile");
          DeleteFile("ram:tempfile");
          AnyKey();
        }
        refreshtotal=TRUE;
        break;


        /* Lad en bruger få luft for sin mening om en fil */
        case 'M':
        case 'm':
        ClearMenuWindow("Quality evaluation of file");
        temp=MoveForward(topfile,current);
        if(accesslevel<5000 && temp->f.flags&FILEFLAG_QUALITYLOCKED)
        {
          msg("\n"RI"The (co)sysop has decided the quality of this file. If you disagree and dare\n");
          msg(RI"question the sysop's judgement you should page him/her. Have a nice day.");
          AnyKey();
        }
        else
        {
          msg("\n"RI"Rate the file you selected -->"FG2);
          msg(temp->f.name);
          msg(FG7"<--\n"RI"Using 0-5 where 0 is worst, 5 is best and 3 is average.\n");
          if(accesslevel>=5000) msg(RI"Since you're the sysop your decision is final!\n");
          hotkey(RI"So, what will it be? ",tempstr);
          if(tempstr[0]>='0' && tempstr[0]<='5')
          {
            temp->f.qual=tempstr[0]-'0';
            if(accesslevel>=5000) temp->f.flags|=FILEFLAG_QUALITYLOCKED;
            activearea->dirty=TRUE;
          }
        }
        refreshfiles=refreshmenu=TRUE;
        break;

        /** Søg, fido, søg **/
        case 'F':
        case 'f':
        ZapCursor(current);
        if(Search(FALSE)) refreshfiles=refreshmenu=TRUE; else refreshtotal=TRUE;
        break;

        /** Søg mere, fido, søg mere **/
        case 'R':
        case 'r':
        ZapCursor(current);
        if(Search(TRUE)) refreshfiles=refreshmenu=TRUE; else refreshtotal=TRUE;
        break;

        /** Download de taggede filer **/
        case 'D':
        case 'd':
        Download();
        FindFiles(activearea);
        topfile=(struct Filemem *)activearea->files.mlh_Head;
        current=0;
        refreshtotal=TRUE;
        break;

        /** Upload filer **/
/*        case 'U':
        case 'u': 
        if(!stricmp(superarea,"hdfiles"))
        {
          Upload();
          FindFiles(activearea);
          topfile=(struct Filemem *)activearea->files.mlh_Head;
          current=0;
          refreshtotal=TRUE;
        }
        break;
*/

        /** Lav en filliste over cdrommen **/
        case 'G':
        case 'g':
        FileList();
        FindFiles(activearea);
        topfile=(struct Filemem *)activearea->files.mlh_Head;
        current=0;
        refreshtotal=TRUE;
        break;

        /** Vælg et nyt area **/
        case 'O':
        case 'o':
        do
        {
          Area();
          topfile=(struct Filemem *)activearea->files.mlh_Head;
        } while(topfile->n.mln_Succ==NULL); /* Tomme areas er væmmelige */
        current=0;
        refreshtotal=TRUE;
        break;

        /** Zap nogle af de taggede filer **/
        case 'Z':
        case 'z':
        if(taggedfiles)
        {
          ZapTagged();
          refreshtotal=TRUE;
        }
        break;

        /** Gå ind i et arkiv **/
        case 'E':
        case 'e':
        msg("\xc");
        temp=MoveForward(topfile,current);
        strcpy(tempstr,activearea->area.path);
        if(!AddPart(tempstr,temp->f.name,200)) break;
        Archive(tempstr);
        refreshtotal=TRUE;
        break;

        /** Byebye **/
        case 'Q':
        case 'q':
        if(taggedfiles)
        {
          ClearMenuWindow("Exit filedoor");
          msg("\n"MENU RI"You still have some tagged files!\n");
          hotkey(RI"Are you sure you want to quit? "KNAP"(Y/n)"MENU,tempstr);
          if(toupper(tempstr[0])!='N') done=TRUE;
          refreshmenu=TRUE;
        }
        else
          done=TRUE;
        break;
        
        /** User prefs **/
        case 'P':
        case 'p':
        SetUserPrefs();
        FindFiles(activearea);
        topfile=(struct Filemem *)activearea->files.mlh_Head;
        current=0;
        refreshtotal=TRUE;
        break;

        /** Refresh **/
        case ' ':
        refreshtotal=TRUE;
        break;

        /** Bogus **/
        default:
        break;
      }
      if(hetwittedus) break;
    }
    PurgeFiles(activearea);
  }
}
