//
//  JavenRoles.m
//
//  Created by Z  on 16/5/11
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "JavenRoles.h"
#import "JavenLastModifyInfo.h"
#import "JavenResources.h"
#import "JavenCreateInfo.h"


NSString *const kJavenRolesDomain = @"domain";
NSString *const kJavenRolesLastModifyInfo = @"lastModifyInfo";
NSString *const kJavenRolesOrder = @"order";
NSString *const kJavenRolesResources = @"resources";
NSString *const kJavenRolesUuid = @"uuid";
NSString *const kJavenRolesCreateInfo = @"createInfo";
NSString *const kJavenRolesVersion = @"version";
NSString *const kJavenRolesDescription = @"description";
NSString *const kJavenRolesName = @"name";
NSString *const kJavenRolesState = @"state";


@interface JavenRoles ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation JavenRoles

@synthesize domain = _domain;
@synthesize lastModifyInfo = _lastModifyInfo;
@synthesize order = _order;
@synthesize resources = _resources;
@synthesize uuid = _uuid;
@synthesize createInfo = _createInfo;
@synthesize version = _version;
@synthesize rolesDescription = _rolesDescription;
@synthesize name = _name;
@synthesize state = _state;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.domain = [self objectOrNilForKey:kJavenRolesDomain fromDictionary:dict];
            self.lastModifyInfo = [JavenLastModifyInfo modelObjectWithDictionary:[dict objectForKey:kJavenRolesLastModifyInfo]];
            self.order = [[self objectOrNilForKey:kJavenRolesOrder fromDictionary:dict] doubleValue];
    NSObject *receivedJavenResources = [dict objectForKey:kJavenRolesResources];
    NSMutableArray *parsedJavenResources = [NSMutableArray array];
    if ([receivedJavenResources isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedJavenResources) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedJavenResources addObject:[JavenResources modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedJavenResources isKindOfClass:[NSDictionary class]]) {
       [parsedJavenResources addObject:[JavenResources modelObjectWithDictionary:(NSDictionary *)receivedJavenResources]];
    }

    self.resources = [NSArray arrayWithArray:parsedJavenResources];
            self.uuid = [self objectOrNilForKey:kJavenRolesUuid fromDictionary:dict];
            self.createInfo = [JavenCreateInfo modelObjectWithDictionary:[dict objectForKey:kJavenRolesCreateInfo]];
            self.version = [[self objectOrNilForKey:kJavenRolesVersion fromDictionary:dict] doubleValue];
            self.rolesDescription = [self objectOrNilForKey:kJavenRolesDescription fromDictionary:dict];
            self.name = [self objectOrNilForKey:kJavenRolesName fromDictionary:dict];
            self.state = [self objectOrNilForKey:kJavenRolesState fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.domain forKey:kJavenRolesDomain];
    [mutableDict setValue:[self.lastModifyInfo dictionaryRepresentation] forKey:kJavenRolesLastModifyInfo];
    [mutableDict setValue:[NSNumber numberWithDouble:self.order] forKey:kJavenRolesOrder];
    NSMutableArray *tempArrayForResources = [NSMutableArray array];
    for (NSObject *subArrayObject in self.resources) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForResources addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForResources addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForResources] forKey:kJavenRolesResources];
    [mutableDict setValue:self.uuid forKey:kJavenRolesUuid];
    [mutableDict setValue:[self.createInfo dictionaryRepresentation] forKey:kJavenRolesCreateInfo];
    [mutableDict setValue:[NSNumber numberWithDouble:self.version] forKey:kJavenRolesVersion];
    [mutableDict setValue:self.rolesDescription forKey:kJavenRolesDescription];
    [mutableDict setValue:self.name forKey:kJavenRolesName];
    [mutableDict setValue:self.state forKey:kJavenRolesState];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.domain = [aDecoder decodeObjectForKey:kJavenRolesDomain];
    self.lastModifyInfo = [aDecoder decodeObjectForKey:kJavenRolesLastModifyInfo];
    self.order = [aDecoder decodeDoubleForKey:kJavenRolesOrder];
    self.resources = [aDecoder decodeObjectForKey:kJavenRolesResources];
    self.uuid = [aDecoder decodeObjectForKey:kJavenRolesUuid];
    self.createInfo = [aDecoder decodeObjectForKey:kJavenRolesCreateInfo];
    self.version = [aDecoder decodeDoubleForKey:kJavenRolesVersion];
    self.rolesDescription = [aDecoder decodeObjectForKey:kJavenRolesDescription];
    self.name = [aDecoder decodeObjectForKey:kJavenRolesName];
    self.state = [aDecoder decodeObjectForKey:kJavenRolesState];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_domain forKey:kJavenRolesDomain];
    [aCoder encodeObject:_lastModifyInfo forKey:kJavenRolesLastModifyInfo];
    [aCoder encodeDouble:_order forKey:kJavenRolesOrder];
    [aCoder encodeObject:_resources forKey:kJavenRolesResources];
    [aCoder encodeObject:_uuid forKey:kJavenRolesUuid];
    [aCoder encodeObject:_createInfo forKey:kJavenRolesCreateInfo];
    [aCoder encodeDouble:_version forKey:kJavenRolesVersion];
    [aCoder encodeObject:_rolesDescription forKey:kJavenRolesDescription];
    [aCoder encodeObject:_name forKey:kJavenRolesName];
    [aCoder encodeObject:_state forKey:kJavenRolesState];
}

- (id)copyWithZone:(NSZone *)zone
{
    JavenRoles *copy = [[JavenRoles alloc] init];
    
    if (copy) {

        copy.domain = [self.domain copyWithZone:zone];
        copy.lastModifyInfo = [self.lastModifyInfo copyWithZone:zone];
        copy.order = self.order;
        copy.resources = [self.resources copyWithZone:zone];
        copy.uuid = [self.uuid copyWithZone:zone];
        copy.createInfo = [self.createInfo copyWithZone:zone];
        copy.version = self.version;
        copy.rolesDescription = [self.rolesDescription copyWithZone:zone];
        copy.name = [self.name copyWithZone:zone];
        copy.state = [self.state copyWithZone:zone];
    }
    
    return copy;
}


@end
