/**
 * @ngdoc function
 * @name domainManagerApp.controller:AboutCtrl
 * @description
 * # AboutCtrl
 * Controller of the domainManagerApp
 */

angular.module('goApp.background', ['famous.angular', 'goApp.data'])
  .controller('BackgroundController',['$scope', '$rootScope', '$famous', '$timeline', 'Data', '$timeout', function ($scope,  $rootScope, $famous, $timeline, Data, $timeout) {

    'use strict';

    $scope.videoList = [];
    $scope.current_video_index = 0;

    // Functions

    $scope.loadBackgroundData = function() {
      $scope.videos = Data.getBackgrounds();
      $rootScope.$on('data:loaded', function(data) {
        if(!$scope.$$phase) {
          $scope.$apply(function(){
            $scope.videos = Data.getBackgrounds();
            $scope.preload();
          });
        } else {
          $scope.videos = Data.getBackgrounds();
          $scope.preload();
        }
      });
    };

    $rootScope.$on('change-bg-video', function(data, index) {
      if(!$scope.$$phase) {
        $scope.$apply(function(){
          $scope.playVideo(index);
        });
      } else {
        $scope.playVideo(index);
      }
    });

    $scope.playVideo = function(index){
      $scope.current_video_index = index;
      console.log("switching to video " + index + ": " + $scope.videoList[$scope.current_video_index]);
      $scope.lightbox.show($scope.videoList[$scope.current_video_index]);
    };

    $scope.preload = function() {
      $scope.current_video = $scope.videos[0];
      console.log($scope.videos);
      console.log("> Preloading video: " + $scope.current_video);
      var preload = new createjs.LoadQueue();
      preload.addEventListener("complete", handleFileComplete);
      // for (var i = 0; i < $scope.videos.length; i++) {
      //     preload.loadFile($scope.videos[i]);
      // }
      preload.loadFile($scope.current_video);
      function handleFileComplete(event) {
          console.log("> Preload Complete");
          $scope.createBackgroundVideos();
          $timeout(function(){
            $rootScope.$broadcast('background:loaded');
          },1000);
      }
    };

    $scope.randomIntFromInterval = function(min,max) {
        return Math.floor(Math.random()*(max-min+1)+min);
    };



    $scope.createBackgroundVideos = function() {
      console.log("> Creating videos:");
      for (var i = 0; i < $scope.videos.length; i++) {
        console.log("> adding video " + $scope.videos[i]);
        var video = new VideoSurface({
            size: [undefined, undefined],
            autoplay: true,
            src: $scope.videos[i],
            classes: ['bg_video'],
            properties: {
                zIndex: 1,
            }
        });
        video.setAttributes({ loop: '' });
        var newView = new View();
        newView.add(videoModifier).set(video);
        $scope.videoList.push(newView);
      }
      var lightboxOpts = {
  			inTransform: Transform.translate(0, 0, 0),
  			outTransform: Transform.translate(0, 0, 1),
  			inTransition: { duration: 1 },
  			outTransition: { duration: 1 },
  			inOpacity: 1,
  			outOpacity: 1,
  			overlap: false
  		};
      $scope.lightbox.setOptions(lightboxOpts);
      $scope.lightbox.show($scope.videoList[$scope.current_video_index]);
      $scope.backgroundView.add($scope.lightbox);
    };


    var View = $famous['famous/core/View'];
    var Modifier = $famous['famous/core/Modifier'];
    var Surface = $famous['famous/core/Surface'];
    var Transform = $famous['famous/core/Transform'];
    var VideoSurface = $famous['famous/surfaces/VideoSurface'];
    var Lightbox = $famous['famous/views/Lightbox'];
    $scope.backgroundView = new View();
    $scope.lightbox = new Lightbox();

    $scope.loadBackgroundData();

    var videoModifier = new Modifier();
    videoModifier.transformFrom(function(){
        //transform: Transform.translate(0, 0, 0);
        Transform.translate(0, 0, 0);
    });

  }]);
