/*
 iPhly: Radio Control on the iPhone
 
 Copyright (C) 2010-2011 Ari Krupnik & Associates
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License V2
 as published by the Free Software Foundation.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#import "MMSlider.h"

#define LABEL_HEIGHT 21

@implementation SliderCell

- (id)initWithMin:(float)n andMax:(float)x {
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    if (self) {
        CGRect sliderFrame = [[self contentView] frame];
        sliderFrame.size.height = [self height] -10;
        sliderFrame.size.width -= 30;
        sliderFrame.origin.x += 5;
        sliderFrame.origin.y += 5;
        slider=[self newSliderWithFrame:sliderFrame min:n max:x];
        [[self contentView] addSubview:slider];
        
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    return self;
}

- (UIView*) newSliderWithFrame:(CGRect)frame min:(float)n max:(float)x {
    // override in subclass
    return nil;
}

- (UILabel*)textLabel {
    if (!label) {
        label = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, [self frame].size.width-30, LABEL_HEIGHT)];
        [label setTextAlignment:UITextAlignmentCenter];
        [label setBackgroundColor:[UIColor clearColor]];
        [[self contentView] addSubview:label];
        CGRect sliderFrame=[slider frame];
        sliderFrame.origin.y+=LABEL_HEIGHT;
        [slider setFrame:sliderFrame];
    }
    return label;
}

- (CGFloat) _heightWithoutLabel {
    // this is the height of plain-vanilla UITableViewCell
    return 44;
}

- (CGFloat) height {
    float f = [self _heightWithoutLabel];
    if (label) {
        f+=LABEL_HEIGHT;
    }
    return f;
}

- (void)dealloc {
    [slider release];
    [label release];
    [super dealloc];
}

@end


@implementation MMTableCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    return [self initWithMin:0 andMax:1];
}

- (UIView*) newSliderWithFrame:(CGRect)sliderFrame min:(float)n max:(float)x {
    return [[MMSlider alloc] initWithFrame:sliderFrame min:n max:x];
}

- (MMSlider*) slider {
    return ((MMSlider*) slider);
}

- (CGFloat) _heightWithoutLabel {
    return 75;
}

@end

@implementation UISliderCell

@synthesize leftLabel;
@synthesize rightLabel;

- (void) updateValueLabel {
    [valueLabel setText:format([[self slider] value])];
}
- (void) thumbDown {
    [valueLabel setAlpha:ACTIVE_ALPHA];
}
- (void) thumbUp {
    [valueLabel setAlpha:INACTIVE_ALPHA];
}

- (id)initWithMin:(float)n andMax:(float)x {
    self = [super initWithMin:n andMax:x];
    if (self) {
        CGRect f = [[self contentView] frame];
        f.size.height = 20;
        f.size.width -= 30;
        f.origin.x += 5;
        f.origin.y = 0;
        valueLabel=[[UILabel alloc] initWithFrame:f];
        [valueLabel setTextAlignment:UITextAlignmentCenter];
        [valueLabel setText:@"value"];
        [valueLabel setBackgroundColor:[UIColor clearColor]];
        [valueLabel setAlpha:INACTIVE_ALPHA];
        [self setFormat:defaultFormat];
        [[self slider] addTarget:self action:@selector(updateValueLabel) forControlEvents:UIControlEventValueChanged];
        [[self slider] addTarget:self action:@selector(thumbDown) forControlEvents:UIControlEventTouchDown];
        [[self slider] addTarget:self action:@selector(thumbUp) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
        [[self contentView] addSubview:valueLabel];
        
        leftLabel = [[UILabel alloc] initWithFrame:f];
        [leftLabel setTextAlignment:UITextAlignmentLeft];
        [leftLabel setBackgroundColor:[UIColor clearColor]];
        [leftLabel setAlpha:INACTIVE_ALPHA];
        [[self contentView] addSubview:leftLabel];
        
        rightLabel = [[UILabel alloc] initWithFrame:f];
        [rightLabel setTextAlignment:UITextAlignmentRight];
        [rightLabel setBackgroundColor:[UIColor clearColor]];
        [rightLabel setAlpha:INACTIVE_ALPHA];
        [[self contentView] addSubview:rightLabel];
        
    }
    return self;
}
- (void)dealloc {
    [valueLabel release];
    [super dealloc];
}
- (UILabel*)textLabel {
    if (!label) {
        [super textLabel];
        CGRect f=[valueLabel frame];
        f.origin.y+=LABEL_HEIGHT;
        [leftLabel setFrame:f];
        [valueLabel setFrame:f];
        [rightLabel setFrame:f];
    }
    return  label;
}
- (UIView*) newSliderWithFrame:(CGRect)sliderFrame min:(float)n max:(float)x {
    sliderFrame.origin.y+=8;
    UISlider* s = [[UISlider alloc] initWithFrame:sliderFrame];
    [s setMinimumValue:n];
    [s setMaximumValue:x];
    return s;
}
- (UISlider*) slider {
    return ((UISlider*) slider);
}
- (void)setFormat:(NSString *(*)(float))f {
    format=f;
    [valueLabel setText:format([[self slider] value])];
}

- (float)value {
    return [(UISlider*)slider value];
}
- (void)setValue:(float)v {
    [(UISlider*)slider setValue:v];
    [self updateValueLabel];
}


- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)events {
    [(UISlider*)slider addTarget:target action:action forControlEvents:events];
}

@end