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

#import "JavenGoodsDetails.h"



NSString *const kJavenGoodsDetailsOrder = @"order";
NSString *const kJavenGoodsDetailsTax = @"tax";
NSString *const kJavenGoodsDetailsCount = @"count";
NSString *const kJavenGoodsDetailsPrice = @"price";
NSString *const kJavenGoodsDetailsCommission = @"commission";
NSString *const kJavenGoodsDetailsGoods = @"goods";
NSString *const kJavenGoodsDetailsUuid = @"uuid";


@interface JavenGoodsDetails ()

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

@end

@implementation JavenGoodsDetails

@synthesize order = _order;
@synthesize tax = _tax;
@synthesize count = _count;
@synthesize price = _price;
@synthesize commission = _commission;
@synthesize goods = _goods;
@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.order = [[self objectOrNilForKey:kJavenGoodsDetailsOrder fromDictionary:dict] doubleValue];
            self.tax = [[self objectOrNilForKey:kJavenGoodsDetailsTax fromDictionary:dict] doubleValue];
            self.count = [[self objectOrNilForKey:kJavenGoodsDetailsCount fromDictionary:dict] doubleValue];
            self.price = [[self objectOrNilForKey:kJavenGoodsDetailsPrice fromDictionary:dict] doubleValue];
            self.commission = [[self objectOrNilForKey:kJavenGoodsDetailsCommission fromDictionary:dict] doubleValue];
            self.goods = [JavenGoods modelObjectWithDictionary:[dict objectForKey:kJavenGoodsDetailsGoods]];
            self.uuid = [self objectOrNilForKey:kJavenGoodsDetailsUuid fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithDouble:self.order] forKey:kJavenGoodsDetailsOrder];
    [mutableDict setValue:[NSNumber numberWithDouble:self.tax] forKey:kJavenGoodsDetailsTax];
    [mutableDict setValue:[NSNumber numberWithDouble:self.count] forKey:kJavenGoodsDetailsCount];
    [mutableDict setValue:[NSNumber numberWithDouble:self.price] forKey:kJavenGoodsDetailsPrice];
    [mutableDict setValue:[NSNumber numberWithDouble:self.commission] forKey:kJavenGoodsDetailsCommission];
    [mutableDict setValue:[self.goods dictionaryRepresentation] forKey:kJavenGoodsDetailsGoods];
    [mutableDict setValue:self.uuid forKey:kJavenGoodsDetailsUuid];

    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.order = [aDecoder decodeDoubleForKey:kJavenGoodsDetailsOrder];
    self.tax = [aDecoder decodeDoubleForKey:kJavenGoodsDetailsTax];
    self.count = [aDecoder decodeDoubleForKey:kJavenGoodsDetailsCount];
    self.price = [aDecoder decodeDoubleForKey:kJavenGoodsDetailsPrice];
    self.commission = [aDecoder decodeDoubleForKey:kJavenGoodsDetailsCommission];
    self.goods = [aDecoder decodeObjectForKey:kJavenGoodsDetailsGoods];
    self.uuid = [aDecoder decodeObjectForKey:kJavenGoodsDetailsUuid];
    return self;
}

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

    [aCoder encodeDouble:_order forKey:kJavenGoodsDetailsOrder];
    [aCoder encodeDouble:_tax forKey:kJavenGoodsDetailsTax];
    [aCoder encodeDouble:_count forKey:kJavenGoodsDetailsCount];
    [aCoder encodeDouble:_price forKey:kJavenGoodsDetailsPrice];
    [aCoder encodeDouble:_commission forKey:kJavenGoodsDetailsCommission];
    [aCoder encodeObject:_goods forKey:kJavenGoodsDetailsGoods];
    [aCoder encodeObject:_uuid forKey:kJavenGoodsDetailsUuid];
}

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

        copy.order = self.order;
        copy.tax = self.tax;
        copy.count = self.count;
        copy.price = self.price;
        copy.commission = self.commission;
        copy.goods = [self.goods copyWithZone:zone];
        copy.uuid = [self.uuid copyWithZone:zone];
    }
    
    return copy;
}


@end
