//
//  Message.m
//  NG911
//
//  Created by Mason Silber on 9/20/11.
//  Copyright (c) 2011 Columbia University. All rights reserved.
//

#import "Message.h"

@implementation Message

@synthesize message, timeStamp, image, sender, sent, imageData, dataString, PIDFClient;

// TODO: Change init methods to include sender address (local IP in this case)

-(void)dealloc
{
    if(message)
    {
        [message release];
    }
    if(image)
    {
        [image release];
    }
    if(sender)
    {
        [sender release];
    }
////    if(imageData)
////    {
////        [imageData release];
////    }
    if(PIDFClient)
    {
        [PIDFClient release];
    }
    [timeStamp release];
    
    [super dealloc];
}

-(id)initWithMessage:(NSString *)aMessage
{
    self = [super init];
    
    message = [aMessage copy];
    timeStamp = [[self getFormattedDate] retain];
    image = nil;
    return self;
}

-(id)initWithImage:(NSData *)anImage
{
    self = [super init];
    message = nil;
    image = [anImage copy];
    imageData = [UIImageJPEGRepresentation(image,1) retain];
    dataString = (const char *)[imageData bytes];
    
    timeStamp = [[self getFormattedDate] retain];
    return self;
}

-(NSString *)getFormattedDate
{
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"MMM dd, yyyy h:mm:ss a"];
    NSString *dateString = [formatter stringFromDate:currentDate];
    return dateString;
}

-(NSString *)description
{
    NSMutableString *description = [[[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"Message:\n%@",message]] autorelease];
    return description;
}

-(BOOL) containsImage {
    return (image != nil);
}

-(BOOL) containsText {
    return (message != nil);// && ![message isEqualToString:@""]);
}



@end
