/*
 * $Id: calEvent.c,v 1.1.1.1 2005/02/17 05:45:51 sundin Exp $
 *
 * Tag: $Name:  $
 *
 * See the file COPYRIGHT for copyright information
 *
 * $Log: calEvent.c,v $
 * Revision 1.1.1.1  2005/02/17 05:45:51  sundin
 * initial import ver0.0.5
 *
 * Revision 1.1.1.1  2005/01/17 18:06:31  ksltuf
 * PIM-Plugin 0.0.4
 *
 * Revision 1.2  2004/07/05 19:00:26  ksltuf
 * Added COPYRIGHT and cvs information
 *
 *
 */


#include <string>
#include <iostream>

#include "i18n.h"
#include "calEvent.h"

// static member

char* cCalEvent::_buffer = NULL;

int cCalEvent::uniqueId = 0;


cCalEvent::cCalEvent()
{
  init();
}


cCalEvent::cCalEvent(int day, int month, int year, char* text)
{
  init();

  _type = evFixedDay;
  _day = day;
  _month = month;
  _year = year;
  _text = text;
}


cCalEvent::cCalEvent(int day, int month, char* text)
{
  init();

  _type = evYearly;
  _day = day;
  _month = month;
  _text = text;
}


cCalEvent::cCalEvent(int dayOfWeek, char* text)
{
  init();

  _type = evWeekly;
  _dayOfWeek = dayOfWeek;
  _text = text;
}


cCalEvent::~cCalEvent()
{
  if (_text)
    free(_text);
}


cCalEvent& cCalEvent::operator= (const cCalEvent& ev)
{
  memcpy(this, &ev, sizeof(*this));
  if (ev._text)
  {
    _text = strdup(ev._text);
  }
  return *this;
}


void cCalEvent::init()
{
  _day = 1;
  _month = 1;
  _year = 2004;
  _dayOfWeek = 1;

  _type = evFixedDay;

  asprintf(&_text, "%*s", MAX_EVENT_NAME, " ");

  _id = uniqueId++;
}


bool cCalEvent::isValid() const
{
  return(_type == evYearly && isValidDay() && isValidMonth()
         ||
	 _type == evWeekly && isValidDayOfWeek()
	 ||
	 _type == evFixedDay && isValidDay() && isValidMonth() && isValidYear()
	);
}


bool cCalEvent::isValidDay() const
{
  // TODO this is very unclean, make it at least month-dependent
  return ( _day >= 1 && _day <= 31 );
}


bool cCalEvent::isValidMonth() const
{
  return ( _month >= 1 && _month <= 12 );
}


bool cCalEvent::isValidYear() const
{
  return ( _year >= 1900 && _year <= 2100 );
}


bool cCalEvent::isValidDayOfWeek() const
{
  return ( _dayOfWeek >= 1 && _dayOfWeek <= 7 );
}


bool cCalEvent::validForDay (int day, int month, int year, int dayOfWeek)
{
  return (_type==evFixedDay && _day == day && _month == month && _year == year
	  ||
	  _type==evWeekly && _dayOfWeek == dayOfWeek
	  ||
	  _type==evYearly && _day == day && _month == month);

}


char* cCalEvent::getText() const
{
  return _text;
}


char* cCalEvent::getLongText() const
{
  if (_buffer) free(_buffer);
  
  switch (_type)
  {
    case evFixedDay:
      asprintf(&_buffer,"%s\t%02d.%02d.%04d\t%s", tr("Fix Day"),
	       _day, _month, _year, _text);
      break;

    case evWeekly:
      asprintf(&_buffer,"%s\t%d\t%s", tr("Weekly"), _dayOfWeek, _text);
      break;

    case evYearly:
      asprintf(&_buffer,"%s\t%02d.%02d.*\t%s", tr("Yearly"), _day, _month,
	       _text);
      break;

    default:
      break;
  }

  return _buffer;
}


int cCalEvent::getId() const
{
  return _id;
}


void cCalEvent::print()
{
  std::cout << "Event id=" << _id
	    << " Type: " << _type
	    << " d=" << _day << " m=" <<  _month << " y=" <<  _year
	    << " text=" << _text
	    << std::endl;
}


char* cCalEvent::toString()
{
  if (_buffer) free(_buffer);
  
  switch (_type)
  {
    case evFixedDay:
      asprintf(&_buffer, "F:%02d.%02d.%d:%s\n", _day, _month, _year, _text);
      break;

    case evWeekly:
      asprintf(&_buffer, "W:%d:%s\n", _dayOfWeek, _text);
      break;

    case evYearly:
      asprintf(&_buffer, "Y:%02d.%02d.:%s\n", _day, _month, _text);
      break;

    default:
      break;
  }

  return _buffer;
}


bool cCalEvent::Save(FILE *f)
{
  if ( isValid() )
    return fprintf (f, toString()) > 0;
  else
    return false;
}


bool cCalEvent::Parse(const char* s)
{
  bool rc = false;

  char dummy;

  if (!s)
    return rc;

  switch (s[0])
    {
    case 'F':
      if (sscanf (s, "%c:%d.%d.%d:%a[^:]",
		  &dummy, &_day, &_month, &_year, &_text))
	{
	  _type = evFixedDay;
	  rc = isValid();
	}
      break;

    case 'Y':
      if (sscanf (s, "%c:%d.%d.:%a[^:]", &dummy, &_day, &_month, &_text))
	{
	  _type = evYearly;
	  rc = isValid();
	}
      break;

    case 'W':
      if (sscanf (s, "%c:%d:%a[^:]", &dummy, &_dayOfWeek, &_text))
	{
	  _type = evWeekly;
	  rc = isValid();
	}
      break;

    default:
      break;
    }

  return rc;
}
