//
//  SetChannelActionHandler.m
//  CyTV
//
//  Created by Andreas Junghans on 16.01.08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "SetChannelActionHandler.h"
#import "AppleScript.h"
#import "JSON.h"
#import "HTTPServer.h"


@implementation SetChannelActionHandler

- (void)handleRequest {
    SInt32 channel = [[_paramsDict objectForKey:@"number"] intValue];
    @try {
        /*NSAppleEventDescriptor *desc = */[AppleScript executeScript:[NSString stringWithFormat:
            @"tell application \"EyeTV\"\nif (count of recordings) > 0 then\nif busy of last recording then\nreturn\nend if\nend if\nset current channel to %d\npause\nend tell", channel]];
    } @catch (NSException *e) {
    }
    channel = 0;
    @try {
        NSAppleEventDescriptor *desc = [AppleScript executeScript:@"tell application \"EyeTV\" to return current channel"];
        channel = [desc int32Value];
    } @catch (NSException *e) {
    }
    NSDictionary *resultContainer = [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithInt:channel], @"currentChannel",
        nil];
    [ResponseHandler writeResponseWithCode:200 content:[resultContainer JSONRepresentation] socket:_asyncSocket timeout:WRITE_BODY_TIMEOUT];
}

@end
