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

#import "JavenGoods.h"
#import "JavenCategory.h"
#import "JavenBrand.h"


NSString *const kJavenGoodsUuid = @"uuid";
NSString *const kJavenGoodsCategory = @"category";
NSString *const kJavenGoodsSalesVolume = @"salesVolume";
NSString *const kJavenGoodsVisited = @"visited";
NSString *const kJavenGoodsBrand = @"brand";
NSString *const kJavenGoodsEngName = @"engName";
NSString *const kJavenGoodsPictures = @"pictures";
NSString *const kJavenGoodsPrice = @"price";
NSString *const kJavenGoodsOriginalPrice = @"originalPrice";
NSString *const kJavenGoodsSpecName = @"specName";
NSString *const kJavenGoodsFavorited = @"favorited";
NSString *const kJavenGoodsCover = @"cover";
NSString *const kJavenGoodsCommission = @"commission";
NSString *const kJavenGoodsTranferType = @"tranferType";
NSString *const kJavenGoodsInv = @"inv";
NSString *const kJavenGoodsName = @"name";
NSString *const kJavenGoodsScore = @"score";


@interface JavenGoods ()

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

@end

@implementation JavenGoods

@synthesize uuid = _uuid;
@synthesize category = _category;
@synthesize salesVolume = _salesVolume;
@synthesize visited = _visited;
@synthesize brand = _brand;
@synthesize engName = _engName;
@synthesize pictures = _pictures;
@synthesize price = _price;
@synthesize originalPrice = _originalPrice;
@synthesize specName = _specName;
@synthesize favorited = _favorited;
@synthesize cover = _cover;
@synthesize commission = _commission;
@synthesize tranferType = _tranferType;
@synthesize inv = _inv;
@synthesize name = _name;
@synthesize score = _score;


+ (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:kJavenGoodsUuid fromDictionary:dict];
            self.category = [JavenCategory modelObjectWithDictionary:[dict objectForKey:kJavenGoodsCategory]];
            self.salesVolume = [[self objectOrNilForKey:kJavenGoodsSalesVolume fromDictionary:dict] doubleValue];
            self.visited = [[self objectOrNilForKey:kJavenGoodsVisited fromDictionary:dict] doubleValue];
            self.brand = [JavenBrand modelObjectWithDictionary:[dict objectForKey:kJavenGoodsBrand]];
            self.engName = [self objectOrNilForKey:kJavenGoodsEngName fromDictionary:dict];
            self.pictures = [self objectOrNilForKey:kJavenGoodsPictures fromDictionary:dict];
            self.price = [[self objectOrNilForKey:kJavenGoodsPrice fromDictionary:dict] doubleValue];
            self.originalPrice = [[self objectOrNilForKey:kJavenGoodsOriginalPrice fromDictionary:dict] doubleValue];
            self.specName = [self objectOrNilForKey:kJavenGoodsSpecName fromDictionary:dict];
            self.favorited = [[self objectOrNilForKey:kJavenGoodsFavorited fromDictionary:dict] doubleValue];
            self.cover = [self objectOrNilForKey:kJavenGoodsCover fromDictionary:dict];
            self.commission = [[self objectOrNilForKey:kJavenGoodsCommission fromDictionary:dict] doubleValue];
            self.tranferType = [self objectOrNilForKey:kJavenGoodsTranferType fromDictionary:dict];
            self.inv = [[self objectOrNilForKey:kJavenGoodsInv fromDictionary:dict] doubleValue];
            self.name = [self objectOrNilForKey:kJavenGoodsName fromDictionary:dict];
            self.score = [[self objectOrNilForKey:kJavenGoodsScore fromDictionary:dict] doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.uuid forKey:kJavenGoodsUuid];
    [mutableDict setValue:[self.category dictionaryRepresentation] forKey:kJavenGoodsCategory];
    [mutableDict setValue:[NSNumber numberWithDouble:self.salesVolume] forKey:kJavenGoodsSalesVolume];
    [mutableDict setValue:[NSNumber numberWithDouble:self.visited] forKey:kJavenGoodsVisited];
    [mutableDict setValue:[self.brand dictionaryRepresentation] forKey:kJavenGoodsBrand];
    [mutableDict setValue:self.engName forKey:kJavenGoodsEngName];
    [mutableDict setValue:self.pictures forKey:kJavenGoodsPictures];
    [mutableDict setValue:[NSNumber numberWithDouble:self.price] forKey:kJavenGoodsPrice];
    [mutableDict setValue:[NSNumber numberWithDouble:self.originalPrice] forKey:kJavenGoodsOriginalPrice];
    [mutableDict setValue:self.specName forKey:kJavenGoodsSpecName];
    [mutableDict setValue:[NSNumber numberWithDouble:self.favorited] forKey:kJavenGoodsFavorited];
    [mutableDict setValue:self.cover forKey:kJavenGoodsCover];
    [mutableDict setValue:[NSNumber numberWithDouble:self.commission] forKey:kJavenGoodsCommission];
    [mutableDict setValue:self.tranferType forKey:kJavenGoodsTranferType];
    [mutableDict setValue:[NSNumber numberWithDouble:self.inv] forKey:kJavenGoodsInv];
    [mutableDict setValue:self.name forKey:kJavenGoodsName];
    [mutableDict setValue:[NSNumber numberWithDouble:self.score] forKey:kJavenGoodsScore];

    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:kJavenGoodsUuid];
    self.category = [aDecoder decodeObjectForKey:kJavenGoodsCategory];
    self.salesVolume = [aDecoder decodeDoubleForKey:kJavenGoodsSalesVolume];
    self.visited = [aDecoder decodeDoubleForKey:kJavenGoodsVisited];
    self.brand = [aDecoder decodeObjectForKey:kJavenGoodsBrand];
    self.engName = [aDecoder decodeObjectForKey:kJavenGoodsEngName];
    self.pictures = [aDecoder decodeObjectForKey:kJavenGoodsPictures];
    self.price = [aDecoder decodeDoubleForKey:kJavenGoodsPrice];
    self.originalPrice = [aDecoder decodeDoubleForKey:kJavenGoodsOriginalPrice];
    self.specName = [aDecoder decodeObjectForKey:kJavenGoodsSpecName];
    self.favorited = [aDecoder decodeDoubleForKey:kJavenGoodsFavorited];
    self.cover = [aDecoder decodeObjectForKey:kJavenGoodsCover];
    self.commission = [aDecoder decodeDoubleForKey:kJavenGoodsCommission];
    self.tranferType = [aDecoder decodeObjectForKey:kJavenGoodsTranferType];
    self.inv = [aDecoder decodeDoubleForKey:kJavenGoodsInv];
    self.name = [aDecoder decodeObjectForKey:kJavenGoodsName];
    self.score = [aDecoder decodeDoubleForKey:kJavenGoodsScore];
    return self;
}

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

    [aCoder encodeObject:_uuid forKey:kJavenGoodsUuid];
    [aCoder encodeObject:_category forKey:kJavenGoodsCategory];
    [aCoder encodeDouble:_salesVolume forKey:kJavenGoodsSalesVolume];
    [aCoder encodeDouble:_visited forKey:kJavenGoodsVisited];
    [aCoder encodeObject:_brand forKey:kJavenGoodsBrand];
    [aCoder encodeObject:_engName forKey:kJavenGoodsEngName];
    [aCoder encodeObject:_pictures forKey:kJavenGoodsPictures];
    [aCoder encodeDouble:_price forKey:kJavenGoodsPrice];
    [aCoder encodeDouble:_originalPrice forKey:kJavenGoodsOriginalPrice];
    [aCoder encodeObject:_specName forKey:kJavenGoodsSpecName];
    [aCoder encodeDouble:_favorited forKey:kJavenGoodsFavorited];
    [aCoder encodeObject:_cover forKey:kJavenGoodsCover];
    [aCoder encodeDouble:_commission forKey:kJavenGoodsCommission];
    [aCoder encodeObject:_tranferType forKey:kJavenGoodsTranferType];
    [aCoder encodeDouble:_inv forKey:kJavenGoodsInv];
    [aCoder encodeObject:_name forKey:kJavenGoodsName];
    [aCoder encodeDouble:_score forKey:kJavenGoodsScore];
}

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

        copy.uuid = [self.uuid copyWithZone:zone];
        copy.category = [self.category copyWithZone:zone];
        copy.salesVolume = self.salesVolume;
        copy.visited = self.visited;
        copy.brand = [self.brand copyWithZone:zone];
        copy.engName = [self.engName copyWithZone:zone];
        copy.pictures = [self.pictures copyWithZone:zone];
        copy.price = self.price;
        copy.originalPrice = self.originalPrice;
        copy.specName = [self.specName copyWithZone:zone];
        copy.favorited = self.favorited;
        copy.cover = [self.cover copyWithZone:zone];
        copy.commission = self.commission;
        copy.tranferType = [self.tranferType copyWithZone:zone];
        copy.inv = self.inv;
        copy.name = [self.name copyWithZone:zone];
        copy.score = self.score;
    }
    
    return copy;
}


@end
