//
//  FTCustomization.mm
//
//  FelineTek iPhone ToolBox. Copyright (C) 2009 FelineTek.
//
//  FelineTek iPhone ToolBox is free software; you can redistribute it 
//  and/or modify it under the terms of the GNU Lesser General Public
//  License (LGPL) 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.
//
//  For more info see http://www.felinetek.com/FTToolbox
//
//  Created by Panos Hadjikomninos on 13/03/2009.
//  Copyright (C) 2009 FelineTek. All rights reserved.


#import "FTToolbox.h"



@implementation FTCustomization





static NSBundle *bundle = nil;              // custom

static NSBundle *bundle_lang = nil;         // custom-fr

static NSBundle *bundle_file = nil;         // local: provider.lproj

static NSString *default_lang = nil;        // en (unless reset)

static NSString *override_lang = nil;       // de








#pragma mark -
#pragma mark Initialization    









+ (void) FTsetCustomization:(NSString*) custom  
{
	
	NSString *path = [[ NSBundle mainBundle ] pathForResource:custom ofType:@"lproj" ];
	
	[bundle release];
	bundle=nil;
	
	if (path == nil)
	{

		bundle = [[NSBundle mainBundle] retain];
	}
	else 
	{
		bundle = [[NSBundle bundleWithPath:path] retain];
		
		
		NSString *path2 = [[ NSBundle mainBundle ] pathForResource:[NSString stringWithFormat:@"%@-%@",custom,[FTCustomization FTdeviceLanguage]] ofType:@"lproj" ];
		
		[bundle_lang release];
		bundle_lang=nil;
		if (path2 != nil)		
			bundle_lang = [[NSBundle bundleWithPath:path2] retain];
	}
	
}





+(void) FTinitializeCustomization
{
	
	[default_lang release];
	default_lang=nil;
	
	default_lang=[[NSString stringWithString:@"en"] retain];
		
	[FTCustomization FTsetCustomization:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"FTCustomizationString"]];
		
}





+ (void) FTinitializeCustomization:(NSString*) short_lang  
{
	
	[default_lang release];
	default_lang=nil;
	
	default_lang=[[NSString stringWithString:short_lang] retain];
	
	[FTCustomization FTsetCustomization:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"FTCustomizationString"]];
	
}




+ (void) FToverrideCustomization:(NSString*) override  
{
	
	[override_lang release];
	override_lang=nil;
	
	override_lang=[[NSString stringWithString:override] retain];
	
	
}





+ (void) FTsetRemoteCustomizationBundle:(NSString*) provider withBundleName:(NSString*) name
{
	
	NSString *filepath = [NSString stringWithString:[FTToolBox getFilePath:name]];
	FT_LOGU(FT_DEBUGGING, @"BUNDLE = %@", filepath);
	
	NSString *path = [[ NSBundle bundleWithPath:filepath ] pathForResource:provider ofType:@"lproj" ];
	FT_LOGU(FT_DEBUGGING, @"FILE = %@", path);
	
	
	if (path != nil) 
	{
		
		[bundle_file release];
		 bundle_file=nil;
		
		bundle_file = [[NSBundle bundleWithPath:path] retain];
	}
}






+ (void) FTsetRemoteCustomizationFile:(NSString*) provider
{
	
	NSString *filepath = [NSString stringWithString:[FTToolBox getFilePath:[NSString stringWithFormat:@"%@.lproj",provider]]];
	FT_LOGU(FT_DEBUGGING, @"FILE = %@", filepath);
	
	
	if (filepath != nil)
	{
		
		[bundle_file release];
		bundle_file=nil;		
		
		bundle_file = [[NSBundle bundleWithPath:filepath] retain];
	}
}






+ (NSString*) FTdeviceLanguage 
{
	
	NSArray* languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
	
	NSString *preferredLang = [[[NSString alloc] initWithString:[languages objectAtIndex:0]] autorelease];
	
	return preferredLang;
}





+ (NSString*) FTdefaultLanguage 
{
	
	NSString *defaultLang = [[[NSString alloc] initWithString:default_lang] autorelease];
	
	return defaultLang;
}





+ (NSString*) FToverrideLanguage 
{
	
	NSString *defaultLang = [[[NSString alloc] initWithString:override_lang] autorelease];
	
	return defaultLang;
}






+ (NSString *)FTcustomizedString:(NSString *)key value:(NSString *)comment
{
	
	FT_LOGS(FT_VERBOSE, [bundle bundlePath]);
	
	
	if (bundle_file != nil)
	{
		if (![key isEqualToString:[bundle_file localizedStringForKey:key value:comment table:nil]])
			return [bundle_file localizedStringForKey:key value:comment table:nil];
	}
	
	if (bundle_lang != nil)
	{
		if (![key isEqualToString:[bundle_lang localizedStringForKey:key value:comment table:nil]])
			return [bundle_lang localizedStringForKey:key value:comment table:nil];
	}
	
	if (![key isEqualToString:[bundle localizedStringForKey:key value:comment table:nil]])
	{
		return [bundle localizedStringForKey:key value:comment table:nil];
	}
	
	
	else if (override_lang != nil)
	{
		NSString *path = [[ NSBundle mainBundle ] pathForResource:override_lang ofType:@"lproj" ];
		
		return [[NSBundle bundleWithPath:path] localizedStringForKey:key value:comment table:nil];
	}
	
	// DEVICE LANGUAGE
	// ---------------
	else if (![key isEqualToString:[[NSBundle mainBundle] localizedStringForKey:key value:comment table:nil]])
		return [[NSBundle mainBundle] localizedStringForKey:key value:comment table:nil];
	

	
	else // ("en")
	{
		NSString *path = [[ NSBundle mainBundle ] pathForResource:default_lang ofType:@"lproj" ];
		
		return [[NSBundle bundleWithPath:path] localizedStringForKey:key value:comment table:nil];
	}

	
	
}









#pragma mark -
#pragma mark Customization




NSString *FTCustomString(NSString * str)
{
	return [FTCustomization FTcustomizedString:str value:@""];
}



int FTCustomInt(NSString * str)
{
	return [FTCustomString(str) intValue];
}



float  FTCustomFloat(NSString * str)
{
	return [FTCustomString(str) floatValue];
}



BOOL FTCustomBool(NSString * str)
{
	return [FTCustomString(str) boolValue];
}



UIImage  *FTCustomImage(NSString * img)
{
	return [UIImage imageNamed:FTCustomString(img)]; 
}



UIColor *FTCustomColor(NSString * str)
{
	
	
	
	NSArray * colors=[FTCustomString(str) componentsSeparatedByString:@","];
	
	if ([colors count] < 4)
		NSLog(@"Invalid Color: %@   Localised Result: %@",str, FTCustomString(str));
	
	int red = [(NSString *) [colors objectAtIndex:0] intValue];
	int green = [(NSString *) [colors objectAtIndex:1] intValue];
	int blue = [(NSString *) [colors objectAtIndex:2] intValue];
	int alpha = [(NSString *) [colors objectAtIndex:3] intValue];
	
	
	UIColor *color = [[[UIColor alloc] initWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha/100.0] autorelease];
	
	return color;
}
	




@end
