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

#import "JavenCustomer.h"
#import "JavenConsumer.h"


NSString *const kJavenCustomerConsumer = @"consumer";
NSString *const kJavenCustomerOrderCount = @"orderCount";
NSString *const kJavenCustomerCommissionTotal = @"commissionTotal";


@interface JavenCustomer ()

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

@end

@implementation JavenCustomer

@synthesize consumer = _consumer;
@synthesize orderCount = _orderCount;
@synthesize commissionTotal = _commissionTotal;


+ (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.consumer = [JavenConsumer modelObjectWithDictionary:[dict objectForKey:kJavenCustomerConsumer]];
            self.orderCount = [[self objectOrNilForKey:kJavenCustomerOrderCount fromDictionary:dict] doubleValue];
            self.commissionTotal = [[self objectOrNilForKey:kJavenCustomerCommissionTotal fromDictionary:dict] doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[self.consumer dictionaryRepresentation] forKey:kJavenCustomerConsumer];
    [mutableDict setValue:[NSNumber numberWithDouble:self.orderCount] forKey:kJavenCustomerOrderCount];
    [mutableDict setValue:[NSNumber numberWithDouble:self.commissionTotal] forKey:kJavenCustomerCommissionTotal];

    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.consumer = [aDecoder decodeObjectForKey:kJavenCustomerConsumer];
    self.orderCount = [aDecoder decodeDoubleForKey:kJavenCustomerOrderCount];
    self.commissionTotal = [aDecoder decodeDoubleForKey:kJavenCustomerCommissionTotal];
    return self;
}

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

    [aCoder encodeObject:_consumer forKey:kJavenCustomerConsumer];
    [aCoder encodeDouble:_orderCount forKey:kJavenCustomerOrderCount];
    [aCoder encodeDouble:_commissionTotal forKey:kJavenCustomerCommissionTotal];
}

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

        copy.consumer = [self.consumer copyWithZone:zone];
        copy.orderCount = self.orderCount;
        copy.commissionTotal = self.commissionTotal;
    }
    
    return copy;
}


@end
