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

#import "JavenPasswordControl.h"


NSString *const kJavenPasswordControlCreated = @"created";
NSString *const kJavenPasswordControlInitPassword = @"initPassword";
NSString *const kJavenPasswordControlExpiry = @"expiry";


@interface JavenPasswordControl ()

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

@end

@implementation JavenPasswordControl

@synthesize created = _created;
@synthesize initPassword = _initPassword;
@synthesize expiry = _expiry;


+ (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.created = [self objectOrNilForKey:kJavenPasswordControlCreated fromDictionary:dict];
            self.initPassword = [self objectOrNilForKey:kJavenPasswordControlInitPassword fromDictionary:dict];
            self.expiry = [self objectOrNilForKey:kJavenPasswordControlExpiry fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.created forKey:kJavenPasswordControlCreated];
    [mutableDict setValue:self.initPassword forKey:kJavenPasswordControlInitPassword];
    [mutableDict setValue:self.expiry forKey:kJavenPasswordControlExpiry];

    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.created = [aDecoder decodeObjectForKey:kJavenPasswordControlCreated];
    self.initPassword = [aDecoder decodeObjectForKey:kJavenPasswordControlInitPassword];
    self.expiry = [aDecoder decodeObjectForKey:kJavenPasswordControlExpiry];
    return self;
}

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

    [aCoder encodeObject:_created forKey:kJavenPasswordControlCreated];
    [aCoder encodeObject:_initPassword forKey:kJavenPasswordControlInitPassword];
    [aCoder encodeObject:_expiry forKey:kJavenPasswordControlExpiry];
}

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

        copy.created = [self.created copyWithZone:zone];
        copy.initPassword = [self.initPassword copyWithZone:zone];
        copy.expiry = [self.expiry copyWithZone:zone];
    }
    
    return copy;
}


@end
