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

#import "IoTPlaylist.h"
#import "IoTPlaylist+Private.h"
#import "IoTPlayerGroup+Private.h"

#define GETTER(type, name) \
- (type)name { \
	return [ self.playerGroup name ]; \
} \

#define ACTION(method) \
- (void)method##WithCompletion:(void(^)(BOOL, NSError *))completion \
               completionQueue:(dispatch_queue_t)queue \
{ \
	[self.playerGroup method##WithCompletion:completion \
							completionQueue:queue]; \
} \

#define ACTION_WITH_PARAM(method, type, param) \
- (void)method:(type)param \
    completion:(void(^)(BOOL, NSError *))completion \
completionQueue:(dispatch_queue_t)queue \
{ \
	[self.playerGroup method:param \
				completion:completion \
           completionQueue:queue]; \
} \

@interface IoTPlaylist ()

@property (nonatomic, weak) IoTPlayerGroup * playerGroup;

@end

@implementation IoTPlaylist

- (instancetype)initWithPlayerGroup:(IoTPlayerGroup *)playerGroup
{
    self = [super init];
    if (self) {
        self->_playerGroup = playerGroup;
    }
    return self;
}

GETTER(IoTMediaItem *,currentItem)
GETTER(int,indexPlaying)

GETTER(BOOL,isShuffleModeEnabled)
GETTER(BOOL,isLoopModeEnabled)
GETTER(IoTLoopMode,loopMode)
GETTER(IoTShuffleMode,shuffleMode)

ACTION_WITH_PARAM(setLoopMode, IoTLoopMode, loopMode)
ACTION_WITH_PARAM(setShuffleMode, IoTShuffleMode, shuffleMode)
ACTION_WITH_PARAM(playItemAtIndex, int, index)
ACTION_WITH_PARAM(removeMediaItemsInRange, NSRange, range)
ACTION(clear)

- (int)count
{
    return [self.playerGroup playlistSize];
}

- (NSString *)userData
{
    return [self.playerGroup playlistUserData];
}

- (void)addMediaItem:(IoTMediaItem *)mediaItem
             atIndex:(int)index
                play:(BOOL)play
            userData:(NSString *)userData
          completion:(void(^)(BOOL, NSError *))completion
     completionQueue:(dispatch_queue_t)queue
{
    [self.playerGroup addMediaItem:mediaItem
                           atIndex:index
                              play:play
                          userData:userData
                        completion:completion
                   completionQueue:queue];
}

- (void)addMediaItems:(NSArray *)mediaItems
              atIndex:(int)index
                 play:(BOOL)play
             userData:(NSString *)userData
           completion:(void(^)(BOOL, NSError *))completion
      completionQueue:(dispatch_queue_t)queue
{
    [self.playerGroup addMediaItems:mediaItems
                            atIndex:index
                               play:play
                           userData:userData
                         completion:completion
                    completionQueue:queue];
}

- (void)moveMediaItemsInRange:(NSRange)range
                     position:(int)position
                   completion:(void(^)(BOOL, NSError *))completion
              completionQueue:(dispatch_queue_t)queue
{
    [self.playerGroup moveMediaItemsInRange:range
                                   position:position
                                 completion:completion
                            completionQueue:queue];
}

- (void)getItemsInRange:(NSRange)range
             completion:(void(^)(BOOL, NSArray<IoTMediaItem *> *,NSError *))completion
        completionQueue:(dispatch_queue_t)queue
{
    [self.playerGroup getItemsInRange:range
                           completion:completion
                      completionQueue:queue];
}

@end
