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

#import "FindServicesRequest.h"
#import "Constants.h"
#import <CoreLocation/CoreLocation.h>


@implementation FindServicesRequest

@synthesize searchRadius, maxResults, serviceURN, location, isDirectedSearch;

- (id) initWithLocation:(CLLocation *)theLocation service:(NSString *)urn searchRadius:(double)radius maxResults:(int)max directed:(BOOL)directed{
	self.location = theLocation;
	self.searchRadius = radius;
	self.maxResults = max;
	self.serviceURN = urn;
	self.isDirectedSearch = directed;
	return self;	
}

+ (id) requestWithLocation:(CLLocation *)theLocation service:(NSString *)urn searchRadius:(double)radius maxResults:(int)max directed:(BOOL)directed{
	return [[[FindServicesRequest alloc] initWithLocation:theLocation service:urn searchRadius:radius maxResults:max directed:directed]autorelease];	
}

- (NSString *)buildXML {
    
    
    if(searchRadius == LOST_VALUE_UNLIMITED && maxResults == LOST_VALUE_UNLIMITED) {
		maxResults = 100;
		searchRadius = 8;
	}
	
	if(searchRadius == LOST_VALUE_UNLIMITED && maxResults != LOST_VALUE_UNLIMITED) {
		searchRadius = 8;
	}
	
	if(searchRadius != LOST_VALUE_UNLIMITED && maxResults == LOST_VALUE_UNLIMITED) {
		maxResults = 100;
	}

    //NSLog(@"Search Radius: %f", searchRadius);
	NSString *query = LOST_QUERY;
	
    double course = location.course;
    NSLog(@"COURSE: %f",course);
    //course = 90;
    
	if(isDirectedSearch && course > 0) {
		NSLog(@"DIRECTED");
        query = LOST_DQUERY;
        
        query = [query stringByReplacingOccurrencesOfString:@"NMAX" withString:[NSString stringWithFormat:@"%i",maxResults]];
        query = [query stringByReplacingOccurrencesOfString:@"SERVICE" withString:serviceURN];
        query = [query stringByReplacingOccurrencesOfString:@"ORIENT" withString:[NSString stringWithFormat:@"%.2f", course]];        
        query = [query stringByReplacingOccurrencesOfString:@"XMAX" withString:[NSString stringWithFormat:@"%.2f",searchRadius]];
        query = [query stringByReplacingOccurrencesOfString:@"COORDS2" withString:[NSString stringWithFormat:@"%f %f",location.coordinate.latitude, location.coordinate.longitude]];
        
        
        double oldLat = location.coordinate.latitude * M_PI/180;
        double oldLon = location.coordinate.longitude * M_PI/180;
        double R = 6371;
        double d = searchRadius / 2;
        
        double newLat = asin(sin(oldLat)*cos(d/R) + cos(oldLat)*sin(d/R)*cos(course));
        
        //double newLon = oldLon + atan2((sin(location.course)*sin(d/R)*cos(oldLat)), (cos(d/R)−sin(oldLat)*sin(newLat)));
        double newLon = oldLon + atan2(sin(course) * sin(d/R) * cos(oldLat), cos(d/R) - sin(oldLat) * sin(newLat));

        
        CLLocation *loc = [[CLLocation alloc] initWithLatitude:newLat * 180/M_PI longitude:newLon * 180/M_PI];
        //CLLocation *loc = [[CLLocation alloc] initWithLatitude:newLat longitude:newLon];
        
        query = [query stringByReplacingOccurrencesOfString:@"COORDS" withString:[NSString stringWithFormat:@"%f %f",loc.coordinate.latitude, loc.coordinate.longitude]];
        
        [loc release];
        NSLog(query);
        

    }
    
    else {
        query = [query stringByReplacingOccurrencesOfString:@"NMAX" withString:[NSString stringWithFormat:@"%i",maxResults]];
        query = [query stringByReplacingOccurrencesOfString:@"XMAX" withString:[NSString stringWithFormat:@"%.2f",searchRadius]];
        query = [query stringByReplacingOccurrencesOfString:@"COORDS" withString:[NSString stringWithFormat:@"%f %f",location.coordinate.latitude,location.coordinate.longitude]];
        query = [query stringByReplacingOccurrencesOfString:@"SERVICE" withString:serviceURN];   
    }
    
    //NSLog(query);
	return query;
}

- (void)dealloc {
	[serviceURN release];
	[location release];
    [super dealloc];
}

@end
