/*
 * CategoryCollectionView.j
 *
 * Created by Philippe Laval on 2009/03/21.
 * Copyright 2009 Philippe Laval. All rights reserved.
 */

@import <Foundation/CPObject.j>
@import <Foundation/CPURLResponse.j>
@import <Foundation/CPURLRequest.j>
@import <Foundation/CPURLConnection.j>


@import <AppKit/CPCollectionView.j>


@import "CPString_JSON.j"


@implementation CategoryCollectionView : CPCollectionView
{
	CPString baseUrl @accessors;
	
	CPURLConnection categoriesConnection;
	CPURLConnection picturesConnection;
}

- (id) initWithFrame:(CGRect) aFrame
{
    self = [super initWithFrame:aFrame];
    if (self)
    {
		// It will be easier to change this for production
		// baseUrl = [CPString stringWithString:@"http://macbookpro.local/~philippe/"];
		baseUrl = [CPString stringWithString:@"http://www.nice-panorama.com/Biot/Fete-Templier-Biot-2009/SlideShowBuilder/"];
		
		// We will handle the double click and selection change callback ourself
		[self setDelegate:self];
    }
    
    return self;
}

-(void) LoadCategories
{
	var url = [CPString stringWithFormat:@"%@%@", baseUrl, @"categories.php"];
	var request = [[CPURLRequest alloc] initWithURL:url]; 
	[request setHTTPMethod:@"GET"]; 

	categoriesConnection = [CPURLConnection connectionWithRequest:request delegate:self]; 
}

// CPCollectionView delegate
// index start at 0
-(void)collectionView:(CPCollectionView)collectionView didDoubleClickOnItemAtIndex:(int)index
{
	if (collectionView == self)
	{
		// debugger;
		// Show a slide show with all the pictures in the category
		
		
	}
}

-(void)collectionViewDidChangeSelection:(CPCollectionView)collectionView
{
	if (collectionView == self)
	{
		//debugger;
		// Download the list of pictures in this category
		// Inform with a notification that ths list of picture is available
		
		var selectedItems = [[self items] objectsAtIndexes:[self selectionIndexes]];
		var enumerator = [selectedItems objectEnumerator];
		
		var selectedItem;
		while (selectedItem = [enumerator nextObject])
		{
			var dictionary = [selectedItem representedObject];
			
			var jsonUrl = [dictionary objectForKey:@"jsonUrl"];
			// CPLog("jsonUrl: " + jsonUrl);
			
			var url = [CPString stringWithFormat:@"%@%@", baseUrl, jsonUrl];
			var request = [[CPURLRequest alloc] initWithURL:url]; 
			[request setHTTPMethod:@"GET"]; 

			picturesConnection = [CPURLConnection connectionWithRequest:request delegate:self]; 
		}
	}
}


-(void)connection:(CPURLConnection)connection didFailWithError:(id)error
{
	//debugger;
	alert(error);
}


-(void)connection:(CPURLConnection)connection didReceiveResponse:(CPHTTPURLResponse)response
{
	// debugger;
	// var statusCode = [response statusCode];
}

-(void)connection:(CPURLConnection)connection didReceiveData:(CPString)data
{
	
	if (connection == categoriesConnection)
	{
		// Create a javascript object from the JSON content 
		var receivedJsonData = [data objectFromJSON];
				
		// Populate an array with the full url to the pictures for this category
		var array = [CPArray arrayWithCapacity:receivedJsonData.length];

		for(var i=0; i<receivedJsonData.length; i++) 
		{
			// var imagePath = [CPString stringWithFormat:@"%@%@", baseUrl, receivedJsonData[i].imagePath];
			var imagePath = [CPString stringWithFormat:@"%@", receivedJsonData[i].imagePath];
			// CPLog("imagePath: " + imagePath);
			var title = [CPString stringWithString:receivedJsonData[i].title]
			var subtitle = [CPString stringWithString:receivedJsonData[i].subtitle]
			var jsonUrl = [CPString stringWithString:receivedJsonData[i].jsonUrl]
			
			var dictionary = [CPDictionary dictionaryWithObject:imagePath forKey:@"imagePath"];
			[dictionary setObject:title forKey:@"title"];
			[dictionary setObject:subtitle forKey:@"subtitle"];
			[dictionary setObject:jsonUrl forKey:@"jsonUrl"];
			
			[array addObject:dictionary];
		}
		
		// Associate the content array with the collection view
		[self setContent:array];
	}
	else if (connection == picturesConnection)
	{
		// Create a javascript object from the JSON content 
		var receivedJsonData = [data objectFromJSON];
	
		// Populate an array with the full url to the pictures for this category
		var picturesArray = [CPArray arrayWithCapacity:receivedJsonData.length];
	
		// receivedJsonData is an array containing dictionaries with "url" and "smallUrl" keys
		for(var i=0; i<receivedJsonData.length; i++) 
		{
			//var url = [CPString stringWithFormat:@"%@%@", baseUrl, receivedJsonData[i].url];
			//var smallUrl = [CPString stringWithFormat:@"%@%@", baseUrl, receivedJsonData[i].smallUrl];
			var url = [CPString stringWithFormat:@"%@", receivedJsonData[i].url];
			var smallUrl = [CPString stringWithFormat:@"%@", receivedJsonData[i].smallUrl];
			// CPLog("url: " + url);
			// CPLog("smallUrl: " + smallUrl);
			var pictureDict = [CPDictionary dictionaryWithObject:url forKey:@"url"];
			[pictureDict setObject:smallUrl forKey:@"smallUrl"];
			
			[picturesArray addObject:pictureDict];
		}
	
		// Send a notification with the array of picture's url as parameter
		var userInfo = [CPDictionary dictionaryWithObject:picturesArray forKey:@"pictures"];
		var notification = [CPNotification notificationWithName:@"CategoryChanged" object:self userInfo:userInfo];	
		[[CPNotificationCenter defaultCenter] postNotification:notification];
	}
}

-(void)connectionDidFinishLoading:(CPURLConnection)connection
{
	//debugger;
}

// var applicationDelegate = [[CPApplication sharedApplication] delegate];


@end
