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

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


NSString *const kUserInfoResourcesDomain = @"domain";
NSString *const kUserInfoResourcesUpperPath = @"upperPath";
NSString *const kUserInfoResourcesLastModifyInfo = @"lastModifyInfo";
NSString *const kUserInfoResourcesUuid = @"uuid";
NSString *const kUserInfoResourcesCreateInfo = @"createInfo";
NSString *const kUserInfoResourcesVersion = @"version";
NSString *const kUserInfoResourcesPath = @"path";
NSString *const kUserInfoResourcesName = @"name";
NSString *const kUserInfoResourcesCaption = @"caption";


@interface UserInfoResources ()

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

@end

@implementation UserInfoResources

@synthesize domain = _domain;
@synthesize upperPath = _upperPath;
@synthesize lastModifyInfo = _lastModifyInfo;
@synthesize uuid = _uuid;
@synthesize createInfo = _createInfo;
@synthesize version = _version;
@synthesize path = _path;
@synthesize name = _name;
@synthesize caption = _caption;


+ (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:kUserInfoResourcesDomain fromDictionary:dict];
            self.upperPath = [self objectOrNilForKey:kUserInfoResourcesUpperPath fromDictionary:dict];
            self.lastModifyInfo = [UserInfoLastModifyInfo modelObjectWithDictionary:[dict objectForKey:kUserInfoResourcesLastModifyInfo]];
            self.uuid = [self objectOrNilForKey:kUserInfoResourcesUuid fromDictionary:dict];
            self.createInfo = [UserInfoCreateInfo modelObjectWithDictionary:[dict objectForKey:kUserInfoResourcesCreateInfo]];
            self.version = [[self objectOrNilForKey:kUserInfoResourcesVersion fromDictionary:dict] doubleValue];
            self.path = [self objectOrNilForKey:kUserInfoResourcesPath fromDictionary:dict];
            self.name = [self objectOrNilForKey:kUserInfoResourcesName fromDictionary:dict];
            self.caption = [self objectOrNilForKey:kUserInfoResourcesCaption fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.domain forKey:kUserInfoResourcesDomain];
    [mutableDict setValue:self.upperPath forKey:kUserInfoResourcesUpperPath];
    [mutableDict setValue:[self.lastModifyInfo dictionaryRepresentation] forKey:kUserInfoResourcesLastModifyInfo];
    [mutableDict setValue:self.uuid forKey:kUserInfoResourcesUuid];
    [mutableDict setValue:[self.createInfo dictionaryRepresentation] forKey:kUserInfoResourcesCreateInfo];
    [mutableDict setValue:[NSNumber numberWithDouble:self.version] forKey:kUserInfoResourcesVersion];
    [mutableDict setValue:self.path forKey:kUserInfoResourcesPath];
    [mutableDict setValue:self.name forKey:kUserInfoResourcesName];
    [mutableDict setValue:self.caption forKey:kUserInfoResourcesCaption];

    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:kUserInfoResourcesDomain];
    self.upperPath = [aDecoder decodeObjectForKey:kUserInfoResourcesUpperPath];
    self.lastModifyInfo = [aDecoder decodeObjectForKey:kUserInfoResourcesLastModifyInfo];
    self.uuid = [aDecoder decodeObjectForKey:kUserInfoResourcesUuid];
    self.createInfo = [aDecoder decodeObjectForKey:kUserInfoResourcesCreateInfo];
    self.version = [aDecoder decodeDoubleForKey:kUserInfoResourcesVersion];
    self.path = [aDecoder decodeObjectForKey:kUserInfoResourcesPath];
    self.name = [aDecoder decodeObjectForKey:kUserInfoResourcesName];
    self.caption = [aDecoder decodeObjectForKey:kUserInfoResourcesCaption];
    return self;
}

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

    [aCoder encodeObject:_domain forKey:kUserInfoResourcesDomain];
    [aCoder encodeObject:_upperPath forKey:kUserInfoResourcesUpperPath];
    [aCoder encodeObject:_lastModifyInfo forKey:kUserInfoResourcesLastModifyInfo];
    [aCoder encodeObject:_uuid forKey:kUserInfoResourcesUuid];
    [aCoder encodeObject:_createInfo forKey:kUserInfoResourcesCreateInfo];
    [aCoder encodeDouble:_version forKey:kUserInfoResourcesVersion];
    [aCoder encodeObject:_path forKey:kUserInfoResourcesPath];
    [aCoder encodeObject:_name forKey:kUserInfoResourcesName];
    [aCoder encodeObject:_caption forKey:kUserInfoResourcesCaption];
}

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

        copy.domain = [self.domain copyWithZone:zone];
        copy.upperPath = [self.upperPath copyWithZone:zone];
        copy.lastModifyInfo = [self.lastModifyInfo copyWithZone:zone];
        copy.uuid = [self.uuid copyWithZone:zone];
        copy.createInfo = [self.createInfo copyWithZone:zone];
        copy.version = self.version;
        copy.path = [self.path copyWithZone:zone];
        copy.name = [self.name copyWithZone:zone];
        copy.caption = [self.caption copyWithZone:zone];
    }
    
    return copy;
}


@end
