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

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


NSString *const kJavenResourcesDomain = @"domain";
NSString *const kJavenResourcesUpperPath = @"upperPath";
NSString *const kJavenResourcesLastModifyInfo = @"lastModifyInfo";
NSString *const kJavenResourcesUuid = @"uuid";
NSString *const kJavenResourcesCreateInfo = @"createInfo";
NSString *const kJavenResourcesVersion = @"version";
NSString *const kJavenResourcesPath = @"path";
NSString *const kJavenResourcesName = @"name";
NSString *const kJavenResourcesCaption = @"caption";


@interface JavenResources ()

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

@end

@implementation JavenResources

@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:kJavenResourcesDomain fromDictionary:dict];
            self.upperPath = [self objectOrNilForKey:kJavenResourcesUpperPath fromDictionary:dict];
            self.lastModifyInfo = [JavenLastModifyInfo modelObjectWithDictionary:[dict objectForKey:kJavenResourcesLastModifyInfo]];
            self.uuid = [self objectOrNilForKey:kJavenResourcesUuid fromDictionary:dict];
            self.createInfo = [JavenCreateInfo modelObjectWithDictionary:[dict objectForKey:kJavenResourcesCreateInfo]];
            self.version = [[self objectOrNilForKey:kJavenResourcesVersion fromDictionary:dict] doubleValue];
            self.path = [self objectOrNilForKey:kJavenResourcesPath fromDictionary:dict];
            self.name = [self objectOrNilForKey:kJavenResourcesName fromDictionary:dict];
            self.caption = [self objectOrNilForKey:kJavenResourcesCaption fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.domain forKey:kJavenResourcesDomain];
    [mutableDict setValue:self.upperPath forKey:kJavenResourcesUpperPath];
    [mutableDict setValue:[self.lastModifyInfo dictionaryRepresentation] forKey:kJavenResourcesLastModifyInfo];
    [mutableDict setValue:self.uuid forKey:kJavenResourcesUuid];
    [mutableDict setValue:[self.createInfo dictionaryRepresentation] forKey:kJavenResourcesCreateInfo];
    [mutableDict setValue:[NSNumber numberWithDouble:self.version] forKey:kJavenResourcesVersion];
    [mutableDict setValue:self.path forKey:kJavenResourcesPath];
    [mutableDict setValue:self.name forKey:kJavenResourcesName];
    [mutableDict setValue:self.caption forKey:kJavenResourcesCaption];

    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:kJavenResourcesDomain];
    self.upperPath = [aDecoder decodeObjectForKey:kJavenResourcesUpperPath];
    self.lastModifyInfo = [aDecoder decodeObjectForKey:kJavenResourcesLastModifyInfo];
    self.uuid = [aDecoder decodeObjectForKey:kJavenResourcesUuid];
    self.createInfo = [aDecoder decodeObjectForKey:kJavenResourcesCreateInfo];
    self.version = [aDecoder decodeDoubleForKey:kJavenResourcesVersion];
    self.path = [aDecoder decodeObjectForKey:kJavenResourcesPath];
    self.name = [aDecoder decodeObjectForKey:kJavenResourcesName];
    self.caption = [aDecoder decodeObjectForKey:kJavenResourcesCaption];
    return self;
}

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

    [aCoder encodeObject:_domain forKey:kJavenResourcesDomain];
    [aCoder encodeObject:_upperPath forKey:kJavenResourcesUpperPath];
    [aCoder encodeObject:_lastModifyInfo forKey:kJavenResourcesLastModifyInfo];
    [aCoder encodeObject:_uuid forKey:kJavenResourcesUuid];
    [aCoder encodeObject:_createInfo forKey:kJavenResourcesCreateInfo];
    [aCoder encodeDouble:_version forKey:kJavenResourcesVersion];
    [aCoder encodeObject:_path forKey:kJavenResourcesPath];
    [aCoder encodeObject:_name forKey:kJavenResourcesName];
    [aCoder encodeObject:_caption forKey:kJavenResourcesCaption];
}

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