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

#import "JavenAdModel.h"


NSString *const kJavenAdModelPicture = @"picture";
NSString *const kJavenAdModelOrder = @"order";
NSString *const kJavenAdModelUrl = @"url";
NSString *const kJavenAdModelUuid = @"uuid";


@interface JavenAdModel ()

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

@end

@implementation JavenAdModel

@synthesize picture = _picture;
@synthesize order = _order;
@synthesize url = _url;
@synthesize uuid = _uuid;


+ (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.picture = [self objectOrNilForKey:kJavenAdModelPicture fromDictionary:dict];
            self.order = [[self objectOrNilForKey:kJavenAdModelOrder fromDictionary:dict] doubleValue];
            self.url = [self objectOrNilForKey:kJavenAdModelUrl fromDictionary:dict];
            self.uuid = [self objectOrNilForKey:kJavenAdModelUuid fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.picture forKey:kJavenAdModelPicture];
    [mutableDict setValue:[NSNumber numberWithDouble:self.order] forKey:kJavenAdModelOrder];
    [mutableDict setValue:self.url forKey:kJavenAdModelUrl];
    [mutableDict setValue:self.uuid forKey:kJavenAdModelUuid];

    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.picture = [aDecoder decodeObjectForKey:kJavenAdModelPicture];
    self.order = [aDecoder decodeDoubleForKey:kJavenAdModelOrder];
    self.url = [aDecoder decodeObjectForKey:kJavenAdModelUrl];
    self.uuid = [aDecoder decodeObjectForKey:kJavenAdModelUuid];
    return self;
}

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

    [aCoder encodeObject:_picture forKey:kJavenAdModelPicture];
    [aCoder encodeDouble:_order forKey:kJavenAdModelOrder];
    [aCoder encodeObject:_url forKey:kJavenAdModelUrl];
    [aCoder encodeObject:_uuid forKey:kJavenAdModelUuid];
}

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

        copy.picture = [self.picture copyWithZone:zone];
        copy.order = self.order;
        copy.url = [self.url copyWithZone:zone];
        copy.uuid = [self.uuid copyWithZone:zone];
    }
    
    return copy;
}


@end
