//
//  Config.m
//  longspear
//
//  Created by Niko Matsakis on Sun Jun 13 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

#import "Config.h"
#import "Entity.h"
#import "MonsterDatabase.h"
#import "EntitySchema.h"

#define kMONSTERDEFAULT @"MonsterDatabase"
#define kENTITYSCHEMADEFAULT @"EntitySchema"

void initializeDefaultConfiguration ()
{
    NSMutableDictionary *defaults = [[[NSMutableDictionary alloc] initWithCapacity:8] autorelease];;

#   define N(x) [NSNumber numberWithInt:x]
    /* Create the default entity schema */
    NSMutableDictionary *entityschdic = [[[NSMutableDictionary alloc] initWithCapacity:32] autorelease];
    [entityschdic setObject:N(kTEXTTYPE) forKey:kNAME];
    [entityschdic setObject:N(kTEXTTYPE) forKey:kTYPE];
    [entityschdic setObject:N(kTEXTTYPE) forKey:kCOMMENTS];
    [entityschdic setObject:N(kNUMBERTYPE) forKey:kHITPOINTS];
    [entityschdic setObject:N(kNUMBERTYPE) forKey:kARMORCLASS];
    [entityschdic setObject:N(kTEXTTYPE) forKey:kATTACKS];
    [entityschdic setObject:N(kTEXTTYPE) forKey:kDAMAGE];
    [entityschdic setObject:N(kBONUSTYPE) forKey:kINITIATIVE];
    [entityschdic setObject:N(kNUMBERTYPE) forKey:kINITSCORE];
    [entityschdic setObject:N(kBONUSTYPE) forKey:kLISTEN];
    [entityschdic setObject:N(kBONUSTYPE) forKey:kSPOT];
    [defaults setObject:entityschdic forKey:kENTITYSCHEMADEFAULT];
#   undef N

    /* Create the default column sets: */
    NSMutableArray *campaigncharset = [[[NSMutableArray alloc] initWithCapacity:8] autorelease];
    [defaults setObject:campaigncharset forKey:kCAMPAIGNCHARSET];
    [campaigncharset addObject:kNAME];
    [campaigncharset addObject:kTYPE];
    [campaigncharset addObject:kLISTEN];
    [campaigncharset addObject:kSPOT];

    NSMutableArray *campaigncombset = [[[NSMutableArray alloc] initWithCapacity:8] autorelease];
    [defaults setObject:campaigncombset forKey:kCAMPAIGNCOMBSET];
    [campaigncombset addObject:kNAME];
    [campaigncombset addObject:kCOMMENTS];
    
    NSMutableArray *combatcharset = [[[NSMutableArray alloc] initWithCapacity:8] autorelease];
    [defaults setObject:combatcharset forKey:kCOMBATCHARSET];
    [combatcharset addObject:kNAME];
    [combatcharset addObject:kINITSCORE];
    
    NSMutableArray *combatset = [[[NSMutableArray alloc] initWithCapacity:8] autorelease];
    [defaults setObject:combatset forKey:kCOMBATSET];
    [combatset addObject:kNAME];
    [combatset addObject:kHITPOINTS];
    [combatset addObject:kARMORCLASS];
    [combatset addObject:kAFFECTEDBY];
    
    NSMutableArray *effectset = [[[NSMutableArray alloc] initWithCapacity:8] autorelease];
    [defaults setObject:effectset forKey:kEFFECTSET];
    [effectset addObject:kNAME];
    [effectset addObject:kROUNDS];
    [effectset addObject:kCOMMENTS];
    
    NSMutableArray *aftermathset = [[[NSMutableArray alloc] initWithCapacity:8] autorelease];
    [defaults setObject:aftermathset forKey:kAFTERMATHSET];
    [aftermathset addObject:kNAME];
    [aftermathset addObject:kFATE];

    NSMutableArray *monsterdbset = [[[NSMutableArray alloc] initWithCapacity:8] autorelease];
    [defaults setObject:monsterdbset forKey:kMONSTERDBSET];
    [monsterdbset addObject:kNAME];
    [monsterdbset addObject:kCR];
    
    /* flags: */
    [defaults setObject:@"NO" forKey:kAUTOROLLATTACKS];
    
    [defaults setObject:Pack (DefaultMonsterDatabase()) forKey:kMONSTERDEFAULT];
    
    NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
    [standard registerDefaults:defaults];
}

NSArray *ColumnSet (NSString *colset)
{
    NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
    NSArray *colsetdata = [standard arrayForKey:colset];
    assert (colsetdata);
    return colsetdata;
}

NSNotificationCenter *
NotificationCenter ()
{
    return [NSNotificationCenter defaultCenter];
}

NSArray *
MonsterDatabase ()
{
    NSMutableArray *result = [[[NSMutableArray alloc] initWithCapacity:1024] autorelease];
    [result addObjectsFromArray:DefaultMonsterDatabase()];
    
    NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
    NSDictionary *additional = Unpack ([standard dataForKey:kMONSTERDEFAULT]);
    
    NSEnumerator enumer = [additional keyEnumerator];
    id monsternm;
    while ((monsternm = [enumer nextObject])) {
        AddSortedMonster (result, [additional objectForKey:monsternm]);
    }
}

BOOL
Flag (NSString *flagname)
{
    NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
    return [standard boolForKey:flagname];
}

void UpdateMonsterDatabase (NSArray *db)
{
    NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
    [standard setObject:Pack (db) forKey:kMONSTERDEFAULT];
}

void UpdateColumnSet (NSString *colset, NSArray *data)
{
    NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
    [standard setObject:data forKey:colset];
}

void UpdateFlag (NSString *flagname, BOOL value)
{
    NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
    [standard setBool:value forKey:flagname];    
}

NSDictionary *gEntitySchema = nil;

NSDictionary *EntitySchema ()
{
    if (!gEntitySchema) {
        // cache since it is looked up quite often
        NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
        gEntitySchema = [[standard dictionaryForKey:kENTITYSCHEMADEFAULT] retain];
    }
    return gEntitySchema;    
}

NSData *Pack (id object)
{
    return [NSKeyedArchiver archivedDataWithRootObject:object];
}

id Unpack (NSData *data)
{
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

