//
//  ImageBrowserArrayController.m
//
//  Created by Frank Mcpherson on 12/13/09.
//  Copyright 2009 bitdoctors.com. All rights reserved.
//

#import "ImageBrowserArrayController.h"
#import "Artwork.h"

@implementation ImageBrowserArrayController

@synthesize browserData;

- (void)awakeFromNib
{	
	browserData = [[NSMutableArray alloc] initWithCapacity:10];
	
	//Browser UI setup (can also be set in IB)
	[browserView setAllowsReordering:YES];
	[browserView setAllowsMultipleSelection:NO];
	[browserView setDelegate:self];
	[browserView setDataSource:self];
	[browserView setDraggingDestinationDelegate:self];
	[browserView setCellsStyleMask:IKCellsStyleShadowed|IKCellsStyleOutlined|IKCellsStyleTitled|IKCellsStyleSubtitled];
	
	// Stuff that's not in the NIB
	contextMenu = [[NSMenu alloc] initWithTitle:@"a menu"];
	
	[[contextMenu addItemWithTitle:@"Autofill Artwork Details for Testing" action:@selector(autofill:) keyEquivalent:@""] setTarget:self];
}

- (IBAction) autofill:(id)sender
{
	int testSequence = 0;
	for (Artwork *a in browserData) {
		[a setChineseTitle:@"陰陽 Yin Yang"];
		[a setCost:@"$450 US"];
		[a setMedium:@"Chinese Ink & Color on Xuan Paper"];
		[a setWesternTitle:@"Yin and Yang"];
		[a setScript:@"Brushed in Oracle Bone & Seal Scripts"];
		[a setInventoryCode:[NSString stringWithFormat:@"Test-%d", testSequence++]];
		[a setSize:@"H.15 x W.12\""];
	}
	[browserView reloadData];
	NSLog(@"Autofill, yo");
}

- (void) imageBrowserSelectionDidChange:(IKImageBrowserView *) aBrowser
{
	[browserArrayController setSelectionIndexes:[browserView selectionIndexes]];
}

- (void) imageBrowser:(IKImageBrowserView *) aBrowser
	backgroundWasRightClickedWithEvent:(NSEvent *) event
{
	[NSMenu popUpContextMenu:contextMenu withEvent:event forView:aBrowser];	
}

#pragma mark - 
#pragma mark Browser Data Source Methods


- (NSUInteger) numberOfItemsInImageBrowser:(IKImageBrowserView *) aBrowser
{	
	return [browserData count];
}

- (id) imageBrowser:(IKImageBrowserView *) aBrowser itemAtIndex:(NSUInteger)index
{
	return [browserData objectAtIndex:index];
}


- (BOOL) imageBrowser:(IKImageBrowserView *)aBrowser moveItemsAtIndexes: (NSIndexSet *)selected toIndex:(NSUInteger)destinationIndex 
{	
	NSArray *objectsToMove = [browserData objectsAtIndexes: selected];
	
	//If any of the removed objects come before the index, we want to decrement the index appropriately
	destinationIndex -= [selected countOfIndexesInRange: (NSRange){0, destinationIndex}];
	[browserData removeObjectsAtIndexes:selected];
	
	[browserData replaceObjectsInRange:(NSRange){destinationIndex,0} withObjectsFromArray:objectsToMove];
	return NO;
}


- (void)imageBrowser:(IKImageBrowserView *)aBrowser removeItemsAtIndexes:(NSIndexSet *)indexes 
{
	[browserArrayController removeObjectsAtArrangedObjectIndexes:indexes];
}



#pragma mark - 
#pragma mark Browser Drag and Drop Methods
- (NSUInteger)draggingEntered:(id <NSDraggingInfo>)sender
{
	
	if( ([sender draggingSource] != self) && ([sender draggingSource] != browserView)){
		NSPasteboard *pb = [sender draggingPasteboard];
		NSString * type = [pb availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]];
		
		if(type != nil){
			return NSDragOperationEvery;
		}
	}
	
	return NSDragOperationNone;
}

- (NSUInteger)draggingUpdated:(id <NSDraggingInfo>)sender
{
	return NSDragOperationEvery;
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
	return YES;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
	
	//Get the files from the drop
	NSArray * files = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType];
		
	// Remove the entries if we're also the dragging source
	if ([sender draggingSource] == browserView) {
		[browserArrayController removeObjectsAtArrangedObjectIndexes:[browserView selectionIndexes]];
	}
	
	int draggedCount = [files count];
	// Counting backwards to preserve the order from the drag source
	for (int i=draggedCount-1;i>=0;i--) {
		Artwork *newArtwork = [[Artwork alloc] initWithImageRepresentation:[files objectAtIndex:i]];
		// Do I have to observe the values for the title and subtitle keys in order to make the browser reflect the changes?
		
		[browserArrayController insertObject:newArtwork atArrangedObjectIndex:[browserView indexAtLocationOfDroppedItem]];
	}
	if ([browserData count] > 0) return YES;
	return NO;
}

- (void)concludeDragOperation:(id < NSDraggingInfo >)sender
{
	[browserView reloadData];
}


// TODO fhm Observe the properties of each Artwork so this can be FALSE before all the data is entered, but true when it has
- (BOOL) dataEntered
{
	return true;
}

- (void)dealloc
{
	[contextMenu dealloc];
	[super dealloc];
}
@end
