//
//  BXSalesConfigurationDataAccess.h
//  BXServices
//
//  Created by Michael Ehrmann on 19.02.07.
//  Copyright 2007 Boinx Software. All rights reserved.
//


extern NSString * const UPDATE_DATE;
extern NSString * const MODELL_VERSION;


@interface BXSalesConfigurationDataAccess : NSObject {

	@private
	NSManagedObjectContext *_objectContext;
	NSManagedObjectModel *_objectModel;
	
	BOOL _loaded;
	BOOL _inLibrary;
	
	NSOperationQueue *_downloadQueue;
}

@property (assign, readonly, getter = isLoaded) BOOL loaded;


#pragma mark -
#pragma mark Access to products

/**	Method that finds a list of products with the specified parameters.
*	Note that nil is not a wildcard. Nil serchas for a nil field in the database.
*	Normally this method should only find one product with the same parameters.
*	@return NSArray with NSManagedObjects representing Products.
*/
- (NSArray *) allProductsWithName:(NSString*)inProductName
						  version:(NSNumber *)inVersion
						   option:(NSString *)inOption
							 type:(NSString *)inType
							seats:(NSNumber *)inSeats;


/** Method that finds a product with the specified parameters.
*	This method uses the previous method and returns the first object of the array
*	@see allProductsWithName:version:option:type:seats:
*/
- (NSManagedObject *) productWithName:(NSString*)inProductName
							  version:(NSNumber *)inVersion
							   option:(NSString *)inOption
								 type:(NSString *)inType
								seats:(NSNumber *)inSeats;

#pragma mark Product languages

/**	Method to find a list of available languages for a product
*	@return NSArray of languages.
*/
- (NSArray *) availableLanguagesForProduct:(NSManagedObject *)inProduct;


/** Method finds the best localisation for a product using system preferences.
*	@return NSManagedObject representing a ProductLocalisation
*/
- (NSManagedObject *)productLocalisationForProduct:(NSManagedObject *)inProduct;

#pragma mark Product updates

/** Finds all updates for a product.
*	The result is ordered by category and priority and only returns enabled products.
*	@return NSArray of NSManagedObjects representing products.
*/
- (NSArray *) allSortedProductUpdatesForProduct:(NSManagedObject *)inProduct;

#pragma mark KRM 

/** Method to construct the KRM initialisation XML for a specific product.
*	If the Product is nil or does not have any KRM data specified this method returns nil.
*	@return NSString with initialisation XML code inside.
*/
- (NSString *) krmXMLForProduct:(NSManagedObject *)inProduct;


#pragma mark -
#pragma mark Region handling

/** Method to find all availabe regions for a product.
*	@returns NSArray with all regions as NSStrings.
*/
- (NSArray *) availableStoreRegionsForProduct:(NSManagedObject *)inProduct;


/** Method that finds a prefered region for a NSLocale.
*	If no region is found for the current locale a default region is searched and returned.
*	The method logs a error to the console if this default region is not found. 
*	The name of the default region must be "Default".
*	@return NSString with the name of the prefered region.
*/
- (NSString *) preferedRegionForLocale:(NSLocale *)inLocale inRegions:(NSArray *)inRegions;


#pragma mark -
#pragma mark Access to stores

/**	Method to find all stores for a product in the current locale
*	@see allSortedStoresForProduct:inLocale:
*	@return NSArray containing NSManagedObjects representing store objects.
*/
- (NSArray *) allSortedStoresForProduct:(NSManagedObject *)inProduct;


/**	Method to find all stores for a product in a specified locale
*	The stores are sorted by category and priority. Only enabled stores are returned.
*	The region is found with the preferedRegionForLocale:inRegions: method.
*	@see allSortedStoresForProduct:
*	@return NSArray containing NSManagedObjects representing store objects.
*/
- (NSArray *) allSortedStoresForProduct:(NSManagedObject *)inProduct inLocale:(NSLocale *)inLocale;


- (NSString *)internalNameOfStore:(NSManagedObject *)inStore;

#pragma mark Store languages

/**	Method to find a list of available languages for a store
*	@return NSArray of languages.
*/
- (NSArray *) avaiableLanguagesForStore:(NSManagedObject *)inStore;


/** Method finds the best localisation for a store using system preferences.
*	@return NSManagedObject representing a ProductLocalisation
*/
- (NSManagedObject *)storeLocalisationForStore:(NSManagedObject *)inStore;

@end