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

#import "JavencommissionAccountModel.h"
#import "JavenReseller.h"


NSString *const kJavencommissionAccountModelBalance = @"balance";
NSString *const kJavencommissionAccountModelMonthlyOrderCount = @"monthlyOrderCount";
NSString *const kJavencommissionAccountModelMonthlySales = @"monthlySales";
NSString *const kJavencommissionAccountModelReseller = @"reseller";
NSString *const kJavencommissionAccountModelUuid = @"uuid";
NSString *const kJavencommissionAccountModelAccumulatedIncome = @"accumulatedIncome";
NSString *const kJavencommissionAccountModelMonthlyIncome = @"monthlyIncome";
NSString *const kJavencommissionAccountModelUnconfirmedBalance = @"unconfirmedBalance";
NSString *const kJavencommissionAccountModelWithdrawAmount = @"withdrawAmount";


@interface JavencommissionAccountModel ()

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

@end

@implementation JavencommissionAccountModel

@synthesize balance = _balance;
@synthesize monthlyOrderCount = _monthlyOrderCount;
@synthesize monthlySales = _monthlySales;
@synthesize reseller = _reseller;
@synthesize uuid = _uuid;
@synthesize accumulatedIncome = _accumulatedIncome;
@synthesize monthlyIncome = _monthlyIncome;
@synthesize unconfirmedBalance = _unconfirmedBalance;
@synthesize withdrawAmount = _withdrawAmount;


+ (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.balance = [[self objectOrNilForKey:kJavencommissionAccountModelBalance fromDictionary:dict] doubleValue];
            self.monthlyOrderCount = [[self objectOrNilForKey:kJavencommissionAccountModelMonthlyOrderCount fromDictionary:dict] doubleValue];
            self.monthlySales = [[self objectOrNilForKey:kJavencommissionAccountModelMonthlySales fromDictionary:dict] doubleValue];
            self.reseller = [JavenReseller modelObjectWithDictionary:[dict objectForKey:kJavencommissionAccountModelReseller]];
            self.uuid = [self objectOrNilForKey:kJavencommissionAccountModelUuid fromDictionary:dict];
            self.accumulatedIncome = [[self objectOrNilForKey:kJavencommissionAccountModelAccumulatedIncome fromDictionary:dict] doubleValue];
            self.monthlyIncome = [[self objectOrNilForKey:kJavencommissionAccountModelMonthlyIncome fromDictionary:dict] doubleValue];
            self.unconfirmedBalance = [[self objectOrNilForKey:kJavencommissionAccountModelUnconfirmedBalance fromDictionary:dict] doubleValue];
            self.withdrawAmount = [[self objectOrNilForKey:kJavencommissionAccountModelWithdrawAmount fromDictionary:dict] doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithDouble:self.balance] forKey:kJavencommissionAccountModelBalance];
    [mutableDict setValue:[NSNumber numberWithDouble:self.monthlyOrderCount] forKey:kJavencommissionAccountModelMonthlyOrderCount];
    [mutableDict setValue:[NSNumber numberWithDouble:self.monthlySales] forKey:kJavencommissionAccountModelMonthlySales];
    [mutableDict setValue:[self.reseller dictionaryRepresentation] forKey:kJavencommissionAccountModelReseller];
    [mutableDict setValue:self.uuid forKey:kJavencommissionAccountModelUuid];
    [mutableDict setValue:[NSNumber numberWithDouble:self.accumulatedIncome] forKey:kJavencommissionAccountModelAccumulatedIncome];
    [mutableDict setValue:[NSNumber numberWithDouble:self.monthlyIncome] forKey:kJavencommissionAccountModelMonthlyIncome];
    [mutableDict setValue:[NSNumber numberWithDouble:self.unconfirmedBalance] forKey:kJavencommissionAccountModelUnconfirmedBalance];
    [mutableDict setValue:[NSNumber numberWithDouble:self.withdrawAmount] forKey:kJavencommissionAccountModelWithdrawAmount];

    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.balance = [aDecoder decodeDoubleForKey:kJavencommissionAccountModelBalance];
    self.monthlyOrderCount = [aDecoder decodeDoubleForKey:kJavencommissionAccountModelMonthlyOrderCount];
    self.monthlySales = [aDecoder decodeDoubleForKey:kJavencommissionAccountModelMonthlySales];
    self.reseller = [aDecoder decodeObjectForKey:kJavencommissionAccountModelReseller];
    self.uuid = [aDecoder decodeObjectForKey:kJavencommissionAccountModelUuid];
    self.accumulatedIncome = [aDecoder decodeDoubleForKey:kJavencommissionAccountModelAccumulatedIncome];
    self.monthlyIncome = [aDecoder decodeDoubleForKey:kJavencommissionAccountModelMonthlyIncome];
    self.unconfirmedBalance = [aDecoder decodeDoubleForKey:kJavencommissionAccountModelUnconfirmedBalance];
    self.withdrawAmount = [aDecoder decodeDoubleForKey:kJavencommissionAccountModelWithdrawAmount];
    return self;
}

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

    [aCoder encodeDouble:_balance forKey:kJavencommissionAccountModelBalance];
    [aCoder encodeDouble:_monthlyOrderCount forKey:kJavencommissionAccountModelMonthlyOrderCount];
    [aCoder encodeDouble:_monthlySales forKey:kJavencommissionAccountModelMonthlySales];
    [aCoder encodeObject:_reseller forKey:kJavencommissionAccountModelReseller];
    [aCoder encodeObject:_uuid forKey:kJavencommissionAccountModelUuid];
    [aCoder encodeDouble:_accumulatedIncome forKey:kJavencommissionAccountModelAccumulatedIncome];
    [aCoder encodeDouble:_monthlyIncome forKey:kJavencommissionAccountModelMonthlyIncome];
    [aCoder encodeDouble:_unconfirmedBalance forKey:kJavencommissionAccountModelUnconfirmedBalance];
    [aCoder encodeDouble:_withdrawAmount forKey:kJavencommissionAccountModelWithdrawAmount];
}

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

        copy.balance = self.balance;
        copy.monthlyOrderCount = self.monthlyOrderCount;
        copy.monthlySales = self.monthlySales;
        copy.reseller = [self.reseller copyWithZone:zone];
        copy.uuid = [self.uuid copyWithZone:zone];
        copy.accumulatedIncome = self.accumulatedIncome;
        copy.monthlyIncome = self.monthlyIncome;
        copy.unconfirmedBalance = self.unconfirmedBalance;
        copy.withdrawAmount = self.withdrawAmount;
    }
    
    return copy;
}


@end
