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

#import "JavenOrder.h"


NSString *const kJavenOrderName = @"name";
NSString *const kJavenOrderUuid = @"uuid";
NSString *const kJavenOrderCode = @"code";


@interface JavenOrder ()

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

@end

@implementation JavenOrder

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


+ (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:kJavenOrderName fromDictionary:dict];
            self.uuid = [self objectOrNilForKey:kJavenOrderUuid fromDictionary:dict];
            self.code = [self objectOrNilForKey:kJavenOrderCode fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.name forKey:kJavenOrderName];
    [mutableDict setValue:self.uuid forKey:kJavenOrderUuid];
    [mutableDict setValue:self.code forKey:kJavenOrderCode];

    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:kJavenOrderName];
    self.uuid = [aDecoder decodeObjectForKey:kJavenOrderUuid];
    self.code = [aDecoder decodeObjectForKey:kJavenOrderCode];
    return self;
}

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

    [aCoder encodeObject:_name forKey:kJavenOrderName];
    [aCoder encodeObject:_uuid forKey:kJavenOrderUuid];
    [aCoder encodeObject:_code forKey:kJavenOrderCode];
}

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

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


@end
