//
//  Created by Open Reply on 22/01/14.
//

#import "SplashClipViewController.h"
#import "FrequencyCapManager.h"
#import <SCInstreamSDK/SCInstreamSDK.h>

@interface SplashClipViewController () <SCInstreamDelegate>

@property (nonatomic, strong) SCInstreamController *instreamController;

@end


@implementation SplashClipViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	
	// check for clip availability
	BOOL canShowClip = [FrequencyCapManager canDisplayAdForItemWithId:@"preapp"];
	
	if (canShowClip) {
		[self setupSCInstreamSDK];
        
	} else {
		DDLog(@"SMART CLIP cannot display AD");
    }
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
    
    if (_instreamController) {
		[_instreamController beforeContent];
        
	} else {
		[self.delegate splashClipCompleted:self];
    }
}

- (BOOL)shouldAutorotate {
	return YES;
}

- (void)dealloc {
    _instreamController = nil;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
	return UIInterfaceOrientationLandscapeLeft;
}

- (NSUInteger)supportedInterfaceOrientations {
	return UIInterfaceOrientationMaskLandscapeLeft;
}

- (void)setupSCInstreamSDK {
	self.instreamController = [[SCInstreamController alloc] initInView:_cntAdv withDelegate:self];
    
    NSString *prerollURL = @"";
    
#if SMARTCLIP_ENVIRONMENT == 0
    /**
     *  URL di TEST
     */
    prerollURL = @"http://ae.amgdgt.com/ads?t=de&p=9372&pl=teststandard&cat=ms_vast2_nofc&sz=400x320&rnd=[random]";
#else
    /**
     *  URL di PRODUCTION
     */
    prerollURL = @"http://ae.amgdgt.com/ads?t=de&p=9372&pl=f164e272&sz=400x320&rnd=[random]";
#endif
    
	[_instreamController.config insertAdURL:[NSURL URLWithString:prerollURL] forAdType:SCInstreamTypePreroll];
	_instreamController.config.showLoadingView = YES;
	_instreamController.config.adCountdownConfiguration.enabled = NO;
    // _instreamController.config.clickThruAlertEnabled = NO;
	_instreamController.config.clickThruAlertTitle = @"Pubblicità";
	_instreamController.config.clickThruAlertMessage = @"Vuoi accedere al sito Sponsor?";
	_instreamController.config.clickThruAlertCancelButtonTitle = @"No, non ora";
	_instreamController.config.clickThruAlertOpenButtonTitle = @"Vai al sito!";
	
	// get skip delay
	NSInteger skipDelay = [FrequencyCapManager getSkipAfterSecsValueForItemWithId:@"preapp"];
	[_instreamController showSkipButtonAfter:skipDelay];
}

#pragma mark - SCInstreamDelegate

- (void)instreamControllerProceedStart:(SCInstreamController *)controller {
	[FrequencyCapManager setDisplayConsumedForItemWithId:@"preapp"];
	[self.delegate splashClipCompleted:self];
}

- (void)instreamControllerProceedHandleFinished:(SCInstreamController *)controller {
	
}

- (CGRect)frameForInstreamController:(SCInstreamController *)controller {
	return _cntAdv.bounds;
}

- (void)instreamControllerShouldPause:(SCInstreamController *)controller {
	
}

- (void)instreamControllerShouldResume:(SCInstreamController *)controller {
	
}

- (void)instreamController:(SCInstreamController *)controller willPlayAdInSlot:(SCSlotType)slot {
    
}

@end
