//
//  CommotityListModel.m
//
//  Created by Z  on 16/4/27
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "CommotityListModel.h"
#import "CommotityListCategory.h"
#import "CommotityListBrand.h"


NSString *const kCommotityListModelUuid = @"uuid";
NSString *const kCommotityListModelCategory = @"category";
NSString *const kCommotityListModelSalesVolume = @"salesVolume";
NSString *const kCommotityListModelVisited = @"visited";
NSString *const kCommotityListModelBrand = @"brand";
NSString *const kCommotityListModelEngName = @"engName";
NSString *const kCommotityListModelPictures = @"pictures";
NSString *const kCommotityListModelPrice = @"price";
NSString *const kCommotityListModelOriginalPrice = @"originalPrice";
NSString *const kCommotityListModelSpecName = @"specName";
NSString *const kCommotityListModelFavorited = @"favorited";
NSString *const kCommotityListModelCover = @"cover";
NSString *const kCommotityListModelCommission = @"commission";
NSString *const kCommotityListModelTranferType = @"tranferType";
NSString *const kCommotityListModelInv = @"inv";
NSString *const kCommotityListModelName = @"name";
NSString *const kCommotityListModelScore = @"score";


@interface CommotityListModel ()

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

@end

@implementation CommotityListModel

@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:kCommotityListModelUuid fromDictionary:dict];
            self.category = [CommotityListCategory modelObjectWithDictionary:[dict objectForKey:kCommotityListModelCategory]];
            self.salesVolume = [[self objectOrNilForKey:kCommotityListModelSalesVolume fromDictionary:dict] doubleValue];
            self.visited = [[self objectOrNilForKey:kCommotityListModelVisited fromDictionary:dict] doubleValue];
            self.brand = [CommotityListBrand modelObjectWithDictionary:[dict objectForKey:kCommotityListModelBrand]];
            self.engName = [self objectOrNilForKey:kCommotityListModelEngName fromDictionary:dict];
            self.pictures = [self objectOrNilForKey:kCommotityListModelPictures fromDictionary:dict];
            self.price = [[self objectOrNilForKey:kCommotityListModelPrice fromDictionary:dict] doubleValue];
            self.originalPrice = [[self objectOrNilForKey:kCommotityListModelOriginalPrice fromDictionary:dict] doubleValue];
            self.specName = [self objectOrNilForKey:kCommotityListModelSpecName fromDictionary:dict];
            self.favorited = [[self objectOrNilForKey:kCommotityListModelFavorited fromDictionary:dict] doubleValue];
            self.cover = [self objectOrNilForKey:kCommotityListModelCover fromDictionary:dict];
            self.commission = [[self objectOrNilForKey:kCommotityListModelCommission fromDictionary:dict] doubleValue];
            self.tranferType = [self objectOrNilForKey:kCommotityListModelTranferType fromDictionary:dict];
            self.inv = [[self objectOrNilForKey:kCommotityListModelInv fromDictionary:dict] doubleValue];
            self.name = [self objectOrNilForKey:kCommotityListModelName fromDictionary:dict];
            self.score = [[self objectOrNilForKey:kCommotityListModelScore fromDictionary:dict] doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.uuid forKey:kCommotityListModelUuid];
    [mutableDict setValue:[self.category dictionaryRepresentation] forKey:kCommotityListModelCategory];
    [mutableDict setValue:[NSNumber numberWithDouble:self.salesVolume] forKey:kCommotityListModelSalesVolume];
    [mutableDict setValue:[NSNumber numberWithDouble:self.visited] forKey:kCommotityListModelVisited];
    [mutableDict setValue:[self.brand dictionaryRepresentation] forKey:kCommotityListModelBrand];
    [mutableDict setValue:self.engName forKey:kCommotityListModelEngName];
    [mutableDict setValue:self.pictures forKey:kCommotityListModelPictures];
    [mutableDict setValue:[NSNumber numberWithDouble:self.price] forKey:kCommotityListModelPrice];
    [mutableDict setValue:[NSNumber numberWithDouble:self.originalPrice] forKey:kCommotityListModelOriginalPrice];
    [mutableDict setValue:self.specName forKey:kCommotityListModelSpecName];
    [mutableDict setValue:[NSNumber numberWithDouble:self.favorited] forKey:kCommotityListModelFavorited];
    [mutableDict setValue:self.cover forKey:kCommotityListModelCover];
    [mutableDict setValue:[NSNumber numberWithDouble:self.commission] forKey:kCommotityListModelCommission];
    [mutableDict setValue:self.tranferType forKey:kCommotityListModelTranferType];
    [mutableDict setValue:[NSNumber numberWithDouble:self.inv] forKey:kCommotityListModelInv];
    [mutableDict setValue:self.name forKey:kCommotityListModelName];
    [mutableDict setValue:[NSNumber numberWithDouble:self.score] forKey:kCommotityListModelScore];

    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:kCommotityListModelUuid];
    self.category = [aDecoder decodeObjectForKey:kCommotityListModelCategory];
    self.salesVolume = [aDecoder decodeDoubleForKey:kCommotityListModelSalesVolume];
    self.visited = [aDecoder decodeDoubleForKey:kCommotityListModelVisited];
    self.brand = [aDecoder decodeObjectForKey:kCommotityListModelBrand];
    self.engName = [aDecoder decodeObjectForKey:kCommotityListModelEngName];
    self.pictures = [aDecoder decodeObjectForKey:kCommotityListModelPictures];
    self.price = [aDecoder decodeDoubleForKey:kCommotityListModelPrice];
    self.originalPrice = [aDecoder decodeDoubleForKey:kCommotityListModelOriginalPrice];
    self.specName = [aDecoder decodeObjectForKey:kCommotityListModelSpecName];
    self.favorited = [aDecoder decodeDoubleForKey:kCommotityListModelFavorited];
    self.cover = [aDecoder decodeObjectForKey:kCommotityListModelCover];
    self.commission = [aDecoder decodeDoubleForKey:kCommotityListModelCommission];
    self.tranferType = [aDecoder decodeObjectForKey:kCommotityListModelTranferType];
    self.inv = [aDecoder decodeDoubleForKey:kCommotityListModelInv];
    self.name = [aDecoder decodeObjectForKey:kCommotityListModelName];
    self.score = [aDecoder decodeDoubleForKey:kCommotityListModelScore];
    return self;
}

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

    [aCoder encodeObject:_uuid forKey:kCommotityListModelUuid];
    [aCoder encodeObject:_category forKey:kCommotityListModelCategory];
    [aCoder encodeDouble:_salesVolume forKey:kCommotityListModelSalesVolume];
    [aCoder encodeDouble:_visited forKey:kCommotityListModelVisited];
    [aCoder encodeObject:_brand forKey:kCommotityListModelBrand];
    [aCoder encodeObject:_engName forKey:kCommotityListModelEngName];
    [aCoder encodeObject:_pictures forKey:kCommotityListModelPictures];
    [aCoder encodeDouble:_price forKey:kCommotityListModelPrice];
    [aCoder encodeDouble:_originalPrice forKey:kCommotityListModelOriginalPrice];
    [aCoder encodeObject:_specName forKey:kCommotityListModelSpecName];
    [aCoder encodeDouble:_favorited forKey:kCommotityListModelFavorited];
    [aCoder encodeObject:_cover forKey:kCommotityListModelCover];
    [aCoder encodeDouble:_commission forKey:kCommotityListModelCommission];
    [aCoder encodeObject:_tranferType forKey:kCommotityListModelTranferType];
    [aCoder encodeDouble:_inv forKey:kCommotityListModelInv];
    [aCoder encodeObject:_name forKey:kCommotityListModelName];
    [aCoder encodeDouble:_score forKey:kCommotityListModelScore];
}

- (id)copyWithZone:(NSZone *)zone
{
    CommotityListModel *copy = [[CommotityListModel 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
