//
//  Service.m
//  LostClient
//
//  Created by Webster Ross III on 4/2/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Service.h"
#import "Constants.h"
#import "FindServicesRequest.h"
#import "FindServicesParser.h"

@implementation Service
@synthesize name, urn, cachedResults, results, resultsRadius;

- (id)initWithName:(NSString *)serviceName urn:(NSString *)serviceURN {
	if (self = [super init]) {
		name = serviceName;
		urn = serviceURN;
		lostClient = [[LostClient lostClientWithServerURL:LOST_CLIENT_SERVER_URL]retain];
		lostClient.delegate = self;
		cachedResults = nil;
		//results = nil;
		resultsRadius = 0;
	}
	return self;
}

+ (id)serviceWithName:(NSString *)serviceName urn:(NSString *)serviceURN {
	return [[[Service alloc] initWithName:serviceName urn:serviceURN] autorelease];
}

- (void)cacheResults:(CLLocation *)location wait:(int)seconds {
	cacheLocation = [location retain];
	[NSTimer scheduledTimerWithTimeInterval:seconds
									 target:self selector:@selector(sendCacheRequest:)
								   userInfo:nil repeats:NO];
}

- (void)sendCacheRequest:(NSTimer*)theTimer {
	FindServicesRequest *request = [FindServicesRequest requestWithLocation:cacheLocation service:urn searchRadius:LOST_VALUE_UNLIMITED maxResults:10 directed:NO];
	[lostClient sendRequest:request];
}

- (void)lostResponse:(NSData *)response {
	cachedResults = [response retain];
	FindServicesParser *parser = [FindServicesParser parserWithData:response];
	parser.searchLocation = cacheLocation;
	parser.delegate = self;
	[parser parseData];
}

- (void)lostResponseError:(NSError *)error {
}

- (void)parserResults:(NSMutableArray *)results {
}

- (void)parserError:(NSError *)error {
	cachedResults = nil;
}

- (void)dealloc {
	[urn release];
	[name release];
	[cachedResults release];
	[lostClient release];
	[cacheLocation release];
    [super dealloc];
}

@end
