//
//  Data.m
//
//  Created by  黄 on 2017/1/23
//  Copyright (c) 2017 __MyCompanyName__. All rights reserved.
//

#import "Data.h"


NSString *const kDataCategory = @"category";
NSString *const kDataThumbnailPicS = @"thumbnail_pic_s";
NSString *const kDataThumbnailPicS03 = @"thumbnail_pic_s03";
NSString *const kDataTitle = @"title";
NSString *const kDataUniquekey = @"uniquekey";
NSString *const kDataAuthorName = @"author_name";
NSString *const kDataDate = @"date";
NSString *const kDataThumbnailPicS02 = @"thumbnail_pic_s02";
NSString *const kDataUrl = @"url";


@interface Data ()

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

@end

@implementation Data

@synthesize category = _category;
@synthesize thumbnailPicS = _thumbnailPicS;
@synthesize thumbnailPicS03 = _thumbnailPicS03;
@synthesize title = _title;
@synthesize uniquekey = _uniquekey;
@synthesize authorName = _authorName;
@synthesize date = _date;
@synthesize thumbnailPicS02 = _thumbnailPicS02;
@synthesize url = _url;


+ (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.category = [self objectOrNilForKey:kDataCategory fromDictionary:dict];
            self.thumbnailPicS = [self objectOrNilForKey:kDataThumbnailPicS fromDictionary:dict];
            self.thumbnailPicS03 = [self objectOrNilForKey:kDataThumbnailPicS03 fromDictionary:dict];
            self.title = [self objectOrNilForKey:kDataTitle fromDictionary:dict];
            self.uniquekey = [self objectOrNilForKey:kDataUniquekey fromDictionary:dict];
            self.authorName = [self objectOrNilForKey:kDataAuthorName fromDictionary:dict];
            self.date = [self objectOrNilForKey:kDataDate fromDictionary:dict];
            self.thumbnailPicS02 = [self objectOrNilForKey:kDataThumbnailPicS02 fromDictionary:dict];
            self.url = [self objectOrNilForKey:kDataUrl fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.category forKey:kDataCategory];
    [mutableDict setValue:self.thumbnailPicS forKey:kDataThumbnailPicS];
    [mutableDict setValue:self.thumbnailPicS03 forKey:kDataThumbnailPicS03];
    [mutableDict setValue:self.title forKey:kDataTitle];
    [mutableDict setValue:self.uniquekey forKey:kDataUniquekey];
    [mutableDict setValue:self.authorName forKey:kDataAuthorName];
    [mutableDict setValue:self.date forKey:kDataDate];
    [mutableDict setValue:self.thumbnailPicS02 forKey:kDataThumbnailPicS02];
    [mutableDict setValue:self.url forKey:kDataUrl];

    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.category = [aDecoder decodeObjectForKey:kDataCategory];
    self.thumbnailPicS = [aDecoder decodeObjectForKey:kDataThumbnailPicS];
    self.thumbnailPicS03 = [aDecoder decodeObjectForKey:kDataThumbnailPicS03];
    self.title = [aDecoder decodeObjectForKey:kDataTitle];
    self.uniquekey = [aDecoder decodeObjectForKey:kDataUniquekey];
    self.authorName = [aDecoder decodeObjectForKey:kDataAuthorName];
    self.date = [aDecoder decodeObjectForKey:kDataDate];
    self.thumbnailPicS02 = [aDecoder decodeObjectForKey:kDataThumbnailPicS02];
    self.url = [aDecoder decodeObjectForKey:kDataUrl];
    return self;
}

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

    [aCoder encodeObject:_category forKey:kDataCategory];
    [aCoder encodeObject:_thumbnailPicS forKey:kDataThumbnailPicS];
    [aCoder encodeObject:_thumbnailPicS03 forKey:kDataThumbnailPicS03];
    [aCoder encodeObject:_title forKey:kDataTitle];
    [aCoder encodeObject:_uniquekey forKey:kDataUniquekey];
    [aCoder encodeObject:_authorName forKey:kDataAuthorName];
    [aCoder encodeObject:_date forKey:kDataDate];
    [aCoder encodeObject:_thumbnailPicS02 forKey:kDataThumbnailPicS02];
    [aCoder encodeObject:_url forKey:kDataUrl];
}

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

        copy.category = [self.category copyWithZone:zone];
        copy.thumbnailPicS = [self.thumbnailPicS copyWithZone:zone];
        copy.thumbnailPicS03 = [self.thumbnailPicS03 copyWithZone:zone];
        copy.title = [self.title copyWithZone:zone];
        copy.uniquekey = [self.uniquekey copyWithZone:zone];
        copy.authorName = [self.authorName copyWithZone:zone];
        copy.date = [self.date copyWithZone:zone];
        copy.thumbnailPicS02 = [self.thumbnailPicS02 copyWithZone:zone];
        copy.url = [self.url copyWithZone:zone];
    }
    
    return copy;
}


@end
