/*
 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"
#import "util.h"

@implementation MMSliderThumb

@synthesize parent;
@synthesize lowerBound;
@synthesize upperBound;

NSString* defaultFormat(float value) {
    return [NSString stringWithFormat:@"%3.2f", value];
}

- (id)initWithParent:(MMSlider*)p andOrientation:(enum MMSliderThumbOrientation)orientation {
    CGRect frame;
    frame.size.height = THUMB_WIDTH;
    frame.size.width = THUMB_WIDTH;
    frame.origin.x=0;
    frame.origin.y=0;
    self = [super initWithFrame:frame];
    if (self) {
        [self setParent:p];
        
        // make thumb button
        thumb = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        CGRect thumbFrame;
        thumbFrame.size.height = THUMB_WIDTH;
        thumbFrame.size.width = THUMB_WIDTH;
        thumbFrame.origin.x = 0;
        thumbFrame.origin.y = 0;
        if (orientation==MMSliderThumbOrientationAbove) {
            // nothing to do
        } else {
            frame.origin.y = [parent frame].size.height - thumbFrame.size.height;
            [self setFrame:frame];
        }
        [thumb setFrame:thumbFrame];

        [thumb addTarget:self action:@selector(moveThumb:event:) forControlEvents:UIControlEventTouchDragInside|UIControlEventTouchDragOutside];
        [thumb addTarget:self action:@selector(thumbUp) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchDragOutside];
        [thumb addTarget:self action:@selector(thumbDown) forControlEvents:UIControlEventTouchDown];
        [self addSubview:thumb];
        
        // make label
        [self setFormat:defaultFormat];
        CGRect labelFrame;
        labelFrame.size.width = 100;
        labelFrame.size.height = thumbFrame.size.height;
        labelFrame.origin.x = thumbFrame.origin.x - (labelFrame.size.width - thumbFrame.size.width)/2;
        if (orientation==MMSliderThumbOrientationAbove) {
            labelFrame.origin.y = [parent frame].size.height - labelFrame.size.height;
        } else {
            labelFrame.origin.y = -([parent frame].size.height - thumbFrame.size.height);
        }
        label = [[UILabel alloc] initWithFrame:labelFrame];
        [label setText:format([self value])];
        [label setTextAlignment:UITextAlignmentCenter];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setAlpha:INACTIVE_ALPHA];
        [self addSubview:label];
    }
    return self;
}

- (void)setFormat:(NSString *(*)(float value))f {
    format = f;
    [label setText:format([self value])];
}

- (float)value {
    // consider using `parent' instead of `superview'
    //return [self frame].origin.x / ([[self superview] frame].size.width - [self frame].size.width);
    float pixels = [self frame].origin.x;
    return scale(pixels, 0, [[self superview] frame].size.width - [self frame].size.width, [parent minimumValue], [parent maximumValue]);
}
- (void)setValue:(float) v {
    if (v<[parent minimumValue]) {
        v=[parent minimumValue];
    }
    if (v>[parent maximumValue]) {
        v=[parent maximumValue];
    }
    if ([self lowerBound] && v<[[self lowerBound] value]) {
        v=[[self lowerBound] value];
    }
    if ([self upperBound] && v>[[self upperBound] value]) {
        v=[[self upperBound] value];
    }
    CGRect frame = [self frame];
    //frame.origin.x = v*([[self superview] frame].size.width-[self frame].size.width);
    frame.origin.x = scale(v, [parent minimumValue], [parent maximumValue], 0, [[self superview] frame].size.width-[self frame].size.width);
    [self setFrame:frame];
    [label setText:format([self value])];
}

- (void) thumbDown {
    [label setAlpha:ACTIVE_ALPHA];
}

- (void) thumbUp {
    [label setAlpha:INACTIVE_ALPHA];
}

- (void)moveThumb:(id)target event:(UIEvent*)event {
    UITouch* touch = [[event allTouches] anyObject];
    float x = [touch locationInView:self].x;
    float prev_x = [touch previousLocationInView:self].x;
    //[self setValue:[self value] + (x - prev_x) / ([[self superview] frame].size.width-[self frame].size.width)];
    float d_pix = (x - prev_x);
    float scale = ([parent maximumValue]-[parent minimumValue])/([[self superview] frame].size.width-[self frame].size.width);
    float d_v = d_pix * scale;
    [self setValue:[self value] + d_v];
    [parent sendActionsForControlEvents:UIControlEventValueChanged];
}


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

@end
