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

#import "JavenOrderModel.h"



NSString *const kJavenOrderModelGoodsDetails = @"goodsDetails";
NSString *const kJavenOrderModelShipTrackingNumber = @"shipTrackingNumber";
NSString *const kJavenOrderModelTotal = @"total";
NSString *const kJavenOrderModelTax = @"tax";
NSString *const kJavenOrderModelCommissions = @"commissions";
NSString *const kJavenOrderModelReseller = @"reseller";
NSString *const kJavenOrderModelShipType = @"shipType";
NSString *const kJavenOrderModelBillNumber = @"billNumber";
NSString *const kJavenOrderModelBillNumberEx = @"billNumberEx";
NSString *const kJavenOrderModelGoodsCount = @"goodsCount";
NSString *const kJavenOrderModelDiscount = @"discount";
NSString *const kJavenOrderModelUuid = @"uuid";
NSString *const kJavenOrderModelPayMethod = @"payMethod";
NSString *const kJavenOrderModelLastModifyInfo = @"lastModifyInfo";
NSString *const kJavenOrderModelTicket = @"ticket";
NSString *const kJavenOrderModelVersion = @"version";
NSString *const kJavenOrderModelState = @"state";
NSString *const kJavenOrderModelDomain = @"domain";
NSString *const kJavenOrderModelOwnerBillNumber = @"ownerBillNumber";
NSString *const kJavenOrderModelFreight = @"freight";
NSString *const kJavenOrderModelShop = @"shop";
NSString *const kJavenOrderModelCreateInfo = @"createInfo";
NSString *const kJavenOrderModelConsumer = @"consumer";
NSString *const kJavenOrderModelGoodsAmount = @"goodsAmount";
NSString *const kJavenOrderModelRemark = @"remark";
NSString *const kJavenOrderModelAddress = @"address";
NSString *const kJavenOrderModelShkjtNo = @"shkjtNo";


@interface JavenOrderModel ()

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

@end

@implementation JavenOrderModel

@synthesize goodsDetails = _goodsDetails;
@synthesize shipTrackingNumber = _shipTrackingNumber;
@synthesize total = _total;
@synthesize tax = _tax;
@synthesize commissions = _commissions;
@synthesize reseller = _reseller;
@synthesize shipType = _shipType;
@synthesize billNumber = _billNumber;
@synthesize billNumberEx = _billNumberEx;
@synthesize goodsCount = _goodsCount;
@synthesize discount = _discount;
@synthesize uuid = _uuid;
@synthesize payMethod = _payMethod;
@synthesize lastModifyInfo = _lastModifyInfo;
@synthesize ticket = _ticket;
@synthesize version = _version;
@synthesize state = _state;
@synthesize domain = _domain;
@synthesize ownerBillNumber = _ownerBillNumber;
@synthesize freight = _freight;
@synthesize shop = _shop;
@synthesize createInfo = _createInfo;
@synthesize consumer = _consumer;
@synthesize goodsAmount = _goodsAmount;
@synthesize remark = _remark;
@synthesize address = _address;
@synthesize shkjtNo = _shkjtNo;


+ (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]]) {
    NSObject *receivedJavenGoodsDetails = [dict objectForKey:kJavenOrderModelGoodsDetails];
    NSMutableArray *parsedJavenGoodsDetails = [NSMutableArray array];
    if ([receivedJavenGoodsDetails isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedJavenGoodsDetails) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedJavenGoodsDetails addObject:[JavenGoodsDetails modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedJavenGoodsDetails isKindOfClass:[NSDictionary class]]) {
       [parsedJavenGoodsDetails addObject:[JavenGoodsDetails modelObjectWithDictionary:(NSDictionary *)receivedJavenGoodsDetails]];
    }

    self.goodsDetails = [NSArray arrayWithArray:parsedJavenGoodsDetails];
            self.shipTrackingNumber = [self objectOrNilForKey:kJavenOrderModelShipTrackingNumber fromDictionary:dict];
            self.total = [[self objectOrNilForKey:kJavenOrderModelTotal fromDictionary:dict] doubleValue];
            self.tax = [[self objectOrNilForKey:kJavenOrderModelTax fromDictionary:dict] doubleValue];
            self.commissions = [[self objectOrNilForKey:kJavenOrderModelCommissions fromDictionary:dict] doubleValue];
            self.reseller = [JavenReseller modelObjectWithDictionary:[dict objectForKey:kJavenOrderModelReseller]];
            self.shipType = [self objectOrNilForKey:kJavenOrderModelShipType fromDictionary:dict];
            self.billNumber = [self objectOrNilForKey:kJavenOrderModelBillNumber fromDictionary:dict];
            self.billNumberEx = [self objectOrNilForKey:kJavenOrderModelBillNumberEx fromDictionary:dict];
            self.goodsCount = [[self objectOrNilForKey:kJavenOrderModelGoodsCount fromDictionary:dict] doubleValue];
            self.discount = [[self objectOrNilForKey:kJavenOrderModelDiscount fromDictionary:dict] doubleValue];
            self.uuid = [self objectOrNilForKey:kJavenOrderModelUuid fromDictionary:dict];
            self.payMethod = [self objectOrNilForKey:kJavenOrderModelPayMethod fromDictionary:dict];
            self.lastModifyInfo = [JavenLastModifyInfo modelObjectWithDictionary:[dict objectForKey:kJavenOrderModelLastModifyInfo]];
            self.ticket = [JavenTicket modelObjectWithDictionary:[dict objectForKey:kJavenOrderModelTicket]];
            self.version = [[self objectOrNilForKey:kJavenOrderModelVersion fromDictionary:dict] doubleValue];
            self.state = [self objectOrNilForKey:kJavenOrderModelState fromDictionary:dict];
            self.domain = [self objectOrNilForKey:kJavenOrderModelDomain fromDictionary:dict];
            self.ownerBillNumber = [self objectOrNilForKey:kJavenOrderModelOwnerBillNumber fromDictionary:dict];
            self.freight = [[self objectOrNilForKey:kJavenOrderModelFreight fromDictionary:dict] doubleValue];
            self.shop = [JavenShop modelObjectWithDictionary:[dict objectForKey:kJavenOrderModelShop]];
            self.createInfo = [JavenCreateInfo modelObjectWithDictionary:[dict objectForKey:kJavenOrderModelCreateInfo]];
            self.consumer = [JavenConsumer modelObjectWithDictionary:[dict objectForKey:kJavenOrderModelConsumer]];
            self.goodsAmount = [[self objectOrNilForKey:kJavenOrderModelGoodsAmount fromDictionary:dict] doubleValue];
            self.remark = [self objectOrNilForKey:kJavenOrderModelRemark fromDictionary:dict];
            self.address = [JavenAddress modelObjectWithDictionary:[dict objectForKey:kJavenOrderModelAddress]];
            self.shkjtNo = [self objectOrNilForKey:kJavenOrderModelShkjtNo fromDictionary:dict];

    }
    
    
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    NSMutableArray *tempArrayForGoodsDetails = [NSMutableArray array];
    for (NSObject *subArrayObject in self.goodsDetails) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForGoodsDetails addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForGoodsDetails addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForGoodsDetails] forKey:kJavenOrderModelGoodsDetails];
    [mutableDict setValue:self.shipTrackingNumber forKey:kJavenOrderModelShipTrackingNumber];
    [mutableDict setValue:[NSNumber numberWithDouble:self.total] forKey:kJavenOrderModelTotal];
    [mutableDict setValue:[NSNumber numberWithDouble:self.tax] forKey:kJavenOrderModelTax];
    [mutableDict setValue:[NSNumber numberWithDouble:self.commissions] forKey:kJavenOrderModelCommissions];
    [mutableDict setValue:[self.reseller dictionaryRepresentation] forKey:kJavenOrderModelReseller];
    [mutableDict setValue:self.shipType forKey:kJavenOrderModelShipType];
    [mutableDict setValue:self.billNumber forKey:kJavenOrderModelBillNumber];
    [mutableDict setValue:self.billNumberEx forKey:kJavenOrderModelBillNumberEx];
    [mutableDict setValue:[NSNumber numberWithDouble:self.goodsCount] forKey:kJavenOrderModelGoodsCount];
    [mutableDict setValue:[NSNumber numberWithDouble:self.discount] forKey:kJavenOrderModelDiscount];
    [mutableDict setValue:self.uuid forKey:kJavenOrderModelUuid];
    [mutableDict setValue:self.payMethod forKey:kJavenOrderModelPayMethod];
    [mutableDict setValue:[self.lastModifyInfo dictionaryRepresentation] forKey:kJavenOrderModelLastModifyInfo];
    [mutableDict setValue:[self.ticket dictionaryRepresentation] forKey:kJavenOrderModelTicket];
    [mutableDict setValue:[NSNumber numberWithDouble:self.version] forKey:kJavenOrderModelVersion];
    [mutableDict setValue:self.state forKey:kJavenOrderModelState];
    [mutableDict setValue:self.domain forKey:kJavenOrderModelDomain];
    [mutableDict setValue:self.ownerBillNumber forKey:kJavenOrderModelOwnerBillNumber];
    [mutableDict setValue:[NSNumber numberWithDouble:self.freight] forKey:kJavenOrderModelFreight];
    [mutableDict setValue:[self.shop dictionaryRepresentation] forKey:kJavenOrderModelShop];
    [mutableDict setValue:[self.createInfo dictionaryRepresentation] forKey:kJavenOrderModelCreateInfo];
    [mutableDict setValue:[self.consumer dictionaryRepresentation] forKey:kJavenOrderModelConsumer];
    [mutableDict setValue:[NSNumber numberWithDouble:self.goodsAmount] forKey:kJavenOrderModelGoodsAmount];
    [mutableDict setValue:self.remark forKey:kJavenOrderModelRemark];
    [mutableDict setValue:[self.address dictionaryRepresentation] forKey:kJavenOrderModelAddress];
    [mutableDict setValue:self.shkjtNo forKey:kJavenOrderModelShkjtNo];

    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.goodsDetails = [aDecoder decodeObjectForKey:kJavenOrderModelGoodsDetails];
    self.shipTrackingNumber = [aDecoder decodeObjectForKey:kJavenOrderModelShipTrackingNumber];
    self.total = [aDecoder decodeDoubleForKey:kJavenOrderModelTotal];
    self.tax = [aDecoder decodeDoubleForKey:kJavenOrderModelTax];
    self.commissions = [aDecoder decodeDoubleForKey:kJavenOrderModelCommissions];
    self.reseller = [aDecoder decodeObjectForKey:kJavenOrderModelReseller];
    self.shipType = [aDecoder decodeObjectForKey:kJavenOrderModelShipType];
    self.billNumber = [aDecoder decodeObjectForKey:kJavenOrderModelBillNumber];
    self.billNumberEx = [aDecoder decodeObjectForKey:kJavenOrderModelBillNumberEx];
    self.goodsCount = [aDecoder decodeDoubleForKey:kJavenOrderModelGoodsCount];
    self.discount = [aDecoder decodeDoubleForKey:kJavenOrderModelDiscount];
    self.uuid = [aDecoder decodeObjectForKey:kJavenOrderModelUuid];
    self.payMethod = [aDecoder decodeObjectForKey:kJavenOrderModelPayMethod];
    self.lastModifyInfo = [aDecoder decodeObjectForKey:kJavenOrderModelLastModifyInfo];
    self.ticket = [aDecoder decodeObjectForKey:kJavenOrderModelTicket];
    self.version = [aDecoder decodeDoubleForKey:kJavenOrderModelVersion];
    self.state = [aDecoder decodeObjectForKey:kJavenOrderModelState];
    self.domain = [aDecoder decodeObjectForKey:kJavenOrderModelDomain];
    self.ownerBillNumber = [aDecoder decodeObjectForKey:kJavenOrderModelOwnerBillNumber];
    self.freight = [aDecoder decodeDoubleForKey:kJavenOrderModelFreight];
    self.shop = [aDecoder decodeObjectForKey:kJavenOrderModelShop];
    self.createInfo = [aDecoder decodeObjectForKey:kJavenOrderModelCreateInfo];
    self.consumer = [aDecoder decodeObjectForKey:kJavenOrderModelConsumer];
    self.goodsAmount = [aDecoder decodeDoubleForKey:kJavenOrderModelGoodsAmount];
    self.remark = [aDecoder decodeObjectForKey:kJavenOrderModelRemark];
    self.address = [aDecoder decodeObjectForKey:kJavenOrderModelAddress];
    self.shkjtNo = [aDecoder decodeObjectForKey:kJavenOrderModelShkjtNo];
    return self;
}

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

    [aCoder encodeObject:_goodsDetails forKey:kJavenOrderModelGoodsDetails];
    [aCoder encodeObject:_shipTrackingNumber forKey:kJavenOrderModelShipTrackingNumber];
    [aCoder encodeDouble:_total forKey:kJavenOrderModelTotal];
    [aCoder encodeDouble:_tax forKey:kJavenOrderModelTax];
    [aCoder encodeDouble:_commissions forKey:kJavenOrderModelCommissions];
    [aCoder encodeObject:_reseller forKey:kJavenOrderModelReseller];
    [aCoder encodeObject:_shipType forKey:kJavenOrderModelShipType];
    [aCoder encodeObject:_billNumber forKey:kJavenOrderModelBillNumber];
    [aCoder encodeObject:_billNumberEx forKey:kJavenOrderModelBillNumberEx];
    [aCoder encodeDouble:_goodsCount forKey:kJavenOrderModelGoodsCount];
    [aCoder encodeDouble:_discount forKey:kJavenOrderModelDiscount];
    [aCoder encodeObject:_uuid forKey:kJavenOrderModelUuid];
    [aCoder encodeObject:_payMethod forKey:kJavenOrderModelPayMethod];
    [aCoder encodeObject:_lastModifyInfo forKey:kJavenOrderModelLastModifyInfo];
    [aCoder encodeObject:_ticket forKey:kJavenOrderModelTicket];
    [aCoder encodeDouble:_version forKey:kJavenOrderModelVersion];
    [aCoder encodeObject:_state forKey:kJavenOrderModelState];
    [aCoder encodeObject:_domain forKey:kJavenOrderModelDomain];
    [aCoder encodeObject:_ownerBillNumber forKey:kJavenOrderModelOwnerBillNumber];
    [aCoder encodeDouble:_freight forKey:kJavenOrderModelFreight];
    [aCoder encodeObject:_shop forKey:kJavenOrderModelShop];
    [aCoder encodeObject:_createInfo forKey:kJavenOrderModelCreateInfo];
    [aCoder encodeObject:_consumer forKey:kJavenOrderModelConsumer];
    [aCoder encodeDouble:_goodsAmount forKey:kJavenOrderModelGoodsAmount];
    [aCoder encodeObject:_remark forKey:kJavenOrderModelRemark];
    [aCoder encodeObject:_address forKey:kJavenOrderModelAddress];
    [aCoder encodeObject:_shkjtNo forKey:kJavenOrderModelShkjtNo];
}

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

        copy.goodsDetails = [self.goodsDetails copyWithZone:zone];
        copy.shipTrackingNumber = [self.shipTrackingNumber copyWithZone:zone];
        copy.total = self.total;
        copy.tax = self.tax;
        copy.commissions = self.commissions;
        copy.reseller = [self.reseller copyWithZone:zone];
        copy.shipType = [self.shipType copyWithZone:zone];
        copy.billNumber = [self.billNumber copyWithZone:zone];
        copy.billNumberEx = [self.billNumberEx copyWithZone:zone];
        copy.goodsCount = self.goodsCount;
        copy.discount = self.discount;
        copy.uuid = [self.uuid copyWithZone:zone];
        copy.payMethod = [self.payMethod copyWithZone:zone];
        copy.lastModifyInfo = [self.lastModifyInfo copyWithZone:zone];
        copy.ticket = [self.ticket copyWithZone:zone];
        copy.version = self.version;
        copy.state = [self.state copyWithZone:zone];
        copy.domain = [self.domain copyWithZone:zone];
        copy.ownerBillNumber = [self.ownerBillNumber copyWithZone:zone];
        copy.freight = self.freight;
        copy.shop = [self.shop copyWithZone:zone];
        copy.createInfo = [self.createInfo copyWithZone:zone];
        copy.consumer = [self.consumer copyWithZone:zone];
        copy.goodsAmount = self.goodsAmount;
        copy.remark = [self.remark copyWithZone:zone];
        copy.address = [self.address copyWithZone:zone];
        copy.shkjtNo = [self.shkjtNo copyWithZone:zone];
    }
    
    return copy;
}

- (NSString *)getState
{
    NSArray *arrStateCode = @[@"toBePaid",@"toBeShipped",@"shipped",@"paid",@"cancelled"];
    NSArray *arrTitle = @[@"待支付",@"待发货",@"已发货",@"已完成",@"已取消"];
    
    NSString *state;
    for (int i = 0; i < arrStateCode.count; i++) {
        if ([arrStateCode[i] isEqualToString:self.state]) {
            state = arrTitle[i];
            break;
        }
    }    return state;
}


@end
