/**
 * @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';

    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'];
    $scope.backgroundView = new View();

    var video = new VideoSurface({
        size: [undefined, undefined],
        autoplay: true,
        classes: ['bg_video'],
        properties: {
            zIndex: 1,
        }
    });
    var videoModifier = new Modifier();
    videoModifier.transformFrom(function(){
        //transform: Transform.translate(0, 0, 0);
        Transform.translate(0, 0, 0);
    });
    var layer = 1;

    // 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();
        }
      });
    };

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

    };

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

    $scope.startBackgroundVideo = function() {
      console.log("> Playing bg video " + $scope.current_video);
      video.setAttributes({ src: $scope.current_video });
      video.setAttributes({ loop: '' });
      $scope.backgroundView.add(videoModifier).add(video);
      $timeout(function(){
        $scope.preload();
      },wait_time[1]);
    };

    var getVideo = function() {
      $scope.current_video = $scope.videos[$scope.randomIntFromInterval(0,$scope.videos.length)];
      if($scope.current_video === undefined || $scope.current_video == "undefined"){
        getVideo();
      }
      if($scope.current_video == $scope.last_video){
        getVideo();
      }
    }

    var wait_time = [3000, 1, 0]
    var timer = function() {
      wait_time[2] = wait_time[2] + 1;
      if(wait_time[2] == 1) {
        wait_time = [1, 10000, wait_time[2]];
      }
      if(wait_time[2] > 1) {
        wait_time = [2000, 30000, wait_time[2]];
      }
      return wait_time;
    }

    // Start Background Controller

    $scope.loadBackgroundData();

  }]);
