/*
 * BirthdayNotices.java
 */

package intranet;

import java.io.*;
import java.util.*;

public class BirthdayNotices
{
   private Vector birthdays = new Vector();

   private static final String birthdayFilename = "birthdays.csv";


   public BirthdayNotices()
   {
      File birthdayFile = new File(birthdayFilename);
      try
      {
         DataInputStream birthdayDataStream
            = new DataInputStream(new FileInputStream(birthdayFile));
         byte[] byteArray = new byte[(int) birthdayFile.length()];
         birthdayDataStream.readFully(byteArray);
         birthdayDataStream.close();
         String birthdayDataString = new String(byteArray);
         // note: assumes input file is Mac-format, ie lines end with '\r'
         StringTokenizer allBirthdayLines = new StringTokenizer(birthdayDataString, "\r");
         int count = 0;
         while (allBirthdayLines.hasMoreTokens())
         {
            String birthdayLine = allBirthdayLines.nextToken();
            StringTokenizer birthdayTokenizer = new StringTokenizer(birthdayLine, ",");
            String firstName = stripDoubleQuotes(birthdayTokenizer.nextToken());
            String lastName = stripDoubleQuotes(birthdayTokenizer.nextToken());
            String date = stripDoubleQuotes(birthdayTokenizer.nextToken());
            if ((date != null) && (date.length() > 0))
            {
               birthdays.addElement(new Birthday(firstName, lastName, date));
               count++;
            }
         }
      }
      catch (IOException e)
      {
         System.out.println("Exception in BirthdayNotices(): " + e.getMessage());
         e.printStackTrace(System.out);
      }
   }


   public String getBirthdayMessage()
   {
      StringBuffer message = new StringBuffer("");
      Calendar today = Calendar.getInstance();
      today.setTime(new Date());
      today.set(Calendar.HOUR_OF_DAY, 0);
      today.set(Calendar.MINUTE, 0);
      today.set(Calendar.SECOND, 0);
      today.set(Calendar.MILLISECOND, 0);
      int daysToClosest = 888;
      int numToShow = 0;
      StringBuffer personList = new StringBuffer("");
      String blanks = new String("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
      for (int i=0; i<birthdays.size(); i++)
      {
         Birthday birthdayEntry = (Birthday) birthdays.elementAt(i);
         Calendar thisDate = Calendar.getInstance();
         thisDate.setTime(new Date());
         thisDate.set(Calendar.MONTH, birthdayEntry.getMonth()-1);
         thisDate.set(Calendar.DATE, birthdayEntry.getDayOfMonth());
         int daysFromToday = daysApart(thisDate, today);
         if (daysFromToday < 0)
         {
            thisDate.roll(Calendar.YEAR, true);
            daysFromToday = daysApart(thisDate, today);
         }
         if (daysFromToday == daysToClosest)
         {
            numToShow++;
            personList.append("<BR>\n").append(blanks);
            personList.append(birthdayEntry.getFullName());
            personList.append("\n");
         }
         if (daysFromToday < daysToClosest)
         {
            daysToClosest = daysApart(thisDate, today);
            numToShow = 1;
            personList = new StringBuffer(blanks);
            personList.append(birthdayEntry.getFullName());
            personList.append("\n");
         }
      }
     if (daysToClosest <= 7)
      {
         message.append("<table>\n");
         message.append("<tr>\n");
         message.append("<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=../graphics/bdaycake.jpg width=70 height=50></td>\n");
         message.append("<td><img width=40 height=10 src=../graphics/blank_dot.gif></td>\n");
         message.append("<td>\n");
         if (daysToClosest == 0)
         {
            message.append("<i>Happy Birthday</i><p><b>\n");
            message.append(personList);
            message.append("</b><p>Hip-hip hooray!<P>\n");
         }
         else if (daysToClosest == 1)
         {
            message.append("Psst!<p><b>\n");
            message.append(personList);
            message.append("</b><p>will be having");
            if (numToShow == 1)
               message.append(" a birthday ");
            else
               message.append(" birthdays ");
            message.append("<i>tomorrow.</i><P>\n");
         }
         else
         {
            message.append("Early warning:<p><b>\n");
            message.append(personList);
            message.append("</b><p>will be having");
            if (numToShow == 1)
               message.append(" a birthday ");
            else
               message.append(" birthdays ");
            message.append("in ").append(daysToClosest).append(" days.<P>\n");
         }
         message.append("</td>\n");
         message.append("</tr>\n");
         message.append("</table>");
      }
      else
      {
         message.append("<blockquote>\n");
         message.append("<blockquote>\n");
         message.append("<font size=1>No birthdays for 7 days</font><P>\n");
         message.append("</blockquote>\n");
         message.append("</blockquote>");
      }
      return message.toString();
   }


   private static String stripDoubleQuotes(String aString)
   {
      return new String(aString.substring(aString.indexOf("\"")+1,
                                          aString.lastIndexOf("\"")));
   }


   private static int daysApart(Calendar testDate, Calendar today)
   {
      double millisecondsInOneDay = 24.0*60.0*60.0*1000.0;
      long timeDifference = testDate.getTime().getTime() - today.getTime().getTime();
      return (int) Math.floor((double) timeDifference/millisecondsInOneDay);
   }

}  // BirthdayNotices
