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

NSString* formatAsDegrees(float value) {
    return [NSString stringWithFormat:@"%2.0f°", value*90];
}
NSString* formatAsPixelRatio(float value) {
    return [NSString stringWithFormat:@"1 pixel swipe = %3.2f pixel reposnse", value];
}
NSString* formatSpringTension(float value) {
    return [NSString stringWithFormat:@"%3.2f (%3.2fs to return from max)", value, 1/value];
}

@implementation GlobalSettingsViewController

- (void)rollLimitValueChanged:(MMSlider*) slider {
    [processor setAccelerometerRollMin:[slider lowerLimit]];
    [processor setAccelerometerRollCenter:[slider value]];    
    [processor setAccelerometerRollMax:[slider upperLimit]];
}
- (void)pitchLimitValueChanged:(MMSlider*) slider {
    [processor setAccelerometerPitchMin:[slider lowerLimit]];
    [processor setAccelerometerPitchCenter:[slider value]];    
    [processor setAccelerometerPitchMax:[slider upperLimit]];
}
- (void)noiseFilteringValueChanged:(UISlider*) slider {
    [processor setAccelerometerSensitivity:1-[slider value]];
}
- (void)swipeScaleValueChanged:(UISlider*) slider {
    [processor setSwipeScale:[slider value]];
}
- (void)returnSpringValueChanged:(UISlider*) slider {
    [processor setReturnSpringTension:[slider value]];
}

- (id)initWithProcessor:(ModelProcessor*)p {
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        processor=p;
        [self setTitle:@"iPhly Config"];
        dataSource = [[ArrayTableDataSource alloc] init];
        NSMutableArray* sections = [NSMutableArray array];
        
        UITableSection* s = [[[UITableSection alloc]init]autorelease];
        [s setHeaderTitle:@"Accelerometer"];
        NSMutableArray* cells = [NSMutableArray array];
        // roll limits
        MMTableCell* c = [[[MMTableCell alloc] initWithMin:-1 andMax:1] autorelease];
        [[c slider] setLowerLimit:[processor accelerometerRollMin]];
        [[c slider] setValue:[processor accelerometerRollCenter]];
        [[c slider] setUpperLimit:[processor accelerometerRollMax]];
        [[c slider] setFormat:formatAsDegrees];
        [[c textLabel] setText:@"Roll Axis Limits"];
        [[c slider] addTarget:self action:@selector(rollLimitValueChanged:) forControlEvents:UIControlEventValueChanged];
        [cells addObject:c];
        // pitch limits
        c = [[[MMTableCell alloc] initWithMin:-1 andMax:1] autorelease];
        [[c slider] setLowerLimit:[processor accelerometerPitchMin]];
        [[c slider] setValue:[processor accelerometerPitchCenter]];
        [[c slider] setUpperLimit:[processor accelerometerPitchMax]];
        [[c slider] setFormat:formatAsDegrees];
        [[c textLabel] setText:@"Pitch Axis Limits"];
        [[c slider] addTarget:self action:@selector(pitchLimitValueChanged:) forControlEvents:UIControlEventValueChanged];
        [cells addObject:c];
        // noise filtering
        UISliderCell* sc = [[[UISliderCell alloc] initWithMin:0 andMax:0.7] autorelease];
        [[sc slider] setValue:1-[processor accelerometerSensitivity]];
        [sc updateValueLabel];
        [[sc textLabel] setText:@"Noise Filtering"];
        [[sc leftLabel] setText:@"Fast"];
        [[sc rightLabel] setText:@"Smooth"];
        [[sc slider] addTarget:self action:@selector(noiseFilteringValueChanged:) forControlEvents:UIControlEventValueChanged];
        [cells addObject:sc];
        [s setCells:cells];
        [sections addObject:s];
        
        s = [[[UITableSection alloc]init]autorelease];
        [s setHeaderTitle:@"Touchscreen"];
        cells = [NSMutableArray array];
        sc = [[[UISliderCell alloc] initWithMin:0.7 andMax:1.5] autorelease];
        [[sc slider] setValue:[processor swipeScale]];
        [sc setFormat:formatAsPixelRatio];
        [[sc textLabel] setText:@"Swipe Scale"];
        [[sc slider] addTarget:self action:@selector(swipeScaleValueChanged:) forControlEvents:UIControlEventValueChanged];
        [cells addObject:sc];
        sc = [[[UISliderCell alloc] initWithMin:0.5 andMax:2.0] autorelease];
        [[sc slider] setValue:[processor returnSpringTension]];
        [sc setFormat:formatSpringTension];
        [[sc textLabel] setText:@"Return Spring Tension"];
        [[sc slider] addTarget:self action:@selector(returnSpringValueChanged:) forControlEvents:UIControlEventValueChanged];
        [cells addObject:sc];
        [s setCells:cells];
        [sections addObject:s];
        
        [dataSource setSections:sections];
        [[self tableView] setDataSource:dataSource];
    }
    return self;
}

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

#pragma mark - View lifecycle

#pragma mark - Table view data source

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* c=[self.tableView.dataSource tableView:tableView cellForRowAtIndexPath:indexPath];
    if ([c isKindOfClass:[SliderCell class]]) {
        return [(SliderCell*) c height];
    } else {
        return 44;
    }
}

@end
