#import "RecipeIngredientToIngredient.h"


@implementation RecipeIngredientToIngredient

- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject*)source 
                                      entityMapping:(NSEntityMapping*)mapping 
                                            manager:(NSMigrationManager*)manager 
                                              error:(NSError**)error
{
  //START:createDestinationInstancesForSourceInstance1
  NSManagedObjectContext *destMOC = [manager destinationContext];
  NSString *destEntityName = [mapping destinationEntityName];
  
  //The name of the ingredient
  NSString *name = [source valueForKey:@"name"];
  //END:createDestinationInstancesForSourceInstance1
  
  //START:createDestinationInstancesForSourceInstance2
  NSMutableDictionary *userInfo = (NSMutableDictionary*)[manager userInfo];
  if (!userInfo) {
    userInfo = [NSMutableDictionary dictionary];
    [manager setUserInfo:userInfo];
  }
  NSMutableDictionary *ingredientLookup = [userInfo valueForKey:@"ingredients"];
  if (!ingredientLookup) {
    ingredientLookup = [NSMutableDictionary dictionary];
    [userInfo setValue:ingredientLookup forKey:@"ingredients"];
  }
  NSManagedObject *dest = [ingredientLookup valueForKey:name];
  if (!dest) {
    dest = [NSEntityDescription insertNewObjectForEntityForName:destEntityName
                                         inManagedObjectContext:destMOC];
    [dest setValue:name forKey:@"name"];
    [ingredientLookup setValue:dest forKey:name];
  }
  //END:createDestinationInstancesForSourceInstance2
  
  //START:createDestinationInstancesForSourceInstance3
  [manager associateSourceInstance:source
           withDestinationInstance:dest
                  forEntityMapping:mapping];
  
  return YES;
  //END:createDestinationInstancesForSourceInstance3
}
//END:createDestinationInstancesForSourceInstance

//START:createRelationshipsForDestinationInstance
- (BOOL)createRelationshipsForDestinationInstance:(NSManagedObject*)dInstance 
                                    entityMapping:(NSEntityMapping*)mapping 
                                          manager:(NSMigrationManager*)manager 
                                            error:(NSError**)error
{
  return YES;
}
//END:createRelationshipsForDestinationInstance

@end
