#import "PopupMenuItem.h"

@interface PopupMenuItem ()

+ (UIFont *)fontForTitle;
+ (UIFont *)fontForTitleWithImage;

- (id)initWithTitle:(NSString *)title image:(UIImage *)image customView:(UIView *)customView target:(id)target action:(SEL)action;

@end

@implementation PopupMenuItem

+ (UIFont *)fontForTitle
{
    return [UIFont boldSystemFontOfSize:17];
}

+ (UIFont *)fontForTitleWithImage
{
    return [UIFont boldSystemFontOfSize:12];
}

+ (id)itemWithTitle:(NSString *)title target:(id)target action:(SEL)action
{
   // self = [[[self alloc] initWithTitle:title target:target action:action] autorelease];
    return [[self alloc] initWithTitle:title target:target action:action];
    //return self;
}

+ (id)itemWithImage:(UIImage *)image target:(id)target action:(SEL)action
{
    //self = [[[self alloc] initWithImage:image target:target action:action] autorelease];
    
    //return self;
    return [[self alloc] initWithImage:image target:target action:action];
}

+ (id)itemWithTitle:(NSString *)title image:(UIImage *)image target:(id)target action:(SEL)action
{
    /*
    self = [[[self alloc] initWithTitle:title image:image target:target action:action] autorelease];
    return self;
     */
    return [[self alloc] initWithTitle:title image:image target:target action:action];
}

+ (id)itemWithCustomView:(UIView *)customView target:(id)target action:(SEL)action
{
    /*self = [[[self alloc] initWithCustomView:customView target:target action:action] autorelease];
    return self;*/
    return [[self alloc] initWithCustomView:customView target:target action:action];
}

- (id)initWithTitle:(NSString *)title target:(id)target action:(SEL)action
{
    self = [self initWithTitle:title image:nil customView:nil target:target action:action];
    
    return self;
}

- (id)initWithImage:(UIImage *)image target:(id)target action:(SEL)action
{
    self = [self initWithTitle:nil image:image customView:nil target:target action:action];
    
    return self;
}

- (id)initWithTitle:(NSString *)title image:(UIImage *)image target:(id)target action:(SEL)action
{
    self = [self initWithTitle:title image:image customView:nil target:target action:action];
    
    return self;
}

- (id)initWithCustomView:(UIView *)customView target:(id)target action:(SEL)action
{
    self = [self initWithTitle:nil image:nil customView:customView target:target action:action];
    
    return self;
}

- (id)initWithTitle:(NSString *)title image:(UIImage *)image customView:(UIView *)customView target:(id)target action:(SEL)action
{
    self = [super init];
    
    if(self) {
        self.title = title;
        self.image = image;
        self.customView = customView;
        self.target = target;
        self.action = action;
        
        self.enabled = YES;
        self.width = 0;
        self.font = [UIFont systemFontOfSize:10];
    }
    
    return self;
}

- (void)dealloc
{
    /*
    [_title release];
    [_image release];
    [_customView release];
    
    [_font release];
    
    [super dealloc];
     */
}


#pragma mark -

- (CGSize)actualSize
{
    CGFloat width = 0, height = 0;
    
    if(self.customView) {
        width = self.customView.frame.size.width;
        height = self.customView.frame.size.height;
    } else {
        if(self.width > 0) {
            width = self.width;
            height = 50;
        } else {
            height = 50;
            
            if(self.title && self.image) {
                UIFont *font = (self.font) ? self.font : [PopupMenuItem fontForTitleWithImage];
                
                CGSize titleSize = [self.title sizeWithFont:font];
                CGSize imageSize = self.image.size;
                
                if(titleSize.width > imageSize.width) {
                    width = titleSize.width;
                } else {
                    width = imageSize.width;
                }
            } else if(self.title) {
                UIFont *font = (self.font) ? self.font : [PopupMenuItem fontForTitle];
                
                CGSize titleSize = [self.title sizeWithFont:font];
                width = titleSize.width;
            } else if(self.image) {
                width = self.image.size.width;
            }
            width = width + ([[self actualFont] pointSize] - 4) * 2;
        }
    }
    
    return CGSizeMake(width, height);
}

- (UIFont *)actualFont
{
    UIFont *font = self.font;
    
    if(font == nil) {
        if(self.title && self.image) {
            font = [PopupMenuItem fontForTitleWithImage];
        } else {
            font = [PopupMenuItem fontForTitle];
        }
    }
    
    return font;
}

- (void)performAction
{
    if([self.target respondsToSelector:self.action]) {
        [self.target performSelectorOnMainThread:self.action withObject:self waitUntilDone:YES];
    }
}

@end
