#import "ControllerSlider.h"

// TODO: add support for vertical sliders

@implementation ControllerSlider

- (BOOL)sendAction:(SEL)theAction to:(id)theTarget {
    printf("sendAction in slider\n");
    return [super sendAction:theAction to:theTarget];
}

- (void)awakeFromNib {
    _grooveImage = [[NSImage imageNamed:@"slider_groove"] retain];
    _buttonImage = [[NSImage imageNamed:@"slider_button"] retain];
    [self setNeedsDisplay:YES];
}


- (void)drawRect:(NSRect)rect {
    // perform custom drawing
    //printf("%d\n", (int)[self isOpaque]);
    //printf("%g %g\n",
    //    [_buttonImage size].width,
    //    [_buttonImage size].height);
    //[[NSColor clearColor] set];
    //NSRectFill(rect);
    [_grooveImage setFlipped:[self isFlipped]];
    [_buttonImage setFlipped:[self isFlipped]];

    // draw the groove
    // TODO: optimize this (only draw what's necessary)
    NSRect bounds = [self bounds];
    //[_buttonImage compositeToPoint:rect.origin operation:NSCompositeSourceOver];
    //return;
    
    float height = [_grooveImage size].height;
    float offsetY = (bounds.size.height - height)/2;
    NSRect target = NSMakeRect(bounds.origin.x, bounds.origin.y + offsetY,
                               bounds.size.width, height);
    target.size.width = 10;
    [_grooveImage drawInRect:target fromRect:NSMakeRect(0, 0, 10, height)
     operation:NSCompositeSourceOver fraction:1.0];
    target.origin.x = bounds.size.width - 10;
    [_grooveImage drawInRect:target fromRect:NSMakeRect([_grooveImage size].width - 10, 0, 10, height)
     operation:NSCompositeSourceOver fraction:1.0];
    target.origin.x = bounds.origin.x + 10;
    float toDraw = bounds.size.width - 20;
    while (toDraw > 0) {
        if (toDraw < 10) {
            target.size.width = toDraw;
        }
        [_grooveImage drawInRect:target
         fromRect:NSMakeRect(10, 0, (toDraw >= 10 ? 10 : toDraw), height)
         operation:NSCompositeSourceOver fraction:1.0];
        toDraw -= 10;
        target.origin.x += 10;
    }
    
    // draw the button
    float relPos = ([self doubleValue] - [self minValue]) /
                   ([self maxValue] - [self minValue]);
    float absPos = bounds.origin.x + relPos*(bounds.size.width - [_buttonImage size].width + 2);
    target.origin.x = absPos;
    target.origin.y = (bounds.size.height - [_buttonImage size].height)/2;
    target.size = [_buttonImage size];
    [_buttonImage drawInRect:target
     fromRect:NSMakeRect(0, 0, [_buttonImage size].width, [_buttonImage size].height)
     operation:NSCompositeSourceOver fraction:1.0];
    //[_buttonImage compositeToPoint:bounds.origin operation:NSCompositeSourceOver];
}

- (void)reposition:(NSEvent *)theEvent {
    NSPoint location = [self convertPoint:[theEvent locationInWindow] fromView:nil];
    //printf("mouseDown at: %g %g\n", location.x, location.y);
    float startValue = ([_buttonImage size].width - 2)/2;
    float stopValue = [self bounds].size.width - startValue;
    if (location.x <= startValue) {
        [self setFloatValue:[self minValue]];
    } else if (location.x >= stopValue) {
        [self setFloatValue:[self maxValue]];
    } else {
        [self setFloatValue:[self minValue] +
         (location.x - startValue)/(stopValue - startValue)*([self maxValue] - [self minValue])];
    }
    //[self setFloatValue:[self minValue] +
    //    (location.x/([self bounds].size.width - 1))*([self maxValue] - [self minValue])];
    //printf("new value: %g\n", [self floatValue]);
    [self setNeedsDisplay:YES];
}

- (void)mouseDown:(NSEvent *)theEvent {
    [self reposition:theEvent];
}

- (void)mouseDragged:(NSEvent *)theEvent {
    [self reposition:theEvent];
}

@end
