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

#import "JavenBillModel.h"



NSString *const kJavenBillModelUuid = @"uuid";
NSString *const kJavenBillModelState = @"state";
NSString *const kJavenBillModelConfirmDate = @"confirmDate";
NSString *const kJavenBillModelVersion = @"version";
NSString *const kJavenBillModelConsumer = @"consumer";
NSString *const kJavenBillModelDomain = @"domain";
NSString *const kJavenBillModelRefundFlowNum = @"refundFlowNum";
NSString *const kJavenBillModelLastModifyInfo = @"lastModifyInfo";
NSString *const kJavenBillModelAction = @"action";
NSString *const kJavenBillModelOrder = @"order";
NSString *const kJavenBillModelIncome = @"income";
NSString *const kJavenBillModelLowerReseller = @"lowerReseller";
NSString *const kJavenBillModelCreateInfo = @"createInfo";
NSString *const kJavenBillModelRemark = @"remark";


@interface JavenBillModel ()

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

@end

@implementation JavenBillModel

@synthesize uuid = _uuid;
@synthesize state = _state;
@synthesize confirmDate = _confirmDate;
@synthesize version = _version;
@synthesize consumer = _consumer;
@synthesize domain = _domain;
@synthesize refundFlowNum = _refundFlowNum;
@synthesize lastModifyInfo = _lastModifyInfo;
@synthesize action = _action;
@synthesize order = _order;
@synthesize income = _income;
@synthesize lowerReseller = _lowerReseller;
@synthesize createInfo = _createInfo;
@synthesize remark = _remark;


+ (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.uuid = [self objectOrNilForKey:kJavenBillModelUuid fromDictionary:dict];
            self.state = [self objectOrNilForKey:kJavenBillModelState fromDictionary:dict];
            self.confirmDate = [self objectOrNilForKey:kJavenBillModelConfirmDate fromDictionary:dict];
            self.version = [[self objectOrNilForKey:kJavenBillModelVersion fromDictionary:dict] doubleValue];
            self.consumer = [JavenConsumer modelObjectWithDictionary:[dict objectForKey:kJavenBillModelConsumer]];
            self.domain = [self objectOrNilForKey:kJavenBillModelDomain fromDictionary:dict];
            self.refundFlowNum = [self objectOrNilForKey:kJavenBillModelRefundFlowNum fromDictionary:dict];
            self.lastModifyInfo = [JavenLastModifyInfo modelObjectWithDictionary:[dict objectForKey:kJavenBillModelLastModifyInfo]];
            self.action = [self objectOrNilForKey:kJavenBillModelAction fromDictionary:dict];
            self.order = [JavenOrder modelObjectWithDictionary:[dict objectForKey:kJavenBillModelOrder]];
            self.income = [JavenIncome modelObjectWithDictionary:[dict objectForKey:kJavenBillModelIncome]];
            self.lowerReseller = [self objectOrNilForKey:kJavenBillModelLowerReseller fromDictionary:dict];
            self.createInfo = [JavenCreateInfo modelObjectWithDictionary:[dict objectForKey:kJavenBillModelCreateInfo]];
            self.remark = [self objectOrNilForKey:kJavenBillModelRemark fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.uuid forKey:kJavenBillModelUuid];
    [mutableDict setValue:self.state forKey:kJavenBillModelState];
    [mutableDict setValue:self.confirmDate forKey:kJavenBillModelConfirmDate];
    [mutableDict setValue:[NSNumber numberWithDouble:self.version] forKey:kJavenBillModelVersion];
    [mutableDict setValue:[self.consumer dictionaryRepresentation] forKey:kJavenBillModelConsumer];
    [mutableDict setValue:self.domain forKey:kJavenBillModelDomain];
    [mutableDict setValue:self.refundFlowNum forKey:kJavenBillModelRefundFlowNum];
    [mutableDict setValue:[self.lastModifyInfo dictionaryRepresentation] forKey:kJavenBillModelLastModifyInfo];
    [mutableDict setValue:self.action forKey:kJavenBillModelAction];
    [mutableDict setValue:[self.order dictionaryRepresentation] forKey:kJavenBillModelOrder];
    [mutableDict setValue:[self.income dictionaryRepresentation] forKey:kJavenBillModelIncome];
    [mutableDict setValue:self.lowerReseller forKey:kJavenBillModelLowerReseller];
    [mutableDict setValue:[self.createInfo dictionaryRepresentation] forKey:kJavenBillModelCreateInfo];
    [mutableDict setValue:self.remark forKey:kJavenBillModelRemark];

    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.uuid = [aDecoder decodeObjectForKey:kJavenBillModelUuid];
    self.state = [aDecoder decodeObjectForKey:kJavenBillModelState];
    self.confirmDate = [aDecoder decodeObjectForKey:kJavenBillModelConfirmDate];
    self.version = [aDecoder decodeDoubleForKey:kJavenBillModelVersion];
    self.consumer = [aDecoder decodeObjectForKey:kJavenBillModelConsumer];
    self.domain = [aDecoder decodeObjectForKey:kJavenBillModelDomain];
    self.refundFlowNum = [aDecoder decodeObjectForKey:kJavenBillModelRefundFlowNum];
    self.lastModifyInfo = [aDecoder decodeObjectForKey:kJavenBillModelLastModifyInfo];
    self.action = [aDecoder decodeObjectForKey:kJavenBillModelAction];
    self.order = [aDecoder decodeObjectForKey:kJavenBillModelOrder];
    self.income = [aDecoder decodeObjectForKey:kJavenBillModelIncome];
    self.lowerReseller = [aDecoder decodeObjectForKey:kJavenBillModelLowerReseller];
    self.createInfo = [aDecoder decodeObjectForKey:kJavenBillModelCreateInfo];
    self.remark = [aDecoder decodeObjectForKey:kJavenBillModelRemark];
    return self;
}

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

    [aCoder encodeObject:_uuid forKey:kJavenBillModelUuid];
    [aCoder encodeObject:_state forKey:kJavenBillModelState];
    [aCoder encodeObject:_confirmDate forKey:kJavenBillModelConfirmDate];
    [aCoder encodeDouble:_version forKey:kJavenBillModelVersion];
    [aCoder encodeObject:_consumer forKey:kJavenBillModelConsumer];
    [aCoder encodeObject:_domain forKey:kJavenBillModelDomain];
    [aCoder encodeObject:_refundFlowNum forKey:kJavenBillModelRefundFlowNum];
    [aCoder encodeObject:_lastModifyInfo forKey:kJavenBillModelLastModifyInfo];
    [aCoder encodeObject:_action forKey:kJavenBillModelAction];
    [aCoder encodeObject:_order forKey:kJavenBillModelOrder];
    [aCoder encodeObject:_income forKey:kJavenBillModelIncome];
    [aCoder encodeObject:_lowerReseller forKey:kJavenBillModelLowerReseller];
    [aCoder encodeObject:_createInfo forKey:kJavenBillModelCreateInfo];
    [aCoder encodeObject:_remark forKey:kJavenBillModelRemark];
}

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

        copy.uuid = [self.uuid copyWithZone:zone];
        copy.state = [self.state copyWithZone:zone];
        copy.confirmDate = [self.confirmDate copyWithZone:zone];
        copy.version = self.version;
        copy.consumer = [self.consumer copyWithZone:zone];
        copy.domain = [self.domain copyWithZone:zone];
        copy.refundFlowNum = [self.refundFlowNum copyWithZone:zone];
        copy.lastModifyInfo = [self.lastModifyInfo copyWithZone:zone];
        copy.action = [self.action copyWithZone:zone];
        copy.order = [self.order copyWithZone:zone];
        copy.income = [self.income copyWithZone:zone];
        copy.lowerReseller = [self.lowerReseller copyWithZone:zone];
        copy.createInfo = [self.createInfo copyWithZone:zone];
        copy.remark = [self.remark copyWithZone:zone];
    }
    
    return copy;
}


@end
