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

#import "JavenTicket.h"


NSString *const kJavenTicketType = @"type";
NSString *const kJavenTicketContent = @"content";
NSString *const kJavenTicketTitle = @"title";


@interface JavenTicket ()

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

@end

@implementation JavenTicket

@synthesize type = _type;
@synthesize content = _content;
@synthesize title = _title;


+ (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.type = [self objectOrNilForKey:kJavenTicketType fromDictionary:dict];
            self.content = [self objectOrNilForKey:kJavenTicketContent fromDictionary:dict];
            self.title = [self objectOrNilForKey:kJavenTicketTitle fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.type forKey:kJavenTicketType];
    [mutableDict setValue:self.content forKey:kJavenTicketContent];
    [mutableDict setValue:self.title forKey:kJavenTicketTitle];

    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.type = [aDecoder decodeObjectForKey:kJavenTicketType];
    self.content = [aDecoder decodeObjectForKey:kJavenTicketContent];
    self.title = [aDecoder decodeObjectForKey:kJavenTicketTitle];
    return self;
}

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

    [aCoder encodeObject:_type forKey:kJavenTicketType];
    [aCoder encodeObject:_content forKey:kJavenTicketContent];
    [aCoder encodeObject:_title forKey:kJavenTicketTitle];
}

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

        copy.type = [self.type copyWithZone:zone];
        copy.content = [self.content copyWithZone:zone];
        copy.title = [self.title copyWithZone:zone];
    }
    
    return copy;
}


@end
