/* iCalAlarm+SOGo.m - this file is part of SOGo
 *
 * Copyright (C) 2015-2022 Inverse inc.
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#import "iCalAlarm+SOGo.h"

#import <Foundation/NSArray.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSString.h>

#import <SOGo/SOGoUser.h>

#import <NGCards/iCalPerson.h>
#import <NGCards/iCalTrigger.h>
#import <NGCards/iCalEntityObject.h>

@implementation iCalAlarm (SOGoExtensions)

- (void) _appendAttendees: (NSArray *) attendees
             toEmailAlarm: (iCalAlarm *) alarm
{
  NSMutableArray *aAttendees;
  int count, max;
  iCalPerson *currentAttendee, *aAttendee;

  max = [attendees count];
  aAttendees = [NSMutableArray arrayWithCapacity: max];
  for (count = 0; count < max; count++)
    {
      currentAttendee = [attendees objectAtIndex: count];
      aAttendee = [iCalPerson elementWithTag: @"attendee"];
      [aAttendee setCn: [currentAttendee cn]];
      [aAttendee setEmail: [currentAttendee rfc822Email]];
      [aAttendees addObject: aAttendee];
    }
  [alarm setAttendees: aAttendees];
}

- (void) _appendOrganizerToEmailAlarm: (iCalAlarm *) alarm
                                owner: (NSString *) uid
{
  NSDictionary *ownerIdentity;
  iCalPerson *aAttendee;

  ownerIdentity = [[SOGoUser userWithLogin: uid roles: nil]
                    primaryIdentity];
  aAttendee = [iCalPerson elementWithTag: @"attendee"];
  [aAttendee setCn: [ownerIdentity objectForKey: @"fullName"]];
  [aAttendee setEmail: [ownerIdentity objectForKey: @"email"]];
  [alarm addChild: aAttendee];
}

+ (id) alarmForEvent: (iCalEntityObject *) theEntity
               owner: (NSString *) theOwner
              action: (NSString *) reminderAction
                unit: (NSString *) reminderUnit
            quantity: (NSString *) reminderQuantity
           reference: (NSString *) reminderReference
    reminderRelation: (NSString *) reminderRelation
      emailAttendees: (BOOL) reminderEmailAttendees
      emailOrganizer: (BOOL) reminderEmailOrganizer
{
  iCalTrigger *aTrigger;
  iCalAlarm *anAlarm;
  NSString *aValue;

  anAlarm = [[self alloc] init];

  aTrigger = [iCalTrigger elementWithTag: @"TRIGGER"];
  [aTrigger setValueType: @"DURATION"];
  [anAlarm setTrigger: aTrigger];
  
  if ([reminderAction length] > 0 && [reminderUnit length] > 0)
    {
      [anAlarm setAction: [reminderAction uppercaseString]];
      [anAlarm setSummary: [theEntity summary]];
      if ([[theEntity comment] length])
        [anAlarm setComment: [theEntity comment]];
      else
        // RFC 5545 requires the DESCRIPTION (comment) field to be defined for a VALARM
        [anAlarm setComment: [theEntity summary]];

      if ([reminderAction isEqualToString: @"email"])
        {
          [anAlarm removeAllAttendees];
          if (reminderEmailAttendees)
            [anAlarm _appendAttendees: [theEntity attendees]
                         toEmailAlarm: anAlarm];
          if (reminderEmailOrganizer)
            [anAlarm _appendOrganizerToEmailAlarm: anAlarm  owner: theOwner];
        }
      
      if ([reminderReference caseInsensitiveCompare: @"BEFORE"] == NSOrderedSame)
        aValue = (NSString *) @"-P";
      else
        aValue = (NSString *) @"P";
      
      if ([reminderUnit caseInsensitiveCompare: @"MINUTES"] == NSOrderedSame ||
          [reminderUnit caseInsensitiveCompare: @"HOURS"] == NSOrderedSame)
        aValue = [aValue stringByAppendingString: @"T"];
      
      aValue = [aValue stringByAppendingFormat: @"%i%@",			 
                       [reminderQuantity intValue],
                [reminderUnit substringToIndex: 1]];
      [aTrigger setSingleValue: aValue forKey: @""];
      [aTrigger setRelationType: reminderRelation];
    }
  else
    {
      [anAlarm release];
      anAlarm = nil;
    }
  
  return AUTORELEASE(anAlarm);
}

- (BOOL) userIsAttendee: (SOGoUser *) user
{
  NSEnumerator *attendees;
  iCalPerson *currentAttendee;
  BOOL isAttendee;

  isAttendee = NO;

  attendees = [[self attendees] objectEnumerator];
  currentAttendee = [attendees nextObject];
  while (!isAttendee
	 && currentAttendee)
    if ([user hasEmail: [currentAttendee rfc822Email]])
      isAttendee = YES;
    else
      currentAttendee = [attendees nextObject];

  return isAttendee;
}

@end
