//
//  CommotityListCategory.m
//
//  Created by Z  on 16/4/27
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "CommotityListCategory.h"


NSString *const kCommotityListCategoryName = @"name";
NSString *const kCommotityListCategoryUuid = @"uuid";
NSString *const kCommotityListCategoryCode = @"code";


@interface CommotityListCategory ()

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

@end

@implementation CommotityListCategory

@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:kCommotityListCategoryName fromDictionary:dict];
            self.uuid = [self objectOrNilForKey:kCommotityListCategoryUuid fromDictionary:dict];
            self.code = [self objectOrNilForKey:kCommotityListCategoryCode fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.name forKey:kCommotityListCategoryName];
    [mutableDict setValue:self.uuid forKey:kCommotityListCategoryUuid];
    [mutableDict setValue:self.code forKey:kCommotityListCategoryCode];

    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:kCommotityListCategoryName];
    self.uuid = [aDecoder decodeObjectForKey:kCommotityListCategoryUuid];
    self.code = [aDecoder decodeObjectForKey:kCommotityListCategoryCode];
    return self;
}

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

    [aCoder encodeObject:_name forKey:kCommotityListCategoryName];
    [aCoder encodeObject:_uuid forKey:kCommotityListCategoryUuid];
    [aCoder encodeObject:_code forKey:kCommotityListCategoryCode];
}

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