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

#import "IoTPlayerGroupMember+Private.h"
#import "IoTClient+Private.h"
#import "IoTPlayer+Private.h"

@interface IoTPlayerGroupMember ()

@property (nonatomic, weak) IoTClient *client;
@property (nonatomic, copy, readonly) NSString *storedName;

@end

@implementation IoTPlayerGroupMember

- (instancetype)initWithStoredName:(NSString *)storedName playerID:(NSString *)playerID client:(IoTClient *)client
{
    self = [self init];
    if (self) {
        self->_playerID = [playerID copy];
        self->_storedName = [storedName copy];
        self->_client = client;
    }
    return self;
}

- (NSString *)displayName
{
    IoTPlayer *player = [_client playerForID:self.playerID];
    return [player displayName] ?: self.storedName;
}

- (BOOL)available
{
	IoTPlayer *player = [_client playerForID:self.playerID];
    return player != nil;
}

- (BOOL)isEqual:(id)object
{
    if (self == object) {
        return YES;
    }

    if (![object isKindOfClass:[IoTPlayerGroupMember class]]) {
        return NO;
    }

    return self.hash == ((IoTPlayerGroupMember *)object).hash;
}

- (NSUInteger)hash
{
    NSString *availablityStr = self.available ? @"YES" : @"NO";
    return self.displayName.hash ^ self.playerID.hash ^ availablityStr.hash;
}

@end
