/*
 * ArticleCollectionView.j
 *
 * Created by Philippe Laval on 2009/04/05.
 * 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 ArticleCollectionView : CPCollectionView
{
	CPString baseUrl @accessors;
	
	CPURLConnection articlesConnection;
}

- (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/Programmation/cappuccino/"];
		
		// We will handle the double click and selection change callback ourself
		[self setDelegate:self];
    }
    
    return self;
}

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

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

-(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 articleUrl = [dictionary objectForKey:@"articleUrl"];
			// CPLog("articleUrl: " + articleUrl);
			
			var url = [CPString stringWithFormat:@"%@%@", baseUrl, articleUrl];
			
			// Send a notification with the array of picture's url as parameter
			var userInfo = [CPDictionary dictionaryWithObject:url forKey:@"articleUrl"];
			var notification = [CPNotification notificationWithName:@"ArticleChanged" object:self userInfo:userInfo];	
			[[CPNotificationCenter defaultCenter] postNotification:notification];
		}
	}
}


-(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 == articlesConnection)
	{
		// 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 articleImageUrl = [CPString stringWithFormat:@"%@%@", baseUrl, receivedJsonData[i].image];
			var articleTitle = [CPString stringWithString:receivedJsonData[i].title]
			var articleUrl = [CPString stringWithString:receivedJsonData[i].url]
			// CPLog("articleUrl: " + articleUrl);
			
			var dictionary = [CPDictionary dictionaryWithObject:articleImageUrl forKey:@"articleImageUrl"];
			[dictionary setObject:articleTitle forKey:@"articleTitle"];
			[dictionary setObject:articleUrl forKey:@"articleUrl"];
			
			[array addObject:dictionary];
		}
		
		// Associate the content array with the collection view
		[self setContent:array];
	}
}

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

@end
