/*
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 "NSMutableDictionary+ModelConfig.h"



NSString* keyFromSelector(SEL selector) {
    // this can be faster with string.h functions if the need arises
    NSString* key=[NSString stringWithCString:sel_getName(selector) encoding:NSASCIIStringEncoding];
    NSString* PREFIX = @"set";
    if ([key hasPrefix:PREFIX]) {
        unichar ic[1];
        ic[0] = [key characterAtIndex:[PREFIX length]];
        NSString* is = [NSString stringWithCharacters:ic length:1];
        is = [is lowercaseString];
        key = [key substringFromIndex:[PREFIX length]+1];
        key = [key substringToIndex:[key length]-1];
        key = [is stringByAppendingString:key];
    }
    return key;
}

@implementation NSMutableDictionary (ModelConfig)

- (id)objectForSelector:(SEL)selector or:(id)missing {
    id result = [self objectForKey:keyFromSelector(selector)];
    return result ? result : missing;
}
- (void)setObject:(id)value forSelector:(SEL)selector {
    [self setObject:value forKey:keyFromSelector(selector)];
}

- (float)floatForSelector:(SEL)selector or:(float)missing {
    return [[self objectForSelector:selector or:[NSNumber numberWithFloat:missing]] floatValue];
}
- (void)setFloat:(float)value forSelector:(SEL)selector {
    [self setObject:[NSNumber numberWithFloat:value] forSelector:selector];
}


- (NSString *) name {
	return [self objectForKey: @"name"];
}
- (void) setName: (NSString *) name {
	[self setObject: name forKey: @"name"];
}

- (float) accelerometerSensitivity {
	return [[self objectForKey:@"accelerometerSensitivity"] floatValue];
}
- (void) setAccelerometerSensitivity: (float) s {
	[self setObject: [NSNumber numberWithFloat: s] forKey:@"accelerometerSensitivity"];
}

- (BOOL) trimsVisible {
    NSNumber* n = [self objectForKey:@"trimsVisible"];
    if (!n) {
        return YES;
    } else {
        return [n boolValue];
    }
}
- (void) setTrimsVisible:(BOOL)trimsVisible {
    [self setObject:[NSNumber numberWithBool:trimsVisible] forKey:@"trimsVisible"];
}

- (BOOL) trimsVibrate {
    NSNumber* n = [self objectForKey:@"trimsVibrate"];
    if (!n) {
        return NO;
    } else {
        return [n boolValue];
    }
}
- (void) setTrimsVibrate:(BOOL)trimsVibrate {
    [self setObject:[NSNumber numberWithBool:trimsVibrate] forKey:@"trimsVibrate"];
}

- (BOOL) isAirplane {
	return [[self objectForKey:@"model-type"] isEqualToString:@"airplane"];
}
- (BOOL) isCar {
	return [[self objectForKey:@"model-type"] isEqualToString:@"car"];
}

- (int) numChannels {
	return [(NSArray *) [self objectForKey: @"channels"] count];
}
- (NSMutableDictionary *) channel: (int) number {
	return [(NSArray *) [self objectForKey: @"channels"] objectAtIndex: number];
}

- (BOOL) reversing {
	return [[self objectForKey: @"reversing"] boolValue];
}
- (void) setReversing: (BOOL) reversing {
	[self setObject: [NSNumber numberWithBool: reversing] forKey:@"reversing"];
}

- (float) trim {
	return [[self objectForKey: @"trim"] floatValue];
}
- (void) setTrim: (float) trim {
	[self setObject: [NSNumber numberWithFloat: trim] forKey:@"trim"];
}

- (float) rate {
	return [[self objectForKey: @"rate"] floatValue];
}
- (void) setRate: (float) rate {
	[self setObject: [NSNumber numberWithFloat: rate] forKey:@"rate"];
}

@end
