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

#import "JavenReferrer.h"


NSString *const kJavenReferrerName = @"name";
NSString *const kJavenReferrerCode = @"code";
NSString *const kJavenReferrerUuid = @"uuid";


@interface JavenReferrer ()

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

@end

@implementation JavenReferrer

@synthesize name = _name;
@synthesize code = _code;
@synthesize uuid = _uuid;


+ (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.name = [self objectOrNilForKey:kJavenReferrerName fromDictionary:dict];
            self.code = [self objectOrNilForKey:kJavenReferrerCode fromDictionary:dict];
            self.uuid = [self objectOrNilForKey:kJavenReferrerUuid fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.name forKey:kJavenReferrerName];
    [mutableDict setValue:self.code forKey:kJavenReferrerCode];
    [mutableDict setValue:self.uuid forKey:kJavenReferrerUuid];

    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.name = [aDecoder decodeObjectForKey:kJavenReferrerName];
    self.code = [aDecoder decodeObjectForKey:kJavenReferrerCode];
    self.uuid = [aDecoder decodeObjectForKey:kJavenReferrerUuid];
    return self;
}

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

    [aCoder encodeObject:_name forKey:kJavenReferrerName];
    [aCoder encodeObject:_code forKey:kJavenReferrerCode];
    [aCoder encodeObject:_uuid forKey:kJavenReferrerUuid];
}

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

        copy.name = [self.name copyWithZone:zone];
        copy.code = [self.code copyWithZone:zone];
        copy.uuid = [self.uuid copyWithZone:zone];
    }
    
    return copy;
}


@end
