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

#import "UserInfoRoles.h"
#import "UserInfoLastModifyInfo.h"
#import "UserInfoResources.h"
#import "UserInfoCreateInfo.h"


NSString *const kUserInfoRolesDomain = @"domain";
NSString *const kUserInfoRolesLastModifyInfo = @"lastModifyInfo";
NSString *const kUserInfoRolesOrder = @"order";
NSString *const kUserInfoRolesResources = @"resources";
NSString *const kUserInfoRolesUuid = @"uuid";
NSString *const kUserInfoRolesCreateInfo = @"createInfo";
NSString *const kUserInfoRolesVersion = @"version";
NSString *const kUserInfoRolesDescription = @"description";
NSString *const kUserInfoRolesName = @"name";
NSString *const kUserInfoRolesState = @"state";


@interface UserInfoRoles ()

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

@end

@implementation UserInfoRoles

@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:kUserInfoRolesDomain fromDictionary:dict];
            self.lastModifyInfo = [UserInfoLastModifyInfo modelObjectWithDictionary:[dict objectForKey:kUserInfoRolesLastModifyInfo]];
            self.order = [[self objectOrNilForKey:kUserInfoRolesOrder fromDictionary:dict] doubleValue];
    NSObject *receivedUserInfoResources = [dict objectForKey:kUserInfoRolesResources];
    NSMutableArray *parsedUserInfoResources = [NSMutableArray array];
    if ([receivedUserInfoResources isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedUserInfoResources) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedUserInfoResources addObject:[UserInfoResources modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedUserInfoResources isKindOfClass:[NSDictionary class]]) {
       [parsedUserInfoResources addObject:[UserInfoResources modelObjectWithDictionary:(NSDictionary *)receivedUserInfoResources]];
    }

    self.resources = [NSArray arrayWithArray:parsedUserInfoResources];
            self.uuid = [self objectOrNilForKey:kUserInfoRolesUuid fromDictionary:dict];
            self.createInfo = [UserInfoCreateInfo modelObjectWithDictionary:[dict objectForKey:kUserInfoRolesCreateInfo]];
            self.version = [[self objectOrNilForKey:kUserInfoRolesVersion fromDictionary:dict] doubleValue];
            self.rolesDescription = [self objectOrNilForKey:kUserInfoRolesDescription fromDictionary:dict];
            self.name = [self objectOrNilForKey:kUserInfoRolesName fromDictionary:dict];
            self.state = [self objectOrNilForKey:kUserInfoRolesState fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.domain forKey:kUserInfoRolesDomain];
    [mutableDict setValue:[self.lastModifyInfo dictionaryRepresentation] forKey:kUserInfoRolesLastModifyInfo];
    [mutableDict setValue:[NSNumber numberWithDouble:self.order] forKey:kUserInfoRolesOrder];
    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:kUserInfoRolesResources];
    [mutableDict setValue:self.uuid forKey:kUserInfoRolesUuid];
    [mutableDict setValue:[self.createInfo dictionaryRepresentation] forKey:kUserInfoRolesCreateInfo];
    [mutableDict setValue:[NSNumber numberWithDouble:self.version] forKey:kUserInfoRolesVersion];
    [mutableDict setValue:self.rolesDescription forKey:kUserInfoRolesDescription];
    [mutableDict setValue:self.name forKey:kUserInfoRolesName];
    [mutableDict setValue:self.state forKey:kUserInfoRolesState];

    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:kUserInfoRolesDomain];
    self.lastModifyInfo = [aDecoder decodeObjectForKey:kUserInfoRolesLastModifyInfo];
    self.order = [aDecoder decodeDoubleForKey:kUserInfoRolesOrder];
    self.resources = [aDecoder decodeObjectForKey:kUserInfoRolesResources];
    self.uuid = [aDecoder decodeObjectForKey:kUserInfoRolesUuid];
    self.createInfo = [aDecoder decodeObjectForKey:kUserInfoRolesCreateInfo];
    self.version = [aDecoder decodeDoubleForKey:kUserInfoRolesVersion];
    self.rolesDescription = [aDecoder decodeObjectForKey:kUserInfoRolesDescription];
    self.name = [aDecoder decodeObjectForKey:kUserInfoRolesName];
    self.state = [aDecoder decodeObjectForKey:kUserInfoRolesState];
    return self;
}

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

    [aCoder encodeObject:_domain forKey:kUserInfoRolesDomain];
    [aCoder encodeObject:_lastModifyInfo forKey:kUserInfoRolesLastModifyInfo];
    [aCoder encodeDouble:_order forKey:kUserInfoRolesOrder];
    [aCoder encodeObject:_resources forKey:kUserInfoRolesResources];
    [aCoder encodeObject:_uuid forKey:kUserInfoRolesUuid];
    [aCoder encodeObject:_createInfo forKey:kUserInfoRolesCreateInfo];
    [aCoder encodeDouble:_version forKey:kUserInfoRolesVersion];
    [aCoder encodeObject:_rolesDescription forKey:kUserInfoRolesDescription];
    [aCoder encodeObject:_name forKey:kUserInfoRolesName];
    [aCoder encodeObject:_state forKey:kUserInfoRolesState];
}

- (id)copyWithZone:(NSZone *)zone
{
    UserInfoRoles *copy = [[UserInfoRoles 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
