//
//  XMLParser.m
//  mTlu
//
//  Created by Developer on 3/21/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "XMLParser.h"
#import "Course.h"

@implementation XMLParser


-(void) parseXML:(NSDate *)date withRoom:(NSString*) room{
    NSURL *url;
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
    NSDateComponents *today=[[NSCalendar currentCalendar] components:unitFlags fromDate:[NSDate date]];
    NSDateComponents *givenDate=[[NSCalendar currentCalendar] components:unitFlags fromDate:date];
    if ([today day]==[givenDate day] && [today month]==[givenDate month] && [today year] == [givenDate year])
        url = [[NSURL alloc] initWithString: [NSString stringWithFormat:@"http://greeny.cs.tlu.ee/~cosmin/room-information?room=%@&format=xml",room]];
    else 
        url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://greeny.cs.tlu.ee/~cosmin/room-information?room=%@&format=xml&year=%d&month=%ld&day=%d",room,[givenDate year],[givenDate month],[givenDate day]]];
    NSData *data=[[NSData alloc] initWithContentsOfURL:url];
    parser=[[NSXMLParser alloc] initWithData:data];
    previousClasses=[[NSMutableArray alloc] init];
    followingClasses=[[NSMutableArray alloc] init];
    addToPrevious=true;
    [parser setDelegate:self];
    [parser parse];
}

-(NSMutableArray*) getPreviousClasses
{
    return previousClasses;
}

-(NSMutableArray*) getFollowingClasses
{
    return followingClasses;
}

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"following"])
        addToPrevious=false;
    if ([elementName isEqualToString:@"course"])
        currentCourse=[[Course alloc] init];
}

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    currentElementContent=[currentElementContent stringByAppendingString:string];
}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"courseId"])
        currentCourse.idCourse=[currentElementContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([elementName isEqualToString:@"title"]) 
        currentCourse.title=[currentElementContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([elementName isEqualToString:@"teacher"])
        currentCourse.teacher=[currentElementContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([elementName isEqualToString:@"starttime"])
        currentCourse.starthour=[currentElementContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([elementName isEqualToString:@"endtime"])
        currentCourse.endhour=[currentElementContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([elementName isEqualToString:@"duration"])
        currentCourse.duration=[currentElementContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([elementName isEqualToString:@"group"]) 
        currentCourse.group=[currentElementContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([elementName isEqualToString:@"course"])
        if (addToPrevious)
            [previousClasses addObject:currentCourse];
        else 
            [followingClasses addObject:currentCourse]; 
    currentElementContent = [[NSString alloc] initWithString:@""];  
}
@end
