//
//  MPFindResult.m
//  VirtualEarthKit
//
//  Created by Colin Cornaby on 11/20/08.
//  Copyright 2008 Consonance Software. All rights reserved.
//

#import "MPFindResult.h"
#import "VELocationUtilities.h"


@implementation MPFindResult

@synthesize properties = m_properties;
@synthesize location = m_location;

-(NSDictionary *)dictionaryFromMapPointPropertyNode:(xmlNodePtr)node
{
	NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
	xmlNodePtr currentNode = nil;
	for(currentNode = node->children; currentNode; currentNode = currentNode->next)
	{
		NSString *propName = nil;
		id prop = nil;
		xmlNodePtr currentPropertyNode = nil;
		for(currentPropertyNode = currentNode->children; currentPropertyNode; currentPropertyNode = currentPropertyNode->next)
		{
			if(!strcmp((char *) currentPropertyNode->name, "Name"))
				propName = [NSString stringWithCString:(char *)xmlNodeGetContent(currentPropertyNode) encoding:NSASCIIStringEncoding];
			if(!strcmp((char *)currentPropertyNode->name, "Value"))
			{
				if(!strcmp((char *)xmlGetProp(currentPropertyNode, BAD_CAST "type"), "xsd:string"))
					prop = [[NSString alloc] initWithCString:(char *)xmlNodeGetContent(currentPropertyNode) encoding:NSASCIIStringEncoding];
			}
		}
		if(prop)
		{
			[dictionary setObject:prop forKey:propName];
			[prop release];
		}
	}
	return [dictionary autorelease];
}

-(id)initWithContentsNode:(xmlNodePtr)node
{
	self = [super init];
	xmlNodePtr currentNode = nil;
	for(currentNode = node->children; currentNode; currentNode = currentNode->next)
	{
		if(!strcmp("FoundLocation", (char *)currentNode->name))
		{
			xmlNodePtr currentLocNode = nil;
			for(currentLocNode = currentNode->children; currentLocNode; currentLocNode = currentLocNode->next)
			{
				if(!strcmp((char *)currentLocNode->name, "LatLong"))
				{
					double lat, longitude, alt;
					xmlNodePtr currentLatLongNode = nil;
					for(currentLatLongNode = currentLocNode->children; currentLatLongNode; currentLatLongNode = currentLatLongNode->next)
					{
						if(!strcmp((char *)currentLatLongNode->name, "Latitude"))
							lat = [[NSString stringWithCString:(char *)xmlNodeGetContent(currentLatLongNode) encoding:NSASCIIStringEncoding] floatValue];
						if(!strcmp((char *)currentLatLongNode->name, "Longitude"))
							longitude = [[NSString stringWithCString:(char *)xmlNodeGetContent(currentLatLongNode) encoding:NSASCIIStringEncoding] floatValue];
						if(!strcmp((char *)currentLatLongNode->name, "Altitude"))
							alt = [[NSString stringWithCString:(char *)xmlNodeGetContent(currentLatLongNode) encoding:NSASCIIStringEncoding] floatValue];
					}
					CLLocationCoordinate2D locationStruct;
					locationStruct.latitude = lat;
					locationStruct.longitude = longitude;
					m_location = [[CLLocation alloc] initWithCoordinate:locationStruct altitude:alt horizontalAccuracy:0 verticalAccuracy:-1 timestamp:[NSDate dateWithTimeIntervalSinceNow:0]];
				}
				if(!strcmp((char *)currentLocNode->name, "Entity"))
					currentLocNode = currentLocNode->children;
				if(!strcmp((char *)currentLocNode->name, "Properties"))
				   m_properties = [[self dictionaryFromMapPointPropertyNode:currentLocNode] retain];
			}
		}
	}
	return self;
}

@end
