//  Shopcat
//
//  Created by Willem Laurentz Middelkoop on 15-01-11.
//  Copyright 2011 WMIT. All rights reserved.


#import "NameCheckResultInterfaceParser.h"
#import "NameCheckResult.h"
#import "NameCheck.h"

static NSSet *interestingKeys;

@implementation NameCheckResultInterfaceParser

@synthesize name;

+ (void)initialize
{
    if (!interestingKeys) {
        interestingKeys = [[NSSet alloc] initWithObjects:@"TestValue",
						   @"IsAvailable",
						   @"NameCheckType",nil];
    }
}

- (void)dealloc
{
	[name release];
	[nameCheck release];
    [items release];
    [super dealloc];
}

- (BOOL)parseData:(NSData *)d 
{
    // Release the old itemArray
    [items release];
	
    // Create a new, empty itemArray
    items = [[NSMutableArray alloc] init];
	
    // Create a parser
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:d];
    [parser setDelegate:self];
	
    // Do the parse
    [parser parse];
	
    [parser release];
	
	nameCheck = [NameCheck alloc];
	nameCheck.name = name;
	
	nameCheck.results = items;
	
	bool isAvailable = YES;
	for(NameCheckResult *result in items){
		if([result.isAvailable isEqual:@"false"]){
			isAvailable = NO;
			break;
		}
	}
	if(isAvailable){
		nameCheck.isAvailable = @"true";
	}else{
		nameCheck.isAvailable = @"false";
	}
	
    NSLog(@"items = %@", items);
    return YES;
}

- (NSArray *)items{
    return items;
}
-(NameCheck *)nameCheck{
	return nameCheck;
}


#pragma mark Delegate calls

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict
{
 //   NSLog(@"starting Element: %@", elementName);
	
    // Is it the start of a new item?
    if ([elementName isEqual:@"NameCheckResult"]) {
		
        // Create a dictionary for the title/url for the item
        resultInProgress = [[NameCheckResult alloc] init];
        return;
    }
	
    // Is it the title/url for the current item?
    if ([interestingKeys containsObject:elementName]) {
        keyInProgress = [elementName copy];
        // This is a string we will append to as the text arrives
        textInProgress = [[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
{
//    NSLog(@"ending Element: %@", elementName);
	
    // Is the current item complete?
    if ([elementName isEqual:@"NameCheckResult"]) {
        [items addObject:resultInProgress];
		
        // Clear the current item
        [resultInProgress release];
        resultInProgress = nil;
        return;
    }
	
    // Is the current key complete?
    if ([elementName isEqual:keyInProgress]) {
        if ([elementName isEqual:@"TestValue"]) {
			resultInProgress.testValue = textInProgress;
        } else if ([elementName isEqual:@"IsAvailable"]) {
			resultInProgress.isAvailable = textInProgress;
        }else if ([elementName isEqual:@"NameCheckType"]) {
			resultInProgress.nameCheckType = textInProgress;
		}
		
		
        // Clear the text and key
        [textInProgress release];
        textInProgress = nil;
        [keyInProgress release];
        keyInProgress = nil;
    }
}

// This method can get called multiple times for the
// text in a single element
- (void)parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string
{
    [textInProgress appendString:string];
}


@end
