//
//  TKTheme.m
//  ThemeKit
//
//  Created by Colin Cornaby on Mon Dec 08 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 "TKTheme.h"
#import "TKVariation.h"
#import "TKPXMResource.h"
#import "TKCLRResource.h"
#import "TKPPATResource.h"
#import "TKICNSResource.h"
#import "TKLAYOResource.h"
#import "TKTdatResource.h"
#import "TKResourceFile.h"
#import "TKAppSkin.h"
#import "RAZipper.h"


@implementation TKTheme

-(id)initWithThemeFromFile:(NSString *)theFilePath{
	self = [super init];
	//Go get the file we are supposed to open and add it to the co-ordinator
	[[self persistentStoreCoordinator] 
		addPersistentStoreWithType:NSSQLiteStoreType configuration:nil
		URL:[NSURL fileURLWithPath:theFilePath] options:nil error:nil];
	return self;
}

-(id)init{
	self = [super init];
	//Set up our store co-ordinator using our schema
	NSPersistentStoreCoordinator *coordinator
        = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:
		[[NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObjects:[NSBundle bundleForClass:[TKTheme class]], nil]] retain]];
	//Set this context to use our new store co-ordinator
	[self setPersistentStoreCoordinator: coordinator];
	[coordinator release];
	return self;
}

-(NSArray *)theVariations{
	NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
	NSEntityDescription *entity = [NSEntityDescription entityForName:@"TKVariation" inManagedObjectContext:self];
	NSSortDescriptor *sorter= [[[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES] autorelease];
	[request setEntity:entity];
	[request setSortDescriptors:[NSArray arrayWithObject:sorter]];
	NSArray *variationsFound = [self executeFetchRequest:request error:nil];
	return variationsFound;
}


+(id)themeWithThemeFromFile:(NSString *)theFilePath{
	return [[[[self class] alloc] initWithThemeFromFile:theFilePath] autorelease];
}


-(id)addVariation:(NSString *)theName{
	int variationCount=[[self theVariations] count];
	TKVariation *newVariation = (TKVariation *)[NSEntityDescription insertNewObjectForEntityForName:@"TKVariation" inManagedObjectContext:self];
	[newVariation setName:theName];
	[newVariation setValue:[NSNumber numberWithInt:variationCount] forKey:@"index"];
	[[self undoManager] setActionName:@"Add Variation"];
	return newVariation;
}

-(void)removeAppSkin:(id)appSkin
{
	[self deleteObject:appSkin];
}

-(void)removeVariation:(id)theVariation
{
	NSArray *variationsList = [self theVariations];
	int index = [variationsList count]-1;
	while([variationsList objectAtIndex:index]!=theVariation)
	{
		TKVariation *variationToModify = [variationsList objectAtIndex:index];
		index--;
		[variationToModify setValue:[NSNumber numberWithInt:index] forKey:@"index"];
	}
	[self deleteObject:theVariation];
}

-(id)appSkinForIdentifier:(NSString *)identifier
{
	NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
	NSEntityDescription *entity = [NSEntityDescription entityForName:@"TKAppSkin" inManagedObjectContext:self];
	if(identifier!=nil){
		NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(appIdentifier == '%@')", identifier]];
		[request setPredicate:predicate];
	}
	[request setEntity:entity];
	NSArray *variationsFound = [self executeFetchRequest:request error:nil];
	if([variationsFound count]==0)
		return nil;
	return [variationsFound objectAtIndex:0];
}

-(NSArray *)appSkinsForIdentifier:(NSString *)identifier sortDescriptors:(NSArray *)sortDescriptors
{
	NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
	NSEntityDescription *entity = [NSEntityDescription entityForName:@"TKAppSkin" inManagedObjectContext:self];
	if(identifier!=nil){
		NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(appIdentifier == '%@')", identifier]];
		[request setPredicate:predicate];
	}
	if(sortDescriptors!=nil)
		[request setSortDescriptors:sortDescriptors];
	[request setEntity:entity];
	NSArray *variationsFound = [self executeFetchRequest:request error:nil];
	if([variationsFound count]==0)
		return nil;
	return variationsFound;
}

-(id)addAppSkinForIdentifier:(NSString *)identifier
{
	TKAppSkin *newSkin = (TKAppSkin *)[NSEntityDescription insertNewObjectForEntityForName:@"TKAppSkin" inManagedObjectContext:self];
	[newSkin setAppIdentifier:identifier];
	return newSkin;
}

-(BOOL)isMutableContext
{
	return YES;
}

-(BOOL)isAllowedToAccessSuper
{
	return YES;
}

@end
