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

#import "UserInfoPasswordControl.h"


NSString *const kUserInfoPasswordControlCreated = @"created";
NSString *const kUserInfoPasswordControlExpiry = @"expiry";
NSString *const kUserInfoPasswordControlInitPassword = @"InitPassword";


@interface UserInfoPasswordControl ()

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

@end

@implementation UserInfoPasswordControl

@synthesize created = _created;
@synthesize expiry = _expiry;
@synthesize InitPassword = _InitPassword;


+ (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:kUserInfoPasswordControlCreated fromDictionary:dict] doubleValue];
            self.expiry = [[self objectOrNilForKey:kUserInfoPasswordControlExpiry fromDictionary:dict] doubleValue];
            self.InitPassword = [self objectOrNilForKey:kUserInfoPasswordControlInitPassword fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithDouble:self.created] forKey:kUserInfoPasswordControlCreated];
    [mutableDict setValue:[NSNumber numberWithDouble:self.expiry] forKey:kUserInfoPasswordControlExpiry];
    [mutableDict setValue:self.InitPassword forKey:kUserInfoPasswordControlInitPassword];

    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 decodeDoubleForKey:kUserInfoPasswordControlCreated];
    self.expiry = [aDecoder decodeDoubleForKey:kUserInfoPasswordControlExpiry];
    self.InitPassword = [aDecoder decodeObjectForKey:kUserInfoPasswordControlInitPassword];
    return self;
}

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

    [aCoder encodeDouble:_created forKey:kUserInfoPasswordControlCreated];
    [aCoder encodeDouble:_expiry forKey:kUserInfoPasswordControlExpiry];
    [aCoder encodeObject:_InitPassword forKey:kUserInfoPasswordControlInitPassword];
}

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

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


@end
