#import "RentalManagerAPI.h"

NSString * const RMAErrorDomain = @"pl.moj-wspanialy-menedzer-nieruchomosci.API";
NSString * const RMAMissingValuesKey = @"RMAMissingValuesKey";
NSString * const RMAAccountExpirationDateKey = @"RMAAccountExpirationDateKey";

@implementation RentalManagerAPI

+ (BOOL)publishAd:(NSDictionary *)anAd error:(NSError **)anError
{
   if (anAd == nil) {
      @throw [NSException exceptionWithName:@"RMABadAPICall" 
                                     reason:@"Słownik ma wartość nil" 
                                   userInfo:nil];
   }

   NSMutableArray *missingValues = [NSMutableArray array];
   for (NSString *key in [@"name price city" componentsSeparatedByString:@" "]) {
      if ([[anAd objectForKey:key] length] == 0) {
          [missingValues addObject:key];
      }
   }

   if ([missingValues count] > 0) {
      if (anError != NULL) {
         NSString *description = @"Reklama nie mogła zostać \
                              Opublikowana, ponieważ brakuje \
                              pewnych wymaganych wartości.";
         NSString *recoverySuggestion = @"Proszę podać brakujące \
                               wartości i spróbować ponownie.";
         NSArray *keys = [NSArray arrayWithObjects: 
                           NSLocalizedDescriptionKey,
                           NSLocalizedRecoverySuggestionErrorKey,
                           RMAMissingValuesKey, nil];
         NSArray *values = [NSArray arrayWithObjects:
                             description,
                             recoverySuggestion,
                             missingValues, nil];
         NSDictionary *userDict = [NSDictionary dictionaryWithObjects:values 
                                                              forKeys:keys];
         *anError = [[[NSError alloc] initWithDomain:RMAErrorDomain
                                                 code:RMAValidationError 
                                             userInfo:userDict] autorelease];
      }
      return NO;
   } else {
      return YES;
   }
}

@end
