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

#import "JavenLoginControl.h"


NSString *const kJavenLoginControlLoginIp = @"loginIp";
NSString *const kJavenLoginControlLoginTime = @"loginTime";
NSString *const kJavenLoginControlFailedTimes = @"failedTimes";


@interface JavenLoginControl ()

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

@end

@implementation JavenLoginControl

@synthesize loginIp = _loginIp;
@synthesize loginTime = _loginTime;
@synthesize failedTimes = _failedTimes;


+ (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.loginIp = [self objectOrNilForKey:kJavenLoginControlLoginIp fromDictionary:dict];
            self.loginTime = [self objectOrNilForKey:kJavenLoginControlLoginTime fromDictionary:dict];
            self.failedTimes = [[self objectOrNilForKey:kJavenLoginControlFailedTimes fromDictionary:dict] doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.loginIp forKey:kJavenLoginControlLoginIp];
    [mutableDict setValue:self.loginTime forKey:kJavenLoginControlLoginTime];
    [mutableDict setValue:[NSNumber numberWithDouble:self.failedTimes] forKey:kJavenLoginControlFailedTimes];

    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.loginIp = [aDecoder decodeObjectForKey:kJavenLoginControlLoginIp];
    self.loginTime = [aDecoder decodeObjectForKey:kJavenLoginControlLoginTime];
    self.failedTimes = [aDecoder decodeDoubleForKey:kJavenLoginControlFailedTimes];
    return self;
}

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

    [aCoder encodeObject:_loginIp forKey:kJavenLoginControlLoginIp];
    [aCoder encodeObject:_loginTime forKey:kJavenLoginControlLoginTime];
    [aCoder encodeDouble:_failedTimes forKey:kJavenLoginControlFailedTimes];
}

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

        copy.loginIp = [self.loginIp copyWithZone:zone];
        copy.loginTime = [self.loginTime copyWithZone:zone];
        copy.failedTimes = self.failedTimes;
    }
    
    return copy;
}


@end
