

#import "SWGPetManagedObjectBuilder.h"

/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

@implementation SWGPetManagedObjectBuilder

-(instancetype)init {
    self = [super init];
    if (self != nil) {
       _categoryBuilder = [[SWGCategoryManagedObjectBuilder alloc] init];
       _tagsBuilder = [[SWGTagManagedObjectBuilder alloc] init];
    }
    return self;
}

-(SWGPetManagedObject*)createNewSWGPetManagedObjectInContext:(NSManagedObjectContext*)context {
    SWGPetManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([SWGPetManagedObject class]) inManagedObjectContext:context];
    return managedObject;
}

-(SWGPetManagedObject*)SWGPetManagedObjectFromSWGPet:(SWGPet*)object context:(NSManagedObjectContext*)context {
    SWGPetManagedObject* newSWGPet = [self createNewSWGPetManagedObjectInContext:context];
    [self updateSWGPetManagedObject:newSWGPet withSWGPet:object];
    return newSWGPet;
}

-(void)updateSWGPetManagedObject:(SWGPetManagedObject*)managedObject withSWGPet:(SWGPet*)object {
    if(!managedObject || !object) {
        return;
    }
    NSManagedObjectContext* context = managedObject.managedObjectContext;
    managedObject._id = [object._id copy];

    if(!managedObject.category) {
        managedObject.category = [self.categoryBuilder SWGCategoryManagedObjectFromSWGCategory:object.category context:managedObject.managedObjectContext];
    } else {
        [self.categoryBuilder updateSWGCategoryManagedObject:managedObject.category withSWGCategory:object.category];
    }
    managedObject.name = [object.name copy];
    managedObject.photoUrls = [object.photoUrls copy];
    if(managedObject.tags) {
        for (id object in managedObject.tags) {
            [context deleteObject:object];
        }
    }
    if(object.tags) {
        NSMutableOrderedSet * convertedObjs = [NSMutableOrderedSet orderedSet];
        for (id innerObject in object.tags) {
            id convertedObj = [self.tagsBuilder SWGTagManagedObjectFromSWGTag:innerObject context:managedObject.managedObjectContext];
            [convertedObjs addObject:convertedObj];
        }
        managedObject.tags = convertedObjs;
    }
    managedObject.status = [object.status copy];

}

-(SWGPet*)SWGPetFromSWGPetManagedObject:(SWGPetManagedObject*)obj {
    if(!obj) {
        return nil;
    }
    SWGPet* newSWGPet = [[SWGPet alloc] init];
    [self updateSWGPet:newSWGPet withSWGPetManagedObject:obj];
    return newSWGPet;
}

-(void)updateSWGPet:(SWGPet*)newSWGPet withSWGPetManagedObject:(SWGPetManagedObject*)obj {
    newSWGPet._id = [obj._id copy];
    newSWGPet.category = [self.categoryBuilder SWGCategoryFromSWGCategoryManagedObject:obj.category];
    newSWGPet.name = [obj.name copy];
    newSWGPet.photoUrls = [obj.photoUrls copy];
    if(obj.tags != nil) {
        NSMutableArray* convertedObjs = [NSMutableArray array];
        for (id innerObject in obj.tags) {
            id convertedObj = [self.tagsBuilder SWGTagFromSWGTagManagedObject:innerObject];
            [convertedObjs addObject:convertedObj];
        }
        newSWGPet.tags = (NSArray<SWGTag>*)convertedObjs;
    }
    
    newSWGPet.status = [obj.status copy];
}

@end
