/* dietpad.c -- command functions for dietaid.c */
/* copywrited June 1986 by Terry W. Gintz */

#define MAXITEMS 202    /* total items in ingredient list (ingredients.c) */
struct itemvalues ingredient[31];  /* up to 30 ingredients per recipe */
char ingred[28]="     Ingredient            ";
char carbo[13]="Carbohydrate";
char pro[10]=" Protein ";
char fats[8]=" Fat   ";
char cal[9]="Calories";
char amount[9]="  Amount";
int number=0;  /* number of ingredients in recipe */
int adding;  /* set flag to add ingredients until "NONE" entered for name */
int valid; /* set flag on valid name (anything but "NONE" or "NULL") */
int totalcal;  /* total calories in recipe */
short int saved=1; /* set flag until new recipe is saved */
double totalcar,totalfat,totalpro; /* totals for recipe */
void addtorecipe(),clearlist(),cutoff(),divide(),showdata();
void saverecipe(),loadrecipe();
char getanswer();
short int matched; /* set flag when ingredient data is known */
extern FILE *console;
char *p,q,buffer[31];
short int waittable;

newrecipe()
{
   register short int i,n;

   if (NOT saved)
      saveoption();
   number=0;
   saved=1;
   clearlist();
   for (i=0;i<30;i++){   /* add up to 30 ingredients per recipe */
      valid=0;
      adding=1;
      matched=0;
      while ((NOT matched)||(NOT valid)){
         getname(i);    /* get name of ingredient */
         if (NOT adding) /* no more ingredients to add */
            break;
         for (n=0;n<MAXITEMS;n++){ /* check for item name in data list */
            checklist(n,i);
            if (matched){
               break;
            }
         }  /* for every item in data list checked until matched */
         if (NOT matched){
            manual(i); /* manual entry of ingredient data */
         }
      } /* while ingredient name being entered and validated */
      number=i+1;
      if (NOT adding){
         --number;
         break;
      }
   } /* last ingredient entered */

   if (number>0){
      showdata();
      saved=0; /* protect data from being overwritten until
                  save option is used or offered */
   }
}

showtable()
{
   register short int i,n,a,b;

   b=0;
   a=0;
   n=1;
   waittable=0;
   fprintf(console,"These are the ingredients I know values for: \n");
   for (i=0;i<MAXITEMS;i++){
      if (n==22){  /* show list in 22-line pages */
         waittable=1;
         waitreturn();
         if(toupper(q)=='Q')
            break;
         fprintf(console,"\f");
         n=0;
      }
      a++;
      if (a<MAXITEMS){   /* eliminate duplicates */
         if (strcmp(ingredval[i].name,ingredval[a].name)!=0){
            if (b<2)
               fprintf(console,"%-25s ",ingredval[i].name);
            else
               fprintf(console,"%-25s\n",ingredval[i].name);
            b++;
         }
      }
      else{
         if (b<2)
            fprintf(console,"%-25s ",ingredval[i].name);
         else
            fprintf(console,"%-25s\n",ingredval[i].name);
         b++;
      }
      if (b==3){
         n++;
         b=0;
      }
   }
   waittable=0;
   fprintf(console,"\n\n");
}

void clearlist()
{
   register short int i;

   for (i=0;i<31;i++){    /* clear ingredient list before adding recipe */
      strcpy(ingredient[i].name,"NULL");
      ingredient[i].quantity=0;
      ingredient[i].measurement='N';
      ingredient[i].calories=-1;
      ingredient[i].protein=-1.0;
      ingredient[i].carbohydrate=-1.0;
      ingredient[i].fat=-1.0;
   }
}

getname(i)
register short int i;
{
   register short int n;
   int length;

   while (NOT valid){
      fprintf(console,"What's the Name of Ingredient #");
      fprintf(console,"%d? ('NONE' to quit) ",i+1);
      rewind(console);
      fgets(buffer,25,console);
      n=strlen(buffer)-1;
      buffer[n]='\0';
      strcpy(ingredient[i].name,buffer);
      valid=strcmp(ingredient[i].name,"NULL");
      if (valid!=0)
         valid=1;
      if (strlen(ingredient[i].name)<1)
         valid=0;
      rewind(console);
   }
   length=strlen(ingredient[i].name);
   for (n=0;n<length;n++){
      if (ingredient[i].name[n]==' ')
         ingredient[i].name[n]='_';
      else
         ingredient[i].name[n]=toupper(ingredient[i].name[n]);
   }
   adding=strcmp(ingredient[i].name,"NONE");
   if (adding==0)
      strcpy(ingredient[i].name,"NULL");
   if(adding!=0)
      adding=1;
}

checklist(n,i)
register short int n,i; 
{
   int status;
   char answer;

   status=strcmp(ingredval[n].name,ingredient[i].name);
   if (status==0){
      fprintf(console,"Okay, I know the value of that ingredient");
      switch (ingredval[n].state){
         case 'U':
            fprintf(console,", raw.\n Is that what you're using?");
            fprintf(console," (y/n)");
            rewind(console);
            answer=getanswer();
            rewind(console);
            answer=toupper(answer);
            if (answer == 'Y'){
               matched=1;
               ingredient[i].measurement=ingredval[n].measurement;
               ingredient[i].calories=ingredval[n].calories;
               ingredient[i].protein=ingredval[n].protein;
               ingredient[i].carbohydrate=ingredval[n].carbohydrate;
               ingredient[i].fat=ingredval[n].fat;
            }
            break;
         case 'C':
            fprintf(console,", cooked.\n Is that what you're ");
            fprintf(console,"using? (y/n)");
            rewind(console);
            answer=getanswer();
            rewind(console);
            answer=toupper(answer);
            if (answer == 'Y'){
               matched=1;
               ingredient[i].measurement=ingredval[n].measurement;
               ingredient[i].calories=ingredval[n].calories;
               ingredient[i].protein=ingredval[n].protein;
               ingredient[i].carbohydrate=ingredval[n].carbohydrate;
               ingredient[i].fat=ingredval[n].fat;
            }
            break;
         default:
            fprintf(console,".\n");
            matched=1;
            ingredient[i].measurement=ingredval[n].measurement;
            ingredient[i].calories=ingredval[n].calories;
            ingredient[i].protein=ingredval[n].protein;
            ingredient[i].carbohydrate=ingredval[n].carbohydrate;
            ingredient[i].fat=ingredval[n].fat;
            break;
      }
      if (matched){
         ingredient[i].quantity=0;
         while(ingredient[i].quantity<=0){
            switch(ingredient[i].measurement){
               case 'C':
                  fprintf(console,"How many cups of ");
                  fprintf(console,"%s ",ingredient[i].name);
                  fprintf(console,"in this recipe?\n");
                  rewind(console);
                  fscanf(console,"%lf",&ingredient[i].quantity);
                  rewind(console);
                  showingred(i);
                  break;
               case 'P':
                  fprintf(console,"How many pounds of ");
                  fprintf(console,"%s ",ingredient[i].name);
                  fprintf(console,"in this recipe?\n");
                  rewind(console);
                  fscanf(console,"%lf",&ingredient[i].quantity);
                  rewind(console);
                  showingred(i);
                  break;
               case 't':
                  fprintf(console,"How many teaspoons of ");
                  fprintf(console,"%s ",ingredient[i].name);
                  fprintf(console,"in this recipe?\n");
                  rewind(console);
                  fscanf(console,"%lf",&ingredient[i].quantity);
                  rewind(console);
                  showingred(i);
                  break;
               case 'T':
                  fprintf(console,"How many tablespoons of ");
                  fprintf(console,"%s ",ingredient[i].name);
                  fprintf(console,"in this recipe?\n");
                  rewind(console);
                  fscanf(console,"%lf",&ingredient[i].quantity);
                  rewind(console);
                  showingred(i);
                  break;
               case 'O':
                  fprintf(console,"How many ounces of ");
                  fprintf(console,"%s ",ingredient[i].name);
                  fprintf(console,"in this recipe?\n");
                  rewind(console);
                  fscanf(console,"%lf",&ingredient[i].quantity);
                  rewind(console);
                  showingred(i);
                  break;
               default:
                  fprintf(console,"How many ");
                  fprintf(console,"%s ",ingredient[i].name);
                  fprintf(console,"in this recipe?\n");
                  rewind(console);
                  fscanf(console,"%lf",&ingredient[i].quantity);
                  rewind(console);
                  showingred(i);
                  break;
            }
         }
      }
   }
}

char getanswer()
{
   int n,yn;
   char answer[30];

   yn=0;
   while (NOT yn){
      rewind(console);
      fgets(answer,29,console);
      n=strlen(answer)-1;
      answer[n]='\0';
      rewind(console);
      if (strlen(answer)==0)
         fprintf(console,"Answer 'yes' or 'no', please.");
      else
         yn=1;
   }
   return(answer[0]);
}

manual(i)
register short int i;
{
   short int validmeasure;
   char answer,measure[12];

   fprintf(console,"That's not in my data list.\n");
   fprintf(console,"Do you want to enter the data manually? (y/n)");
   rewind(console);
   answer=getanswer();
   rewind(console);
   if (toupper(answer)=='Y'){  /* ask for quantity, measurement, calories,
                                     protein, carbohydrate, fat */
      matched=1;
      validmeasure=0;
      while(NOT validmeasure){
         fprintf(console,"What is the standard measure used?\n");
         fprintf(console,"('C'=cup,'t'=teaspoon,'T'=tablespoon,\n");
         fprintf(console," 'P'=pound,'O'=ounce,'W'=whole\n");
         rewind(console);
         fscanf(console,"%c",&ingredient[i].measurement);
         rewind(console);
         if (ingredient[i].measurement!='t');
            ingredient[i].measurement=toupper(ingredient[i].measurement);
         switch(ingredient[i].measurement){
            case 'C':
               strcpy(measure,"cup");
               validmeasure=1;
               break;
            case 't':
               strcpy(measure,"teaspoon");
               validmeasure=1;
               break;
            case 'T':
               strcpy(measure,"tablespoon");
               validmeasure=1;
               break;
            case 'P':
               strcpy(measure,"pound");
               validmeasure=1;
               break;
            case 'O':
               strcpy(measure,"ounce");
               validmeasure=1;
               break;
            case 'W':
               validmeasure=1;
               break;
            default:
               fprintf(console,"Not a standard measure!\n");
         }
      }
      while(ingredient[i].quantity<=0){
         switch(ingredient[i].measurement){
            case 'C':
               fprintf(console,"How many cups of ");
               fprintf(console,"%s ",ingredient[i].name);
               fprintf(console,"in this recipe?\n");
               rewind(console);
               fscanf(console,"%lf",&ingredient[i].quantity);
               rewind(console);
               break;
            case 'P':
               fprintf(console,"How many pounds of ");
               fprintf(console,"%s ",ingredient[i].name);
               fprintf(console,"in this recipe?\n");
               rewind(console);
               fscanf(console,"%lf",&ingredient[i].quantity);
               rewind(console);
               break;
            case 't':
               fprintf(console,"How many teaspoons of ");
               fprintf(console,"%s ",ingredient[i].name);
               fprintf(console,"in this recipe?\n");
               rewind(console);
               fscanf(console,"%lf",&ingredient[i].quantity);
               rewind(console);
               break;
            case 'T':
               fprintf(console,"How many tablespoons of ");
               fprintf(console,"%s ",ingredient[i].name);
               fprintf(console,"in this recipe?\n");
               rewind(console);
               fscanf(console,"%lf",&ingredient[i].quantity);
               rewind(console);
               break;
            case 'O':
               fprintf(console,"How many ounces of ");
               fprintf("%s ",ingredient[i].name);
               fprintf("in this recipe?\n");
               rewind(console);
               fscanf(console,"%lf",&ingredient[i].quantity);
               rewind(console);
               break;
         }
      }
      fprintf(console,"How many calories in one %s",measure);
      fprintf(console," of %s?\n",ingredient[i].name);
      rewind(console);
      fscanf(console,"%d",&ingredient[i].calories);
      rewind(console);
      fprintf(console,"How much protein in one %s",measure);
      fprintf(console," of %s?\n",ingredient[i].name);
      rewind(console);
      fscanf(console,"%lf",&ingredient[i].protein);
      rewind(console);
      fprintf(console,"How much carbohydrate in one %s",measure);
      fprintf(console," of %s?\n",ingredient[i].name);
      rewind(console);
      fscanf(console,"%lf",&ingredient[i].carbohydrate);
      rewind(console);
      fprintf(console,"How much fat in one %s",measure);
      fprintf(console," of %s?\n",ingredient[i].name);
      rewind(console);
      fscanf(console,"%lf",&ingredient[i].fat);
      rewind(console);
      showingred(i);
   }
   else {  /* re-enter name, if mistake */
      strcpy(ingredient[i].name,"NULL");
      valid=0;
   }
}

void showdata()
{
   register short int i;
   int calo;
   double fa,prot,carb;

   if (number==0){
      fprintf(console,"\f");
      fprintf(console,"There's no recipe to show!\n");
      return;
   }
   totalcal=0;
   totalpro=0;
   totalcar=0;
   totalfat=0;
   fprintf(console,"\f");
   fprintf(console,"%s%s%s%s%s%s\n\n",amount,ingred,cal,pro,carbo,fats);
   for (i=0;i<number;i++){
      if (ingredient[i].quantity!=1.0){
         calo=(int)(ingredient[i].quantity*ingredient[i].calories);
         prot=ingredient[i].quantity*ingredient[i].protein;
         carb=ingredient[i].quantity*ingredient[i].carbohydrate;
         fa=ingredient[i].quantity*ingredient[i].fat;
      }
      else{
         calo=ingredient[i].calories;
         prot=ingredient[i].protein;
         carb=ingredient[i].carbohydrate;
         fa=ingredient[i].fat;
      }
      totalcal=totalcal+calo;
      totalfat=totalfat+fa;
      totalpro=totalpro+prot;
      totalcar=totalcar+carb;
      if (i==18){
         fprintf(console,"\n");
         waitreturn();
         fprintf(console,"\f");
         fprintf(console,"%s%s%s",amount,ingred,cal);
         fprintf(console,"%s%s%s\n\n",pro,carbo,fats);
      }
      fprintf(console,"%2d %2.2lf ",i+1,ingredient[i].quantity);
      fprintf(console,"%c ",ingredient[i].measurement);
      fprintf(console,"%-25s",ingredient[i].name);
      fprintf(console,"%6d  ",calo);
      fprintf(console,"  %5.1lf",prot);
      fprintf(console,"     %5.1lf",carb);
      fprintf(console,"  %5.1lf\n",fa);
   }
   fprintf(console,"\n%s","Totals:                             ");
   fprintf(console,"%6d  ",totalcal);
   fprintf (console,"  %5.1lf",totalpro);
   fprintf(console,"     %5.1lf",totalcar);
   fprintf(console,"  %5.1lf\n",totalfat);
}

showingred(i)
short int i;
{
   int calo;
   double fa,prot,carb;

      if (ingredient[i].quantity!=1.0){
         calo=(int)(ingredient[i].quantity*ingredient[i].calories);
         prot=ingredient[i].quantity*ingredient[i].protein;
         carb=ingredient[i].quantity*ingredient[i].carbohydrate;
         fa=ingredient[i].quantity*ingredient[i].fat;
      }
      else{
         calo=ingredient[i].calories;
         prot=ingredient[i].protein;
         carb=ingredient[i].carbohydrate;
         fa=ingredient[i].fat;
      }
      fprintf(console,"\n%2d %2.2lf ",i+1,ingredient[i].quantity);
      fprintf(console,"%c ",ingredient[i].measurement);
      fprintf(console,"%-25s",ingredient[i].name);
      fprintf(console,"  %6d",calo);
      fprintf(console,"  %5.1lf",prot);
      fprintf(console,"     %5.1lf",carb);
      fprintf(console,"  %5.1lf\n\n",fa);
}

saveoption()
{
   char answer;

   fprintf(console,"Do you want to save the current recipe before ");
   fprintf(console,"proceeding? (y/n)");
   rewind(console);
   answer=getanswer();
   rewind(console);
   if(toupper(answer)=='Y')
      saverecipe();
   saved=1;
}

waitreturn()
{
   q='_';
   fprintf(console,"Type <RETURN> to Continue with Listing");
   if (waittable)
      fprintf(console,", 'Q' to quit");
   rewind(console);
   fscanf(console,"%c",&q);
   rewind(console);
}

void addtorecipe()
{
   register short int i,n,start;

   if (number==0){
      fprintf(console,"Use 'N' to enter a new recipe\n");
      return;
   }
   if (number==30){
      fprintf(console,"Aren't 30 ingredients enough for one recipe?\n");
      fprintf(console,"One has to draw the line somewhere. . .\n\n");
      return;
   }
   start=number; /* start adding from ingredient # */
   for (i=number;i<30;i++){   /* add up to 30 ingredients per recipe */
      valid=0;
      adding=1;
      matched=0;
      while ((NOT matched)||(NOT valid)){
         getname(i);    /* get name of ingredient */
         if (NOT adding) /* no more ingredients to add */
            break;
         matched=0; /* name not valid until checked against data list or
                     ingredient data entered manually */
         for (n=0;n<MAXITEMS;n++){ /* check for item name in data list */
            checklist(n,i);
            if (matched){
               break;
            }
         }  /* for every item in data list checked until matched */
         if (NOT matched){
            manual(i); /* manual entry of ingredient data */
         }
      } /* while ingredient name being entered and validated */
      number=i+1;
      if (NOT adding){
         --number;
         break;
      }
   } /* last ingredient entered */

   if (number>start){
      showdata();
      saved=0; /* protect data from being overwritten until
                  save option is used or offered */
   }
}

void cutoff()
{
   register short inc,n;
   int calo,i;
   double carb,fa,prot;
   char answer;

   if (number==0){
      fprintf(console,"There's no ingredient to delete!\n");
      return;
   }
   i=0;
   fprintf(console,"Delete which ingredient (1-%d)?",number);
   rewind(console);
   fscanf(console,"%d",&i);
   rewind(console);
   if ((i>number)||(i<=0)){
      fprintf(console,"No such ingredient!\n");
      return;
   }
   --i;
   if (ingredient[i].quantity!=1.0){
      calo=(int)(ingredient[i].quantity*ingredient[i].calories);
      prot=ingredient[i].quantity*ingredient[i].protein;
      carb=ingredient[i].quantity*ingredient[i].carbohydrate;
      fa=ingredient[i].quantity*ingredient[i].fat;
   }
   else{
      calo=ingredient[i].calories;
      prot=ingredient[i].protein;
      carb=ingredient[i].carbohydrate;
      fa=ingredient[i].fat;
   }
   fprintf(console,"\n%2d %2.2lf ",i+1,ingredient[i].quantity);
   fprintf(console,"%c ",ingredient[i].measurement);
   fprintf(console,"%-28s",ingredient[i].name);
   fprintf(console," %6d   ",calo);
   fprintf(console,"%5.1lf     ",prot);
   fprintf(console,"%5.1lf  ",carb);
   fprintf(console,"%5.1lf\n\n",fa);
   fprintf(console,"Is this the ingredient you want to delete?");
   rewind(console);
   answer=getanswer();
   rewind(console);
   if (toupper(answer)=='Y'){
      --number;
      for (n=i;n<number;n++){
         inc=n+1;
         ingredient[n].quantity=ingredient[inc].quantity;
         strcpy(ingredient[n].name,ingredient[inc].name);
         ingredient[n].measurement=ingredient[inc].measurement;
         ingredient[n].calories=ingredient[inc].calories;
         ingredient[n].protein=ingredient[inc].protein;
         ingredient[n].carbohydrate=ingredient[inc].carbohydrate;
         ingredient[n].fat=ingredient[inc].fat;
      }
      fprintf(console,"Done.\n");
      saved=0;
   }
}

void divide()
{
   double div;
   register short int i;

   if (number==0){
      fprintf(console,"There's no recipe to divide!\n");
      return;
   }
   fprintf(console,"What factor should I divide this recipe by?");
   rewind(console);
   fscanf(console,"%lf",&div);
   rewind(console);
   for (i=0;i<number;i++)
      ingredient[i].quantity/=div;
   showdata();
}

void loadrecipe()
{
   char filename[31];
   FILE *fp;
   short int i;
   char id[255];
   char temp[2];

   if (NOT saved)
      saveoption();
   fprintf(console,"What's the name of the recipe to load?");
   rewind(console);
   fscanf(console,"%s",filename);
   rewind(console);
   fp=fopen(filename,"r");
   if (fp==NULL){
      fprintf(console,"\nCan't open file\n");
      return;
   }
   fscanf(fp,"%s",id);
   if (strcmp(id,"RECIPE")!=0){
      fprintf(console,"Not a recipe file!\n");
      fclose(fp);
      return;
   }
   fscanf(fp,"%d",&number);
   for (i=0;i<31;i++){
      fscanf(fp,"%lf%s",&ingredient[i].quantity,temp);
      ingredient[i].measurement=temp[0];
      fscanf(fp,"%s%d",ingredient[i].name,&ingredient[i].calories);
      fscanf(fp,"%lf",&ingredient[i].protein);
      fscanf(fp,"%lf%lf",&ingredient[i].carbohydrate,&ingredient[i].fat);
   }
   fclose(fp);
   showdata();
}

void saverecipe()
{
   char filename[31];
   FILE *fp;
   short int i,n;
   char temp[2];
   int named;

   if (number==0){
      fprintf(console,"There's no recipe to save!\n");
      return;
   }
   named = 0;
   while (NOT named){
      fprintf(console,"What's the name of this recipe?");
      rewind(console);
      fgets(filename,30,console);
      n=strlen(filename)-1;
      filename[n]='\0';
      rewind(console);
      if (strlen(filename)>0)
         named=1;
   }
   fp=fopen(filename,"w");
   if (fp==NULL){
      fprintf(console,"\nCan't open file\n");
      return;
   }
   fprintf(fp,"%s\t%d\n","RECIPE",number);
   for (i=0;i<31;i++){
      temp[0]=ingredient[i].measurement;
      temp[1]='\0';
      fprintf(fp,"%lf\t%s",ingredient[i].quantity,temp);
      fprintf(fp,"\t%s\t%d",ingredient[i].name,ingredient[i].calories);
      fprintf(fp,"\t%lf\t",ingredient[i].protein);
      fprintf(fp,"%lf\t%lf\n",ingredient[i].carbohydrate,ingredient[i].fat);
   }
   fclose(fp);
   saved=1;
}

