/*
 * ImagetteCell.j
 *
 * Created by Philippe Laval on 2009/04/10.
 * Copyright 2009 Philippe Laval. All rights reserved.
 */

@import <Foundation/CPObject.j>

ImagetteCellDragType = @"ImagetteCellDragType";

@implementation ImagetteCell : CPView
{
    CPImage         imagette;
    CPImageView     imagetteView;
    CPView          highlightView;
    
    CPString        imagettePath @accessors;
    CPString        imagePath @accessors;
}

// anObject is a dictionary containing the "url" and "smallUrl" keys
- (void)setRepresentedObject:(CPObject)anObject
{
    if(!imagetteView)
    {
        imagetteView = [[CPImageView alloc] initWithFrame:CGRectMakeCopy([self bounds])];
        [imagetteView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
        [imagetteView setImageScaling:CPScaleProportionally];
        [imagetteView setHasShadow:YES];
        [self addSubview:imagetteView];
    }

    
    imagePath = [anObject objectForKey:@"url"];
    imagettePath = [anObject objectForKey:@"smallUrl"];
    
    [imagette setDelegate:nil];
    
    imagette = [[CPImage alloc] initWithContentsOfFile:imagettePath];
    [imagette setDelegate:self];
    
    if([imagette loadStatus] == CPImageLoadStatusCompleted)
        [imagetteView setImage:imagette];
    else
        [imagetteView setImage:nil];
}

- (void)imageDidLoad:(CPImage)anImagette
{
    [imagetteView setImage:anImagette];
}

- (void)setSelected:(BOOL)flag
{
	// This is a lazy creation : we postpone the creation of the highlight view since
	// the user may never want to select the picture (this allows to create less views)
	if(!highlightView)
    {
        highlightView = [[CPView alloc] initWithFrame:CGRectCreateCopy([self bounds])];
		// This will allow the highlight to be scaled as well (if displayed)
        [highlightView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
        [highlightView setBackgroundColor:[CPColor colorWithCalibratedWhite:0.8 alpha:0.6]];
    }
    else
	{
		// Since we allow the photo cell to be scaled after creation,
		// we have to be sure to update the size (it will not be autoresized when removed from superview)
		[highlightView setFrame:CGRectCreateCopy([self bounds])]
	}

    if(flag)
    {
        // [highlightView setFrame:CGRectInset([imagetteView imageRect], -10.0, -10.0)];
        [self addSubview:highlightView positioned:CPWindowBelow relativeTo:imagetteView];
    }
    else
	{
		// We remove the hightlight from the view hierarchy
        [highlightView removeFromSuperview];
	}
}


@end