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

#import "JavenIncome.h"
#import "JavenReseller.h"


NSString *const kJavenIncomeReseller = @"reseller";
NSString *const kJavenIncomeIncomeLevel = @"incomeLevel";
NSString *const kJavenIncomeAmount = @"amount";


@interface JavenIncome ()

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

@end

@implementation JavenIncome

@synthesize reseller = _reseller;
@synthesize incomeLevel = _incomeLevel;
@synthesize amount = _amount;


+ (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.reseller = [JavenReseller modelObjectWithDictionary:[dict objectForKey:kJavenIncomeReseller]];
            self.incomeLevel = [[self objectOrNilForKey:kJavenIncomeIncomeLevel fromDictionary:dict] doubleValue];
            self.amount = [[self objectOrNilForKey:kJavenIncomeAmount fromDictionary:dict] doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[self.reseller dictionaryRepresentation] forKey:kJavenIncomeReseller];
    [mutableDict setValue:[NSNumber numberWithDouble:self.incomeLevel] forKey:kJavenIncomeIncomeLevel];
    [mutableDict setValue:[NSNumber numberWithDouble:self.amount] forKey:kJavenIncomeAmount];

    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.reseller = [aDecoder decodeObjectForKey:kJavenIncomeReseller];
    self.incomeLevel = [aDecoder decodeDoubleForKey:kJavenIncomeIncomeLevel];
    self.amount = [aDecoder decodeDoubleForKey:kJavenIncomeAmount];
    return self;
}

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

    [aCoder encodeObject:_reseller forKey:kJavenIncomeReseller];
    [aCoder encodeDouble:_incomeLevel forKey:kJavenIncomeIncomeLevel];
    [aCoder encodeDouble:_amount forKey:kJavenIncomeAmount];
}

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

        copy.reseller = [self.reseller copyWithZone:zone];
        copy.incomeLevel = self.incomeLevel;
        copy.amount = self.amount;
    }
    
    return copy;
}


@end
