#include "UserEdit.h"

void ViewPersonal(void);

/// EditPersonal()
void EditPersonal(void)
{
   BOOL  Changed  = FALSE;
   BOOL  Write    = FALSE;
   char  temp[128];
   char  c[2];

   while(TRUE)
   {
      ViewPersonal();

      DLGInput("Select letter to edit parameter -> ",NULL,c,NULL,1,1,1);
      TwoLines();

///   Change Alias (A)
      if(!Strnicmp(c,"A",1))
      {
         DLGInput("New Alias -> ",NULL,temp,NULL,29,29,3);
         TwoLines();

         temp[29] = '\0';

         if(0 == CheckUser(temp))
         {
            strcpy(udat.Alias,temp);
            AFPrintf(&UserDat,sout,"%a7Changed!\n\n");
            Write = TRUE;
         }
         else
         {
            AFPrintf(&UserDat,sout,"That alias is already in use.\n\n");
            Pause();
         }
      }
//-

///   Change Email (B)
      if(!Strnicmp(c,"B",1))
      {
         DLGInput("New Internet (E-Mail) Alias -> ",NULL,temp,NULL,29,29,3);
         TwoLines();
         temp[29] = '\0';

         if(0 == CheckUser(temp))
         {
            strcpy(udat.Email,temp);
            AFPrintf(&UserDat,sout,"%a7Changed!\n\n");
            Write = TRUE;
         }
         else
         {
            AFPrintf(&UserDat,sout,"That internet alias is already in use.\n\n");
            Pause();
         }
      }
//-

///   Street Address (C)
      if(!Strnicmp(c,"C",1))
      {
         DLGInput("New Street Address -> ",NULL,temp,NULL,26,26,3);
         TwoLines();
         temp[26] = '\0';
         strcpy(udat.Address,temp);
         AFPrintf(&UserDat,sout,"%a7Changed!\n\n");
         Write = TRUE;
      }
//-

///   City (D)
      if(!Strnicmp(c,"D",1))
      {
         DLGInput("New City -> ",NULL,temp,NULL,24,24,3);
         TwoLines();
         temp[24] = '\0';
         strcpy(udat.City,temp);
         AFPrintf(&UserDat,sout,"%a7Changed!\n\n");
         Write = TRUE;
      }
//-

///   State (E)
      if(!Strnicmp(c,"E",1))
      {
         DLGInput("New State or Province -> ",NULL,temp,NULL,6,6,1);
         TwoLines();
         temp[6] = '\0';
         strcpy(udat.Prov,temp);
         AFPrintf(&UserDat,sout,"%a7Changed!\n\n");
         Write = TRUE;
      }
//-

///   Country (F)
      if(!Strnicmp(c,"F",1))
      {
         DLGInput("New Country -> ",NULL,temp,NULL,25,25,1);
         TwoLines();
         temp[25] = '\0';
         strcpy(udat.Country,temp);
         AFPrintf(&UserDat,sout,"%a7Changed!\n\n");
         Write = TRUE;
      }
//-

///   Postal Code (G)
      if(!Strnicmp(c,"G",1))
      {
         DLGInput("New Postal Code -> ",NULL,temp,NULL,8,8,3);
         TwoLines();
         temp[8] = '\0';
         strcpy(udat.postal,temp);
         AFPrintf(&UserDat,sout,"%a7Changed!\n\n");
         Write = TRUE;
      }
//-

///   Phone Number (I)
      if(!Strnicmp(c,"I",1))
      {
         DLGInput("New Phone Number -> ",NULL,temp,NULL,15,15,3);
         TwoLines();
         temp[15] = '\0';
         strcpy(udat.Phone,temp);
         AFPrintf(&UserDat,sout,"%a7Changed!\n\n");
         Write = TRUE;
      }
//-

///   Computer Type (J)
      if(!Strnicmp(c,"J",1))
      {
         char cptr;

         cptr = SelectComputer();

         if(cptr != udat.Computer_Type)
         {
            udat.Computer_Type = cptr;
            AFPrintf(&UserDat,sout,"%a7Changed!\n\n");
            Write = TRUE;
         }
      }
//-

///   Birth Day (K)
      if(!Strnicmp(c,"K",1))
      {
         char bday, bmon, byear;

         bday =  IntQuery("Select DAY of birth -> ",1,31,0,&uin);
         TwoLines();

         if(bday)
         {
            bmon =  IntQuery("Select MONTH of birth -> ",1,12,0,&uin);
            TwoLines();

            if(bmon)
            {
               byear = IntQuery("Select YEAR of birth -> ",0,99,0,&uin);
               TwoLines();

               Changed  = TRUE;
               Write    = TRUE;

               udat.Birth_Year   = byear;
               udat.Birth_Month  = bmon;
               udat.Birth_Day    = bday;

               AFPrintf(&UserDat,sout,"%a7Changed!\n\n");
            }
         }
      }
//-



///   Exit
      if(!Strnicmp(c,"X",1))
      {
         if(Write)
         {
            struct NameStruct ns;

            strcpy(ns.name,name);

            if(0 == DeleteStruct(NAMERECORD,(char *)&ns,sizeof(struct NameStruct),30))
            {
               char fname[128];

               strcpy(ns.alias,udat.Alias);
               strcpy(ns.email,udat.Email);
               AddStruct(NAMERECORD,(char *)&ns,sizeof(struct NameStruct),30);

               ASPrintf(NULL,fname,"USER:%s/User.Data",name);
               UnderScore(fname);

               if(Changed && !Sysop)   // Only make backups if user changed it
               {
                  char bname[128];

                  ASPrintf(NULL,bname,"User:%s/User.Data.Backup",name);
                  UnderScore(bname);
                  Copy(fname,bname);
               }

               if(Same) memcpy(&UserDat,&udat,sizeof(struct USER_DATA));
               DeleteFile(fname);
               DumpFile(fname,(char *)&udat,sizeof(struct USER_DATA));
            }

            AFPrintf(&UserDat,sout,"%a7Changes saved.\n\n");
            Pause();
         }
         else
         {
            AFPrintf(&UserDat,sout,"%a7User record unchanged.\n\n");
            Pause();
         }

         break;
      }
//-

///   Quit
      if(!Strnicmp(c,"Q",1))
      {
         if(Write)
         {
            AFPrintf(&UserDat,sout,"%b7 %a1NOTE: %b0%a7 Changes not saved!\n\n");
            Pause();
         }

         break;
      }
//-

   }

   return;
}
//-

/// ViewPersonal()
void ViewPersonal(void)
{
   char computer[36];

   Clr(UserDat.Ansi_Flag);
   AFPrintf(&UserDat,sout,"\n%a6 Personal Information for user %a2[%au%a7%s%ao%a2]%a7\n",name);
   Draw_Line(UserDat.Screen_Width);

   AFPrintf(&UserDat,sout,"%b0%a2[ ] %a6NAME ... %b7%a1 %-30.30s \n",name);
   AFPrintf(&UserDat,sout,"%b0%a2[%a7A%a2] %a6ALIAS .. %b7%a4 %-30.30s \n",udat.Alias);
   AFPrintf(&UserDat,sout,"%b0%a2[%a7B%a2] %a6E-Mail . %b7%a4 %-30.30s \n",udat.Email);

   AFPrintf(&UserDat,sout,"%b0%a7");
   Draw_Line(UserDat.Screen_Width);

   AFPrintf(&UserDat,sout,"%b0%a2[%a7C%a2] %a6Street Address . %b7%a4 %-26.26s\n",udat.Address);
   AFPrintf(&UserDat,sout,"%b0%a2[%a7D%a2] %a6City ........... %b7%a4 %-24.24s  %b0 %a2[%a7E%a2] %a6State/Prov .. %b7%a4 %-6.6s  \n",udat.City,udat.Prov);
   AFPrintf(&UserDat,sout,"%b0%a2[%a7F%a2] %a6Country ........ %b7%a4 %-25.25s %b0 %a2[%a7G%a2] %a6Postal Code . %b7%a4 %-8.8s\n",udat.Country,udat.postal);
   AFPrintf(&UserDat,sout,"%b0%a2[%a7I%a2] %a6Phone Number ... %b7%a4 %-15.15s           \n",udat.Phone);

   AFPrintf(&UserDat,sout,"%b0%a7");
   Draw_Line(UserDat.Screen_Width);

   if(!GetComputerType(udat.Computer_Type,computer))
      strcpy(computer,"None Selected");

   AFPrintf(&UserDat,sout,"%b0%a2[%a7J%a2] %a6Computer Type .. %b7%a4 %-36.36s         \n",computer);

   AFPrintf(&UserDat,sout,"%b0%a2[%a7K%a2] %a6Birth Day ...... %b7%a4 %02.02d%a0 / %a4%02.02d%a0 / %a4%s%02.02d%b0 %a2[ ] %a6Gender . %b7%a1 Not Implemented \n",
            udat.Birth_Day,udat.Birth_Month,(udat.Birth_Year < 20)?"20":"19",udat.Birth_Year);

   AFPrintf(&UserDat,sout,"%b0%a7");
   Draw_Line(UserDat.Screen_Width);

   AFPrintf(&UserDat,sout,"%a2[%a7H%a2] %a6Help    %a2[%a7?%a2] %a6Re-Display    %a2[%a7X%a2] %a6Save and Exit   %a2[%a7Q%a2] %a6Quit (no save)%a7\n");
   Draw_Line(UserDat.Screen_Width);

   AFPrintf(&UserDat,sout,"    %a6Text in %b7 %a1Red Text %b0%a6 is not editable.%a7\n");
   Draw_Line(UserDat.Screen_Width);
   OneLine();

   return;
}
//-
