//
// © 2018-2020 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
//

#import "IoTServiceDiscoveryResult.h"

@interface IoTServiceDiscoveryResult ()

@property (nonatomic, copy) NSString *identifier;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *host;
@property (nonatomic, copy) NSString *type;
@property (nonatomic, assign) NSTimeInterval lastSeen;


@end

@implementation IoTServiceDiscoveryResult

+ (instancetype)resultWithIdentifier:(NSString *)identifier
                                name:(NSString *)name
                                host:(NSString *)host
                                type:(NSString *)type
{
    IoTServiceDiscoveryResult *me = [[self alloc] init];
    me.identifier = identifier;
    me.name = name;
    me.host = host;
    me.type = type;
    me.lastSeen = [NSDate timeIntervalSinceReferenceDate];
    return me;
}

- (NSString *)description
{
	NSTimeInterval age = fabs([NSDate timeIntervalSinceReferenceDate] - self.lastSeen);
    return [NSString stringWithFormat:@"Host: %@ Last Seen %.0fs ago", self.host, age];
}

- (NSUInteger)hash
{
    return [self.identifier hash] ^ [self.name hash] ^ [self.host hash] ^ [self.type hash];
}

- (BOOL)isEqual:(id)object
{
    if (![object isKindOfClass:[IoTServiceDiscoveryResult class]]) {
        return NO;
    }
    return [object hash] == [self hash];
}

- (void)markAsSeen
{
	self.lastSeen = [NSDate timeIntervalSinceReferenceDate];
}

- (void)markAsExpired
{
	self.lastSeen = [[NSDate distantPast] timeIntervalSinceReferenceDate];
}

@end
