
#import "ABEvent.h"


@implementation ABEvent


- (id) _initWithRouter:(id<ABPostboxDelegate>)router event:(NSDictionary*)event
{
    if ((self = [super init])) {
        _router = router;
        _event = event;
    }
    return self;
}

- (long) eventId
{
    return [((NSNumber*)[_event objectForKey:@"id"]) longValue];
}

- (NSString*) method
{
    return [_event objectForKey:@"method"];
}

- (NSDictionary*) info
{
    return [_event objectForKey:@"info"];
}


- (void) respond:(NSDictionary*)result;
{
    [self _sendResponseEvent:[NSMutableDictionary dictionaryWithObjectsAndKeys:
                             result, @"result",
                             nil]];
}

- (void) respondError:(NSDictionary*)error;
{
    [self _sendResponseEvent:[NSMutableDictionary dictionaryWithObjectsAndKeys:
                              error, @"error",
                              nil]];
}

- (void) _sendResponseEvent:(NSMutableDictionary*)e
{
    [e setObject:[_event objectForKey:@"from"] forKey:@"to"];
    [e setObject:[_event objectForKey:@"to"] forKey:@"from"];
    [e setObject:[_event objectForKey:@"id"] forKey:@"id"];
    [_router _processEvent:e];
}

@end
