//
//  IPSliderThumb.m
//  Ranger
//
//  Created by Ari Krupnik on 6/8/11.
//  Copyright 2011 lib.aero. All rights reserved.
//

#import "MMSlider.h"

#define ACTIVE_ALPHA 1.0
#define INACTIVE_ALPHA 0.5

@implementation MMSliderThumb

@synthesize lowerBound;
@synthesize upperBound;

- (id)initWithParent:(MMSlider*)parent 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) {
        // 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
        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:[NSString stringWithFormat:@"%3.2f", [self value]]];
        [label setTextAlignment:UITextAlignmentCenter];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setAlpha:INACTIVE_ALPHA];
        [self addSubview:label];
    }
    return self;
}

- (float)value {
    return [self frame].origin.x / ([[self superview] frame].size.width - [self frame].size.width);
}
- (void)setValue:(float) v {
    if (v<0) {
        v=0;
    }
    if (v>1) {
        v=1;
    }
    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);
    [self setFrame:frame];
    [label setText:[NSString stringWithFormat:@"%3.2f", [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)];
}


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

@end
