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

#import "IoTPlayerGroupsInfo.h"
#import "IoTResourceAttributes.h"

@implementation IoTPlayerGroupMemberInfo

- (instancetype)initWithName:(NSString *)name memberID:(NSString *)memberID
{
    self = [self init];
    if (self) {
        self->_memberName = [name copy];
        self->_memberID = [memberID copy];
    }
    return self;
}

@end

@implementation IoTPlayerGroupInfo

- (instancetype)initWithName:(NSString *)name
                     groupID:(NSString *)groupID
                     members:(NSArray<IoTPlayerGroupMemberInfo *> *)members
{
    self = [self init];
    if (self) {
        self->_groupName = [name copy];
        self->_groupID = [groupID copy];
        self->_members = [members copy];
    }
    return self;
}

@end

@interface IoTPlayerGroupsInfo ()

@property (nonatomic, copy) NSArray<IoTPlayerGroupInfo *> *groups;

@end

@implementation IoTPlayerGroupsInfo

- (instancetype)initWithGroupAttribute:(IoTGroupAttr *)attr
{
    self = [self init];
    if (self) {
        [self processAttribute:attr];
    }
    return self;
}

- (void)processAttribute:(IoTGroupAttr *)groupAttr
{
    NSMutableArray *groups = [[NSMutableArray alloc] initWithCapacity:groupAttr.groupInfo.count];
    for (IoTGroupInfoHelperAttr *attrGroup in groupAttr.groupInfo) {
        NSMutableArray *members = [[NSMutableArray alloc] initWithCapacity:attrGroup.groupMembersList.count];
        for (IoTGroupMembersHelperAttr *attrMember in attrGroup.groupMembersList) {
            IoTPlayerGroupMemberInfo *member = [[IoTPlayerGroupMemberInfo alloc] initWithName:attrMember.memberName
                                                                                     memberID:attrMember.memberId];
            [members addObject:member];
        }
        IoTPlayerGroupInfo *group = [[IoTPlayerGroupInfo alloc] initWithName:attrGroup.groupName
                                                                     groupID:attrGroup.groupId
                                                                     members:members];
        [groups addObject:group];
    }
    self.groups = groups;
}

@end
