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

#import "ViewController.h"
#import "IoTControllerSDK.h"

@interface ViewController () <IoTAppListenerProtocol>

@property (nonatomic, strong) IoTClient *iotClient;
@property (nonatomic, weak) IoTPlayerGroup *playingGroup;

@end

@implementation ViewController

- (void)viewDidLoad {
    self.toggleButton.enabled = NO;
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.iotClient = [[IoTClient alloc] initWithApplicationName:@"test"];

    self.iotClient.delegate = self;
    [self.iotClient start];
}

- (void)playerAdded:(IoTPlayer *)player
{
	NSLog(@"**** Got player: %@",player);
}

- (void)deviceAdded:(IoTDevice *)device
{
    NSLog(@"**** Got device: %@",device);
}

- (void)playerGroupAdded:(IoTPlayerGroup *)group
{
    NSLog(@"**** Got group: %@",group);

    if (!self.playingGroup) {
        self.playingGroup = group;
        IoTMediaItem *mediaItem = [[IoTMediaItem alloc] init];
        mediaItem.streamURL = @"http://upload.wikimedia.org/wikipedia/commons/4/42/Bach_-_cantata_140._7._chorale.ogg";

        [[self.playingGroup playlist] addMediaItem:mediaItem
                                           atIndex:0
                                              play:YES
                                          userData:@""
                                        completion:^(BOOL success, NSError * error) {
                                            NSLog(@"**** Play command: %@", success ? @"SUCCESS" : [NSString stringWithFormat:@"FAILURE: %@", error]);
                                            if (success) {
                                                self.label.text = self.playingGroup.displayName;
                                                self.toggleButton.enabled = YES;
                                                [self.toggleButton setTitle:@"PAUSE" forState:UIControlStateNormal];
                                            } else {
                                                self.playingGroup = nil;
                                            }
                                        }
                                   completionQueue:dispatch_get_main_queue()];
    }
}

- (void)playerGroupRemoved:(IoTPlayerGroup *)group
{
    NSLog (@"**** Group removed: %@", group);
    if ([self.playingGroup.groupID isEqualToString:group.groupID]) {
        self.playingGroup = nil;
        self.label.text = @"Nothing";
        self.toggleButton.enabled = NO;
        [self.toggleButton setTitle:@"PLAY" forState:UIControlStateNormal];
    }
}

- (IBAction)toggle:(id)sender
{
    if (!self.playingGroup) {
        return;
    }

    if (self.playingGroup.playState == IoTPlayStatePaused) {
        [self.playingGroup playWithCompletion:^(BOOL success, NSError * error) {
            NSLog(@"**** Resume command: %@", success ? @"SUCCESS" : [NSString stringWithFormat:@"FAILURE: %@", error]);
            if (success) {
                [self.toggleButton setTitle:@"PAUSE" forState:UIControlStateNormal];
            }
        } completionQueue:dispatch_get_main_queue()];
    } else {
        [self.playingGroup pauseWithCompletion:^(BOOL success, NSError *error) {
            NSLog(@"**** Pause command: %@", success ? @"SUCCESS" : [NSString stringWithFormat:@"FAILURE: %@", error]);
            if (success) {
                [self.toggleButton setTitle:@"PLAY" forState:UIControlStateNormal];
            }
        } completionQueue:dispatch_get_main_queue()];
    }
}

@end
