//
//  extras.m
//  geometrixx
//
//  Created by ATS on 1/1/15.
//  Copyright (c) 2015 Adobe Training Services. All rights reserved.
//

#import <Foundation/Foundation.h>



#import "extras.h"
#import <MediaPlayer/MediaPlayer.h>
#import "ADBMobile.h"

@interface extras () {
    MPMoviePlayerViewController *movieViewController;
}

@property (weak, nonatomic) IBOutlet UILabel *targetMessage;

@end

@implementation extras

- (void)viewDidAppear:(BOOL)animated{
    [ADBMobile trackState:@"Extras" data:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self configureMedia];

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"geometrixx_background.png"]];
}

- (IBAction)targetMbox:(id)sender {
    NSDictionary *parametersTarget = @{
                                       @"someTargetParam1":@"apple",
                                       @"someTargetParam2":@"oranges"};
    
    
    //Step 1 - Create an mBox
    //This is an mBox by a different name (i.e. TargetLocationRequest)
    ADBTargetLocationRequest *myRequest = [ADBMobile targetCreateRequestWithName:@"targetExample"
                                                                  defaultContent:@"default.png"
                                                                      parameters:parametersTarget];
    
    //Step 2 - Load content into the the mBox
    [ADBMobile targetLoadRequest:myRequest
                        callback:^(NSString *content) {
                            NSLog(content);
                        }];
    
    
   /*
    NSDictionary *parametersTarget = @{
     @"someTargetParam1":@"apple",
     @"someTargetParam2":@"oranges"};
    
    
    //Step 1 - Create an mBox
    //This is an mBox by a different name (i.e. TargetLocationRequest)
    
    ADBTargetLocationRequest *myRequest = [ADBMobile targetCreateRequestWithName:@"targetExample"
                                                                  defaultContent:@"default.png"
                                                                      parameters:parametersTarget];
    
    //Step 2 - Load content into the the mBox
    [ADBMobile targetLoadRequest:myRequest
                        callback:^(NSString *content) {
                            NSLog(content);
                        }];
    
    */
    
}

- (IBAction)targetSuccessExample:(id)sender {
    
    //Step 1 - Create an mBox
    //This is an order Confirmation mBox
    ADBTargetLocationRequest *myConfirmRequest = [ADBMobile targetCreateOrderConfirmRequestWithName:@"targetSuccessExample" orderId:@"13245" orderTotal:@"49.99" productPurchasedId:@"1234wxyz" parameters:nil];
    
    
    //Step 2 - Load content into the the mBox
    [ADBMobile targetLoadRequest:myConfirmRequest
                        callback:^(NSString *content) {
                            NSLog(content);
                        }];
}

- (IBAction)clearTargetCookies:(id)sender {
    
    [ADBMobile targetClearCookies];
    
}




- (IBAction)playVideoButton:(id)sender {
    
    // Plays our "iGermx2" commercial - configuration below
    [self configureMediaTracking];
    [self playMedia];
    
}


/*
 /
 \
 // CONFIGURE OUR VIDEO TRACKING * * * * * * * * * * * * * * * * * * * * * * * *
 /
 \
 */
- (void) configureMediaTracking {
    ADBMediaSettings *mediaSettings = [ADBMobile mediaCreateSettingsWithName:MEDIA_NAME length:MEDIA_LENGTH playerName:PLAYER_NAME playerID:PLAYER_ID];
    
    //Choose Milestone
    mediaSettings.milestones = @"25,50,75";
    mediaSettings.segmentByMilestones = YES;
    
    // or choose Offset Milestone (seconds from start of video)
    /* mediaSettings.offsetMilestones = @"60,120";
     mediaSettings.segmentByOffsetMilestones = YES;
     */
    
    // seconds tracking - sends a hit every x seconds
   // mediaSettings.trackSeconds = 30; // sends a hit every 30 seconds
    
    
    // opens the media item, preparing it for tracking
    
    [ADBMobile mediaOpenWithSettings:mediaSettings callback:nil];
    
}

//Video details
static NSString *const VIDEO_URL	= @"http://ats.adobe.com/jjsite/include/downloads/igermx2.mp4";
static NSString *const MEDIA_NAME	= @"iGermx2 Advertisement";
static NSString *const PLAYER_NAME	= @"MPMoviePlayer";
static NSString *const PLAYER_ID	= @"MPMoviePlayer1";
static double const MEDIA_LENGTH	= 146;


// set up the movie player and notifications
- (void) configureMedia {
    NSURL *movieURL = [NSURL URLWithString:VIDEO_URL];
    movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    
    [self configureNotifications:movieViewController.moviePlayer];
}

// set up the notifications we are intereseted in for tracking
- (void) configureNotifications:(MPMoviePlayerController *) movieController {
    // this is where we will close our media item
    [[NSNotificationCenter defaultCenter]
     addObserver: self
     selector: @selector(mediaFinishedCallback:)
     name: MPMoviePlayerPlaybackDidFinishNotification
     object: movieController];
    
    // this notifies us so we can play/pause/stop our tracking on a media item
    [[NSNotificationCenter defaultCenter]
     addObserver: self
     selector: @selector(mediaPlaybackChangedCallback:)
     name: MPMoviePlayerPlaybackStateDidChangeNotification
     object: movieController];
}





// display and begin playing the media
- (void) playMedia {
    [self presentMoviePlayerViewControllerAnimated:movieViewController];
    [movieViewController.moviePlayer play];
}

// this notifies us so we can close our media item
- (void) mediaFinishedCallback: (NSNotification*) notification {
    /*
     * Adobe Tracking - Media
     *
     * closes the media item
     * media item should recieve no more calls unless its a new open and play
     */
    [ADBMobile mediaClose:MEDIA_NAME];
}


// this notifies us so we can play/pause/stop our tracking on a media item
- (void) mediaPlaybackChangedCallback: (NSNotification*) notification {
    MPMoviePlayerController *mediaController = notification.object;
    if (mediaController.playbackState == MPMoviePlaybackStatePlaying) {
        /*
         * Adobe Tracking - Media
         *
         * sets the media item into a playing state
         * if this is the initial play, it begins the tracking and monitor for the media item
         */
        [ADBMobile mediaPlay:MEDIA_NAME offset: isnan(mediaController.currentPlaybackTime) ? 0.0 : mediaController.currentPlaybackTime];
    }
    else if (mediaController.playbackState == MPMoviePlaybackStateSeekingBackward) {
        /*
         * Adobe Tracking - Media
         *
         * stops the media item
         * we need to stop here because we are no longer in a playing state
         */
        [ADBMobile mediaStop:MEDIA_NAME offset:mediaController.currentPlaybackTime];
    }
    else if (mediaController.playbackState == MPMoviePlaybackStateSeekingForward) {
        /*
         * Adobe Tracking - Media
         *
         * stops the media item
         * we need to stop here because we are no longer in a playing state
         */
        [ADBMobile mediaStop:MEDIA_NAME offset:mediaController.currentPlaybackTime];
    }
    else if (mediaController.playbackState == MPMoviePlaybackStatePaused) {
        /*
         * Adobe Tracking - Media
         *
         * stops the media item
         * we need to stop here because we are no longer in a playing state
         */
        [ADBMobile mediaStop:MEDIA_NAME offset:mediaController.currentPlaybackTime];
    }
    else if (mediaController.playbackState == MPMoviePlaybackStateInterrupted) {
        /*
         * Adobe Tracking - Media
         *
         * stops the media item
         * we need to stop here because we are no longer in a playing state
         */
        [ADBMobile mediaStop:MEDIA_NAME offset:mediaController.currentPlaybackTime];
    }
    else if (mediaController.playbackState == MPMoviePlaybackStateStopped) {
        /*
         * Adobe Tracking - Media
         *
         * stops the media item
         * we need to stop here because we are no longer in a playing state
         */
        [ADBMobile mediaStop:MEDIA_NAME offset:mediaController.currentPlaybackTime];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end