angular
	.module('dating.directives.videoPlayer', [
		'ngSanitize',
		'com.2fdevs.videogular',
		'com.2fdevs.videogular.plugins.controls',
		'com.2fdevs.videogular.plugins.overlayplay',
		'com.2fdevs.videogular.plugins.buffering'
	])
	.controller('VideoPlayerCtrl', function ($sce, $scope, $rootScope, authService, ProfilesModel, globalParamsService) {
		var GLOBALS = globalParamsService.GLOBALS;
		var videoPlayerCtrl = this;
		videoPlayerCtrl.currentUser = authService.currentUser;
		videoPlayerCtrl.isPremium = videoPlayerCtrl.currentUser && videoPlayerCtrl.currentUser.membership === 'premium';
		videoPlayerCtrl.isCreditsSite = GLOBALS.isCreditsSite;
		videoPlayerCtrl.insufficientCredits = false;
		videoPlayerCtrl.creditsPerVideoReveal = GLOBALS.creditsPerVideoReveal;
		videoPlayerCtrl.API = null;

		videoPlayerCtrl.theme = '/themes/scss/vendor/videogular/videogular.min.css';

		videoPlayerCtrl.config = {
			sources: [
				{
					src: $sce.trustAsResourceUrl(videoPlayerCtrl.videoSource),
					type: 'video/mp4'
				}
			]
		};

		videoPlayerCtrl.onPlayerReady = function (API) {
			videoPlayerCtrl.API = API;
			videoPlayerCtrl.API.playsInline = true;
			videoPlayerCtrl.progress = 0;
			videoPlayerCtrl.videoBlurred = false;
			API.setState('stop');
		};

		$scope.onCompletePlayer = function () {
			$rootScope.$broadcast('videoEnded', { stopped: true });
		};

		videoPlayerCtrl.onCanPlay = function () {
			$rootScope.$broadcast('canPlay', { canPlay: true });
			if (videoPlayerCtrl.autoplay) {
				videoPlayerCtrl.API.setVolume(0);
				videoPlayerCtrl.API.setState('play');
				videoPlayerCtrl.API.play();
			}
		};

		videoPlayerCtrl.onUpdateTime = function (currentTime) {
			if (videoPlayerCtrl.showNudityCover) {
				if (currentTime > videoPlayerCtrl.secondsForLoop) {
					videoPlayerCtrl.API.seekTime(0);
				}
			} else {
				if (Math.abs(currentTime - videoPlayerCtrl.progress) < 1) {
					videoPlayerCtrl.progress = currentTime;
				}
				if (
					videoPlayerCtrl.shouldBlur &&
					videoPlayerCtrl.secondsBeforeBlur &&
					currentTime >= videoPlayerCtrl.secondsBeforeBlur
				) {
					videoPlayerCtrl.videoBlurred = true;
				}
			}
			if (videoPlayerCtrl.stopAt && currentTime >= videoPlayerCtrl.stopAt) {
				videoPlayerCtrl.API.pause();
				videoPlayerCtrl.videoBlurred = true;
				$rootScope.$broadcast('videoEnded', { stopped: true });
			}
		};

		videoPlayerCtrl.onBarClick = function () {
			if (!videoPlayerCtrl.isPremium) {
				videoPlayerCtrl.API.seekTime(videoPlayerCtrl.progress);
			}
		};

		videoPlayerCtrl.revealVideo = function (videoId) {
			ProfilesModel.revealVideo(videoId).then(function (result) {
				if (result.data.res) {
					$rootScope.$broadcast('videoRevealed', { shouldBlur: false });
					videoPlayerCtrl.videoBlurred = false;
					videoPlayerCtrl.insufficientCredits = false;
				} else {
					videoPlayerCtrl.insufficientCredits = true;
					$rootScope.$broadcast('showPurchaseModal');
				}
			});
		};

		videoPlayerCtrl.redirectToUpgrade = function () {
			if (
				(videoPlayerCtrl.isCreditsSite && videoPlayerCtrl.insufficientCredits) ||
				(!videoPlayerCtrl.isCreditsSite && videoPlayerCtrl.videoBlurred)
			) {
				$('.fancybox-close').click();
				window.location.href = '/billing/upgrade';
			}
		};

		videoPlayerCtrl.setSafeMode = function () {
			ProfilesModel.setSafeMode().then(function () {
				$scope.$parent.videoPlayerCtrl.API.stop();
				$scope.$parent.videoPlayerCtrl.API.seekTime(0);
				var currentSafeModeValue = !videoPlayerCtrl.currentUser.safeMode;
				videoPlayerCtrl.currentUser.safeMode = currentSafeModeValue;
				$rootScope.$broadcast('setSafeMode', {
					safeMode: currentSafeModeValue
				});
				$rootScope.$broadcast('safeModeChanged');
			});
		};

		$scope.$on('sign-out:success', function () {
			videoPlayerCtrl.currentUser = null;
		});
	})
	.directive('videoPlayer', function () {
		return {
			restrict: 'E',
			controller: 'VideoPlayerCtrl',
			controllerAs: 'videoPlayerCtrl',
			bindToController: true,
			scope: {
				videoSource: '=videoSource',
				autoplay: '=autoplay',
				videoType: '@videoType',
				secondsBeforeBlur: '=secondsBeforeBlur',
				secondsForLoop: '=secondsForLoop',
				showNudityCover: '=showNudityCover',
				shouldBlur: '=shouldBlur',
				videoId: '=videoId',
				stopAt: '=stopAt'
			},
			templateUrl: globalParams.templateUrls.directives.video_player
		};
	})
	.directive('videoBlur', function () {
		return {
			restrict: 'E',
			controller: 'VideoPlayerCtrl',
			controllerAs: 'videoPlayerCtrl',
			bindToController: true,
			require: '^videogular',
			scope: {
				videoType: '=videoType',
				showNudityCover: '=showNudityCover',
				videoBlurred: '=videoBlurred',
				videoId: '=videoId',
				insufficientCredits: '=insufficientCredits'
			},
			templateUrl: globalParams.templateUrls.directives.video_blur
		};
	});
