package de.knipp.registryannouncements;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;


public class ICal
{
  StringBuffer sb = new StringBuffer ();
  private static final int MAX_LINE_LEN = 50;
  private static final String crlf = "\r\n";


  public ICal (Announcement a)
  {
    // prepare the presentation

    String whoAndWhat = a.getRegistryName () + " (" + a.getDomain () + "):";
    String affectedProd =
      (a.isAffectedProdSrs () ? "srs " : "") +
      (a.isAffectedProdWhois () ? "whois " : "") +
      (a.isAffectedProdAdminWebsite () ? "admin-website " : "") +
      (a.isAffectedProdDns () ? "dns " : "") +
      (a.isAffectedProdOther () ? "other " : "");
    if (affectedProd.length () > 0)
      whoAndWhat += " PROD " + affectedProd;

    String affectedOte =
      (a.isAffectedOteSrs () ? "srs " : "") +
      (a.isAffectedOteWhois () ? "whois " : "") +
      (a.isAffectedOteAdminWebsite () ? "admin-website " : "") +
      (a.isAffectedOteDns () ? "dns " : "") +
      (a.isAffectedOteOther () ? "other " : "");
    if (affectedOte.length () > 0)
      whoAndWhat += " OTE " + affectedOte;

    StringBuffer ds = new StringBuffer ();
    for (Description d: a.getDescriptions ())
    {
      ds.append (d.getMessage ().trim ());
      ds.append (" ");
    }
    String s = ds.toString ();
    s = s.replaceAll (",", "\\\\,");
    s = s.replaceAll (";", "\\\\;");


    // create the iCal event
    sb.append ("BEGIN:VCALENDAR"); sb.append (crlf);
    sb.append ("PRODID:-//CORE//Demo 1.0//EN"); sb.append (crlf);
    sb.append ("VERSION:2.0"); sb.append (crlf);
    sb.append ("CALSCALE:GREGORIAN"); sb.append (crlf);
    sb.append ("BEGIN:VEVENT"); sb.append (crlf);
    sb.append ("DTSTART:");
    sb.append (a.getDateFrom ().replaceAll ("[:-]", "").replaceAll ("\\.[0-9]+Z", "Z"));
    sb.append (crlf);
    sb.append ("DTEND:");
    sb.append (a.getDateTo ().replaceAll ("[:-]", "").replaceAll ("\\.[0-9]+Z", "Z"));
    sb.append (crlf);
    sb.append ("SUMMARY:"); sb.append (whoAndWhat); sb.append (crlf);
    sb.append ("DESCRIPTION:"); sb.append (longLines (s)); sb.append (crlf);
    sb.append ("CATEGORIES:GTLD,ICANN"); sb.append (crlf);
    sb.append ("END:VEVENT"); sb.append (crlf);
    sb.append ("END:VCALENDAR"); sb.append (crlf);
  }


  private StringBuffer longLines (String l)
  {

    StringBuffer res = new StringBuffer ();
    String[] tokens = l.split (" ");

    int lineLength = 0;
    for (int i = 0; i < tokens.length; i++)
    {
      String token = tokens[i];
      int tokenLength = token.length ();
      if (lineLength + tokenLength > MAX_LINE_LEN)
      {
        res.append (crlf);
        res.append (" ");
        lineLength = tokenLength;
      }
      else
      {
        lineLength += tokenLength + 1;
      }
      res.append (" ");
      res.append (token);
    }

    return new StringBuffer (res);
  }


  public String toString () {return sb.toString ();}


  public void toFile (String fileName)
  {
    FileWriter fw;
    try 
    {
      fw = new FileWriter (fileName);
      fw.write (sb.toString ());
      fw.close ();
    }
    catch (IOException e)
    {
      System.err.println ("fatal: could not write to " + fileName);
      System.err.println ("Exception: " + e);
      System.exit (-1);
    }
  }


//xxxxxxxxxxxxxxxxxxxx
public void test (String fileName)
{
try
{
java.io.FileInputStream fin = new java.io.FileInputStream (fileName);

net.fortuna.ical4j.data.CalendarBuilder builder = new net.fortuna.ical4j.data.CalendarBuilder ();

net.fortuna.ical4j.model.Calendar calendar = builder.build (fin);
}
catch (Exception e)
{
//kaputt
      System.err.println ("Exception: " + e);
}
}
}
