//
//  EntitySchema.m
//  longspear
//
//  Created by Niko Matsakis on Fri Jun 18 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

#import "EntitySchema.h"
#import "Dice.h"

NSString *kTypeNames[] = { @"Text", @"Number", @"Bonus" };

NSString *PrepareDisplayValue (NSMutableDictionary *entsch, // the schema dictionary
                               NSString *field,             
                               NSString *value,
                               int flags)
{
    NSNumber *fieldtypeobj = [entsch objectForKey:field];
    int fieldtype = (fieldtypeobj ? [fieldtypeobj intValue] : kBONUSTYPE);
    
    switch (fieldtype) {
        case kTEXTTYPE:
            return value;
            
        case kNUMBERTYPE:
            if ([value length] == 0) return @"0";
            return value;

        case kBONUSTYPE:
            if (flags & kROLLED)
                return @"TODO";
            return value;
    }
    
    return @"error---unknown type";
}

NSString *PrepareEnteredValue (NSMutableDictionary *entsch, // the schema dictionary
                               NSString *field,             
                               NSString *value)
{
    NSNumber *fieldtypeobj = [entsch objectForKey:field];
    int fieldtype = (fieldtypeobj ? [fieldtypeobj intValue] : kBONUSTYPE);

    switch (fieldtype) {
        case kTEXTTYPE:
            return value;
            
        case kNUMBERTYPE:
        case kBONUSTYPE:
            return [NSString stringWithFormat:@"%d", [value rolledValue]];
    }
    
    return @"error---unknown type";
}

BOOL IsBonus (NSMutableDictionary *entsch, NSString *field)
{
    NSNumber *fieldtypeobj = [entsch objectForKey:field];
    int fieldtype = (fieldtypeobj ? [fieldtypeobj intValue] : kBONUSTYPE);
    return fieldtype == kBONUSTYPE;
}
