//
//  UICookieMonster.m
//  Unsanity Installer
//
//  Created by Slava Karpenko on Thu Nov 28 2002.
//  Copyright (c) 2002 Unsanity LLC. All rights reserved.
//

#import "UICookieMonster.h"

#define kCookieID CFSTR("Unsanity Installer Receipts")

@implementation UICookieMonster
+ monsterWithProductID:(NSString*)pid global:(BOOL)global version:(UInt32)inVersion
{
    if (!pid)
        return nil;
        
    return [[[UICookieMonster alloc] initWithProductID:pid globalVersionValid:YES global:global version:inVersion] autorelease];
}

+ monsterWithProductID:(NSString*)pid global:(BOOL)global
{
    if (!pid)
        return nil;

    return [[[UICookieMonster alloc] initWithProductID:pid globalVersionValid:NO global:global version:0] autorelease];
}

+ monsterWithProductID:(NSString*)pid
{
    if (!pid)
        return nil;
        
    return [[[UICookieMonster alloc] initWithProductID:pid globalVersionValid:NO global:NO version:0] autorelease];
}

- initWithProductID:(NSString*)pid globalVersionValid:(BOOL)gvValid global:(BOOL)global version:(UInt32)inVersion
{
    CFDictionaryRef dict;
    
    productID = [pid retain];
    isGlobal = global;
    version = inVersion;
    cachedVersion = NULL;
    
    files = [[NSMutableArray arrayWithCapacity:0] retain];
    
    CFPreferencesSynchronize(kCookieID, kCFPreferencesAnyUser, kCFPreferencesCurrentHost);
    CFPreferencesSynchronize(kCookieID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
    
    dict = CFPreferencesCopyValue((CFStringRef)productID, kCookieID, kCFPreferencesAnyUser, kCFPreferencesCurrentHost);
    
    if (dict)
    {
        NSNumber* vers = (NSNumber*)CFDictionaryGetValue(dict, CFSTR("Version"));
        [files addObjectsFromArray:(NSArray*)CFDictionaryGetValue(dict, CFSTR("Files"))];
        cachedVersion = [vers intValue];
        
        if (!gvValid) isGlobal = YES;
        
        //NSLog(@"Global data read = %@", dict);
        CFRelease(dict);
    }
    else
    {
        dict = CFPreferencesCopyValue((CFStringRef)productID, kCookieID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
        
        if (dict)
        {
            NSNumber* vers = (NSNumber*)CFDictionaryGetValue(dict, CFSTR("Version"));
            [files addObjectsFromArray:(NSArray*)CFDictionaryGetValue(dict, CFSTR("Files"))];
            cachedVersion = [vers intValue];
            if (!gvValid) isGlobal = NO;
            
            //NSLog(@"Local data read = %@", dict);
            CFRelease(dict);
        }
    
    }
    
    if (!cachedVersion) cachedVersion = version;
    if (!gvValid) version = cachedVersion;
    
    dirty = NO;
    
    return self;
}

- (BOOL)isGlobal
{
    return isGlobal;
}

- (UInt32)productVersion
{
    return cachedVersion;
}

- (void)dealloc
{
    [self flush];
    
    [productID release];
    [files release];
}

- (void)addFile:(NSString*)path
{
    if (![files containsObject:path])
    {
        [files addObject:path];
        dirty = YES;
    }
}

- (void)removeFile:(NSString*)path
{
}

- (NSArray*)filesToRemove
{
    return files;
}

- (void)flush
{
    NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithCapacity:0];
    
    if (!dirty)
        return;
    
    [dict setObject:[NSNumber numberWithInt:version] forKey:@"Version"];
    [dict setObject:files forKey:@"Files"];
    
    CFPreferencesSetValue((CFStringRef)productID, NULL, kCookieID, isGlobal?kCFPreferencesCurrentUser:kCFPreferencesAnyUser, isGlobal?kCFPreferencesAnyHost:kCFPreferencesCurrentHost);
    CFPreferencesSetValue((CFStringRef)productID, (CFPropertyListRef)dict, kCookieID, isGlobal?kCFPreferencesAnyUser:kCFPreferencesCurrentUser, isGlobal?kCFPreferencesCurrentHost:kCFPreferencesAnyHost);
    
    CFPreferencesSynchronize(kCookieID, kCFPreferencesAnyUser, kCFPreferencesCurrentHost);
    CFPreferencesSynchronize(kCookieID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
    
    dirty = NO;
}

+ (void)removeProductID:(NSString*)pid
{
    CFPreferencesSetValue((CFStringRef)pid, NULL, kCookieID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
    CFPreferencesSetValue((CFStringRef)pid, NULL, kCookieID, kCFPreferencesAnyUser, kCFPreferencesCurrentHost);
    
    CFPreferencesSynchronize(kCookieID, kCFPreferencesAnyUser, kCFPreferencesCurrentHost);
    CFPreferencesSynchronize(kCookieID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
}

@end
