#include "SysUser.h"

#include <link/io.h>
#include <link/file.h>
#include <link/config.h>
#include <link/user.h>

/// Edit_User
int Edit_User(char *name)
{
   long zflag;
   char a;
   char string[80];
   char valid [80];
   char filename[128];

   int           oldlevel;
   int           tempint;
   unsigned long templong;

   if (SRead_User(name))   return(-1);

/// Main loop
   while(1)
   {
      if (!INSTACK)  Print_User(name);

      a = iinput(1,51,0, SA[2190]);
      Clr(UserDat.Ansi_Flag);

///   Act on input
      switch(a)
      {
///      RETURN
         case 0:
                  if (!INSTACK)
                  {
                     AFPrintf(NULL,sout,SA[2191]);
                     Clr(UserDat.Ansi_Flag);
                  }
                  
                  WriteUser(name,&User);
                  return(0);
                  break;
//-

         case 1:
                  if (DLGInput(NULL,string,User.Alias,35,35,3, SA[2192]))
                     strcpy(User.Alias,string);
                  
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 2:
                  if (DLGInput(NULL,string,User.Address,25,25,1, SA[2193]))
                     strcpy(User.Address,string);
                  
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 3:
                  User.Computer_Type = iinput(1,Display_Computer_Types(),1, SA[2194]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 4:
                  if (DLGInput(NULL,string,User.City,23,23,1, SA[2195]))
                     strcpy(User.City,string);
                  
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 5:
                  if (DLGInput(NULL,string,NULL,11,11,2, SA[2196]))
                     strcpy(User.Password,string);
                  
                  AFPrintf(NULL,sout,"\n\n");
                  Upper(User.Password);
                  break;

         case 6:
                  if (DLGInput(NULL,string,User.Prov,5,5,1, SA[2197]))
                     strcpy(User.Prov,string);
                 
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 7:
                  if (DLGInput(NULL,string,User.Country,24,24,1, SA[2198]))
                     strcpy(User.Country,string);
                  
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 8:
                  if(DLGInput(NULL,string,User.postal,7,7,1, SA[2199]))
                     strcpy(User.postal,string);
      
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 9:
                  User.Birth_Year = iinput(1,99,User.Birth_Year, SA[2200]);
                  AFPrintf(NULL,sout,"\n");
                  User.Birth_Month = iinput(1,12,User.Birth_Month, SA[2201]);
                  AFPrintf(NULL,sout,"\n");
                  User.Birth_Day = iinput(1,31,User.Birth_Day, SA[2202]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 10:
                  if(DLGInput(NULL,string,User.Phone,14,14,0, SA[2203]))
                     strcpy(User.Phone,string);
      
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 11:
                  AFPrintf(&UserDat,sout,SA[2204]);
                  DLGInput(NULL,string,NULL,1,1,1, SA[2205]);
                  AFPrintf(NULL,sout,"\n\n");
      
                  if(string[0]=='N')
                     User.Help_Level=0;
      
                  if(string[0]=='I')
                     User.Help_Level=1;
      
                  if(string[0]=='E')
                     User.Help_Level=2;
      
                  break;

         case 12:
                  oldlevel=User.User_Level;
                  User.User_Level = iinput(0,255,User.User_Level, SA[2206]);
                  AFPrintf(NULL,sout,"\n\n");
      
                  if(User.User_Level!=oldlevel)
                  {
                     AFPrintf(NULL,sout,SA[2207]);
                     AddGlobalAreas(name,oldlevel,User.User_Level,0|128,"MSG:Area.bbs");
                     AddGlobalAreas(name,oldlevel,User.User_Level,1|128,"FILE:Area.bbs");
                     AFPrintf(NULL,sout,SA[2092]);
                  }
                  
                  break;

         case 13:
                  User.Screen_Len=iinput(5,128,User.Screen_Len, SA[2208]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 14:
                  User.More_Flag=((User.More_Flag+1)&1);
                  break;

         case 15:
                  User.Daily_Limit = iinput(1,1440,User.Daily_Limit, SA[2209]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 16:
                  User.Screen_Width = iinput(20,127,User.Screen_Width, SA[2210]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 17:
                  GetAnsi();
                  break;

         case 18:
                  User.Session_Limit = iinput(0,1440,User.Session_Limit, SA[2211]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 19:
                  User.Messages_Read = (long)iinput(0,30000,(int)User.Messages_Read, SA[2212]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 20:
                  User.Hot_Keys=((User.Hot_Keys+1)&1);
                  break;

         case 21:
                  User.Daily_Used = iinput(0,1440,User.Daily_Used, SA[2213]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 22:
                  User.Files_Uploaded = (long)iinput(0,30000,(int)User.Files_Uploaded, SA[2214]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 23:
                  User.Lex_Flag=((User.Lex_Flag+1)&1);
                  break;

         case 24:
                  User.Total_Calls = (long)iinput(0,30000,(int)User.Total_Calls, SA[2215]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 25:
                  templong=User.Bytes_Uploaded/1024L;
                  
                  if (DLGInput(NULL,string,NULL,8,8,0, SA[2216]))
                     sscanf(string,"%ld",&templong);

                  User.Bytes_Uploaded=templong*1024L;
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 26:
                  User.Pop_Screen=((User.Pop_Screen+1)&1);
                  break;

         case 27:
                  User.DirLimit = iinput(0,30000,User.DirLimit, SA[2217]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 28:
                  User.Files_Downloaded = (long)iinput(0,30000,(int)User.Files_Downloaded, SA[2218]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 29:
                  DLGInput(NULL,string,NULL,1,1,1, SA[2219]);
                  AFPrintf(NULL,sout,"\n\n");
      
                  if(string[0]=='C')
                  {
                     User.UUCP=1;
                  }
                  else
                  {
                     if(string[0]=='W')
                     {
                        User.UUCP=2;
                     }
                     else
                     {
                        User.UUCP=0;
                     }
                  }
                  
                  break;

         case 30:
                  User.Messages_Entered = (long)iinput(0,30000,(int)User.Messages_Entered, SA[2220]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 31:
                  templong=User.Bytes_Downloaded/1024L;

                  if(DLGInput(NULL,string,NULL,8,8,4, SA[2221]))
                  {  
                     if(sscanf(string,"%ld",&templong))
                     {
                        User.Bytes_Downloaded=templong*1024L;
                     }
                  }
      
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 32:
                  if(DLGInput(NULL,string,NULL,8,8,4, SA[2222]))
                  {
                     if(sscanf(string,"%ld",&templong))
                     {
                        User.Online_Time = templong;
                     }
                  }
      
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 33:
                  User.Last_Area = iinput(1,999999,User.Last_Area, SA[2223]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 34:
                  User.Sysop_Pages = (long)iinput(0,30000,(int)User.Sysop_Pages, SA[2224]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 35:
                  User.Bulletin_Access=((User.Bulletin_Access+1)&1);
                  break;

         case 36:
                  User.Last_File_Area = iinput(1,999999,User.Last_File_Area, SA[2225]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 37:
                  User.Ratio=iinput(0,99,User.Ratio, SA[2226]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 38:
                  User.credit=iinput(0,32000,User.credit, SA[2227]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 39:
                  ListProtocols(User.User_Level,valid,PROTO_SEND,1);
                  DLGInput(NULL,string,NULL,1,1,1, SA[2228]);
                  User.send='\0';
                  a=0;
      
                  while(valid[a]!='\0')
                  {
                     if(valid[a++]==string[0])
                     {
                        User.send=string[0];
                     }
                  }
                  
                  AFPrintf(NULL,sout,"\n\n");

                  ListProtocols(User.User_Level,valid,PROTO_REC,1);
                  DLGInput(NULL,string,NULL,1,1,1, SA[2229]);
      
                  a=0;
      
                  User.receive='\0';
      
                  while(valid[a]!='\0')
                  {
                     if(valid[a++]==string[0])
                     {
                        User.receive=string[0];
                     }
                  }

                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 40:
                  User.captureflags=0;
                  zflag=finput(1, SA[2230]);
                  AFPrintf(NULL,sout,"\n");
      
                  if(!zflag)
                  {
                     AFPrintf(NULL,sout,"\n");
                     break;
                  }
      
                  User.captureflags|=1;
                  zflag=finput(0, SA[2231]);
                  AFPrintf(NULL,sout,"\n");
      
                  if(zflag)
                     User.captureflags|=2;
      
                  zflag=finput(0, SA[2232]);
                  AFPrintf(NULL,sout,"\n\n");
      
                  if(zflag)
                     User.captureflags|=4;
      
                  break;

///      Edit Editor
         case 41:
                  tempint=iinput(1,ListEditors(User.User_Level),User.Editor, SA[2233]);
                  AFPrintf(NULL,sout,"\n\n");
                  
                  if(CheckEditor(tempint,User.User_Level))
                     User.Editor=tempint;
      
                  break;
//-

         case 42:
                  User.NetMail=0;
      
                  if(finput(1, SA[2234]))
                     User.NetMail+=1;
      
                  AFPrintf(NULL,sout,"\n\n");
      
                  if(finput(1, SA[2235]))
                     User.NetMail+=2;
      
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 43:
                  Edit_Search_Areas(name, 0);
                  break;

         case 44: 
                  Edit_Search_Areas(name, 1);
                  break;

         case 45:
                  Edit_Search_Areas(name, 2);
                  break;

         case 46:
                  tempint=iinput(1,ListArchivers(User.User_Level,COMPRESS|DECOMPRESS|VIEW|INTEGRITY),0, SA[2236]);
                  AFPrintf(NULL,sout,"\n\n");
                  
                  if(CheckArchiver(tempint,User.User_Level,COMPRESS|DECOMPRESS|VIEW|INTEGRITY) || !tempint)
                     User.archiver=tempint;
      
                  break;

         case 47:
                  tempint=iinput(1,ListMenuSets(User.User_Level),0, SA[2237]);
                  AFPrintf(NULL,sout,"\n\n");
      
                  if(tempint==0 || CheckMenuSet(tempint,User.User_Level))
                     User.menuset=tempint;
      
                  break;

         case 48:
                  if(!Exists("DLGConfig:CharSets/CharSets.bbs"))
                  {
                     AFPrintf(NULL,sout,SA[2238]);
                     Pause();
                     break;
                  }
      
                  ListSets(User.User_Level);
                  tempint=iinput(1,255,0, SA[2239]);
      
                  AFPrintf(NULL,sout,"\n\n");
      
                  if(tempint==0 || CheckSet(tempint,User.User_Level))
                     User.charset=tempint;
                  
                  break;

         case 49:
                  DLGInput(NULL,User.stack,User.stack,39,39,7, SA[2240]);
                  AFPrintf(NULL,sout,"\n\n");
                  break;

         case 50:
                  zflag=finput(1, SA[2241]);
                  AFPrintf(NULL,sout,"\n\n");
                  oldlevel=User.User_Level;
      
                  if(zflag)
                  {
                     RawValidate(&User,0);
                  }
                  else
                  {
                     RawValidate(&User,1);
                  }
      
                  AFPrintf(NULL,sout,SA[2242]);
                  AddGlobalAreas(name,oldlevel,User.User_Level,0|128,"MSG:Area.bbs");
                  AddGlobalAreas(name,oldlevel,User.User_Level,1|128,"FILE:Area.bbs");
                  AFPrintf(NULL,sout,SA[2092]);

                  if(Exists("DLGConfig:batch/AppliedTemplate.batch"))
                  {
                     UnderScore(name);
                     ASPrintf(NULL,filename,"Execute DLGConfig:batch/AppliedTemplate.batch %s %d %d",name,oldlevel,User.User_Level);
                     DeScore(name);
                     Execute(filename,0L,0L);
                  }

                  break;
 
         case 51:
                  ASPrintf(NULL,string,"DLGCONFIG:Notes/%s.Notes",name);
                  UnderScore(string);
      
                  if(CallEditor(NULL,NULL,string,TEXT,&UserDat,&RStruct,Ext)==0)
                  {
                     AFPrintf(NULL,sout,SA[2243]);
                  }
                  else
                  {
                     AFPrintf(NULL,sout,SA[2244]);
                  }
      
                  break;
      }
//-
   }
//-
}
//-
/// SRead_User
int SRead_User(char *name)
{
   BPTR  fp;
 
   char filename[80];

   ASPrintf(NULL,filename,"USER:%s/User.Data", name);
   UnderScore(filename);
 
   if ((fp=Open(filename,MODE_OLDFILE)) == EOF)
   {
      AFPrintf(NULL,sout,SA[2245], name);
      return(1);
   }

   Read(fp, &User, sizeof(User));
   Close(fp);
   return(0);
}
//-

