//
//  TKVariation.m
//  ThemeKit
//
//  Created by Colin Cornaby on Sun Dec 21 2003.
//  Copyright (c) 2003 Carpe Stellarem. All rights reserved.
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library 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
//  Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#import "TKVariation.h"
#import "TKTheme.h"
#import "TKAppSkin.h"

@implementation TKVariation

-(id)appSkinForIdentifier:(NSString *)identifier
{
	NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(appIdentifier == '%@')", identifier];
	NSArray *variationsFound = [[[self primitiveValueForKey:@"applicationSkins"] allObjects] filteredArrayUsingPredicate:predicate];
	if([variationsFound count]==0)
		return nil;
	return [variationsFound objectAtIndex:0];
}

-(void)removeAppSkin:(id)appSkin
{
	[[self mutableSetValueForKey:@"applicationSkins"] removeObject:appSkin];
}

-(NSArray *)appSkins{
	return [[self valueForKey:@"applicationSkins"] allObjects];
}

-(id)addAppSkin:(id)appSkin
{
	NSMutableSet *appSkinSet = [self mutableSetValueForKey:@"applicationSkins"];
	NSEnumerator *enumerator = [appSkinSet objectEnumerator];
	id value;
	while ((value = [enumerator nextObject])) {
		if([[value appIdentifier] isEqualToString:[appSkin appIdentifier]])
			[appSkinSet removeObject:value];
	}
	[appSkinSet addObject:appSkin];
	return appSkin;
}

-(void)setDescriptorHTML:(NSString *)htmlCode
{
	[self setMetadataForKey:@"TKVariationHTMLCode" forValue:htmlCode];
}

-(NSString *)descriptorHTML
{
	NSString *htmlCode = [self metadataForKey:@"TKVariationHTMLCode"];
	if(htmlCode == nil)
		return @"";
	return htmlCode;
}

-(NSDictionary *)descriptorFileDictionary
{
	return [[self metadataForKey:@"TKDescriptorFileDictionary"] propertyList];
}

-(void)setDescriptorFileDictionary:(NSDictionary *)descriptorFileDictionary
{
	[self setMetadataForKey:@"TKDescriptorFileDictionary" forValue:[descriptorFileDictionary description]];
}

- (id)initWithEntity:(NSEntityDescription*)entity insertIntoManagedObjectContext:(NSManagedObjectContext*)context
{
	self = [super initWithEntity:(NSEntityDescription*)entity insertIntoManagedObjectContext:(NSManagedObjectContext*)context];
	if([self metadataForKey:@"TKDRMKey"])
		return NULL;
	idCache = [[NSMutableDictionary alloc] init];
	return self;
}

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

-(void)setDRMPublisher:(NSString *)publisher
{
	[self setMetadataForKey:@"TKDRMPublisher" forValue:publisher];
}

-(void)setName:(NSString *)theName
{
	[self setMetadataForKey:@"TKFlavorName" forValue:theName];
}

-(NSString *)name{
	NSString *flavorName = [self metadataForKey:@"TKFlavorName"];
	if(flavorName==nil)
		return @"Untitled Theme";
	return flavorName;
}
@end
