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

@import <Foundation/CPObject.j>

CategoryCellDragType = @"CategoryCellDragType";

@implementation CategoryCell : CPView
{
    CPImage         image;
	CPTextField		titleTextField;
	CPTextField		subtitleTextField;
    CPImageView     imageView;
    CPView          highlightView;
    
	CPDictionary	dictionary @accessors;
}

- (void)setRepresentedObject:(CPObject)anObject
{
	var cellBounds = [self bounds];
	
	// Associate the dictionary with the CategoryCell instance
    dictionary = anObject;
	var imagePath = [dictionary objectForKey:@"imagePath"];
	var title = [dictionary objectForKey:@"title"];
	var subtitle = [dictionary objectForKey:@"subtitle"];
	
	// imageView
	// 4 pixels
	// title (20 pixels)
	// 4 pixels
	// subtitle (20 pixels)
	// 4 pixels
    if(!imageView)
    {
        imageView = [[CPImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(cellBounds), CGRectGetHeight(cellBounds) - 52)];
 //       [imageView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
        [imageView setImageScaling:CPScaleProportionally];
        [imageView setHasShadow:YES];
        [self addSubview:imageView];
    }
	
	if (!titleTextField)
	{
		var y = CGRectGetHeight(cellBounds) - 48;
		
        titleTextField = [[CPTextField alloc] initWithFrame:CGRectMake(0, y, CGRectGetWidth(cellBounds), 20)];
		[titleTextField setAlignment:CPCenterTextAlignment]
		[titleTextField setFont:[CPFont boldSystemFontOfSize:12.0]];
		[titleTextField setAutoresizingMask:CPViewWidthSizable];
        [self addSubview:titleTextField positioned:CPWindowAbove relativeTo:imageView];
	}

	if (!subtitleTextField)
	{
		var y = CGRectGetHeight(cellBounds) - 24;
		
        subtitleTextField = [[CPTextField alloc] initWithFrame:CGRectMake(0, y, CGRectGetWidth(cellBounds), 20)];
		[subtitleTextField setAlignment:CPCenterTextAlignment]
		[subtitleTextField setFont:[CPFont systemFontOfSize:12.0]];
		[subtitleTextField setAutoresizingMask:CPViewWidthSizable];
        [self addSubview:subtitleTextField positioned:CPWindowAbove relativeTo:imageView];
	}   

	[titleTextField setStringValue:title];
	[subtitleTextField setStringValue:subtitle];
   
    [image setDelegate:nil];
    
	// Load the image
    image = [[CPImage alloc] initWithContentsOfFile:imagePath];
    [image setDelegate:self];
    
	// Verify that the image is loaded. 
	// If it is not, it will be associated with the imageView by the image delegate method imageDidLoad:
    if([image loadStatus] == CPImageLoadStatusCompleted)
        [imageView setImage:image];
    else
        [imageView setImage:nil];
}

// Called since we have marked ouself to be the image delegate
- (void)imageDidLoad:(CPImage)anImage
{
	//debugger;
    [imageView setImage:anImage];
}

- (void)setSelected:(BOOL)flag
{
    if(!highlightView)
    {
        highlightView = [[CPView alloc] initWithFrame:CGRectCreateCopy([self bounds])];
        [highlightView setBackgroundColor:[CPColor colorWithCalibratedWhite:0.8 alpha:0.6]];
    }

    if(flag)
    {
        // [highlightView setFrame:CGRectInset([imageView imageRect], -10.0, -10.0)];
        [self addSubview:highlightView positioned:CPWindowBelow relativeTo:imageView];
    }
    else
        [highlightView removeFromSuperview];
}


@end