/**
 * @ngdoc function
 * @name domainManagerApp.controller:AboutCtrl
 * @description
 * # AboutCtrl
 * Controller of the domainManagerApp
 */
angular.module('goApp.navigation', ['famous.angular', 'ngRoute', 'ngFx', 'ngAnimate', 'goApp.data'])
  .controller('NavigationController',['$scope', '$rootScope', '$famous', '$timeline', '$http', '$location', '$route', '$timeout', 'Data', '$mixpanel', function ($scope,  $rootScope, $famous, $timeline, $http, $location, $route, $timeout, Data, $mixpanel) {

    'use strict';
    $mixpanel.track('Index View');
    var bg_loaded = false;
    var menu_opened = false;
    $scope.lang = 0;

    $rootScope.$on('background:loaded', function() {
      if(Data.isLangSet()){
        openMenu();
      }
      bg_loaded = true;
    });

    $rootScope.$on('data:lang_set', function() {
      if(bg_loaded){
        openMenu();
      }
      $scope.lang = Data.getLang();
    });

    var openMenu = function() {
      if(!$scope.$$phase) {
        $scope.$apply(function(){
          $scope.navigation = filterByTranslation(Data.getNavigation());
          if(menu_opened === false){
            menu_opened = true;
            $scope.menu1pos = getMenuPos($scope.navigation.length);
            startRandomBgText();
          }
        });
      } else {
        $scope.navigation = filterByTranslation(Data.getNavigation());
        if(menu_opened === false){
          menu_opened = true;
          $scope.menu1pos = getMenuPos($scope.navigation.length);
          startRandomBgText();
        }
      }
    };

    $scope.subNavigation = [];
    $scope.sub2Navigation = [];
    $scope.last_click = "";
    $scope.last_click2 = "";

    $scope.btnNavclick = function(index){
      if($scope.navigation[index].link == 'submenu') {
        if($scope.last_click != $scope.navigation[index].title){
          $scope.menu2pos = getMenuPos($scope.navigation[index].submenu.length);
          console.log("Opening submenu: " + $scope.navigation[index].title);
          $scope.sub2Navigation = [];
          $scope.subNavigation = filterByTranslation($scope.navigation[index].submenu);
        } else if ($scope.last_click === $scope.navigation[index].title && $scope.subNavigation.length === 0) {
          $scope.menu2pos = getMenuPos($scope.navigation[index].submenu.length);
          $scope.subNavigation = filterByTranslation($scope.navigation[index].submenu);
          console.log("Opening submenu: " + $scope.navigation[index].title);
        } else {
          $scope.subNavigation = [];
          $scope.sub2Navigation = [];
          console.log("Closing submenu: " + $scope.navigation[index].title);
        }
        $scope.last_click = $scope.navigation[index].title;
      } else {
        console.log("> Redirecting to page " + $scope.navigation[index].link);
        $location.path($scope.navigation[index].link);
      }
    };

    $scope.btnSubNavclick = function(index){
      if($scope.subNavigation[index].link == "submenu") {
        if($scope.last_click2 != $scope.subNavigation[index].title){
          $scope.menu3pos = getMenuPos($scope.subNavigation[index].submenu.length);
          $scope.sub2Navigation = [];
          $scope.sub2Navigation = filterByTranslation($scope.subNavigation[index].submenu);
        } else if ($scope.last_click2 === $scope.subNavigation[index].title && $scope.sub2Navigation.length === 0){
          $scope.menu3pos = getMenuPos($scope.subNavigation[index].submenu.length);
          $scope.sub2Navigation = filterByTranslation($scope.subNavigation[index].submenu);
          console.log("Opening submenu: " + $scope.subNavigation[index].title);
        } else {
          $scope.sub2Navigation = [];
          console.log("Closing submenu: " + $scope.subNavigation[index].title);
        }
        $scope.last_click2 = $scope.subNavigation[index].title;
      } else {
        console.log("> Redirecting to page " + $scope.subNavigation[index].link);
        $location.path($scope.subNavigation[index].link);
      }
    };

    $scope.btnSub2Navclick = function(index) {
      if($scope.sub2Navigation[index].link != "submenu") {
        console.log("> Redirecting to page " + $scope.sub2Navigation[index].link);
        $location.path($scope.sub2Navigation[index].link);
      } else {
        console.log("* Error: Only 2 levels of submenu allowed.");
      }
    };

    var getMenuPos = function(len) {
      var menu_size = len * 20;
      var max = window.innerHeight - menu_size - 25;
      var pos = Math.floor(Math.random()*(max-15+1)+15);
      return {'margin-top': pos };
    };

    var filterByTranslation = function(nav){
      var new_nav = [];
      for (var i = 0; i < nav.length; i++) {
        var obj = nav[i];
        if(obj.i18n !== undefined){
          if(obj.i18n[$scope.lang] !== ""){
            new_nav.push(obj);
          }
        }
      }
      return new_nav;
    };

    $scope.random_text = "";
    $scope.show_bg_text = false;

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

    var startRandomBgText = function(){
      console.log("> Starting Random BG Text Function");
      $timeout(function(){
        console.log("> Showing BG Text");
        $scope.random_text = "<p><span>Se a mão lança</span><br><span>no ar não fica</span><br><span>se a mão alcança</span><br><span>o que caiu</span></p></p>";
        $scope.bg_text_style = randomPos(130, 50);
        $scope.show_bg_text = true;
        $timeout(function(){
          console.log("> Hiding BG Text");
          $scope.random_text = "";
          $scope.show_bg_text = false;
          startRandomBgText();
        }, randomIntFromInterval(8000, 15000));
      }, randomIntFromInterval(12000, 30000));
    };

    var randomPos = function(width, height){
      var max_v = window.innerHeight - height - 50;
      var pos_v = Math.floor(Math.random() * max_v);
      var max_h = window.innerWidth - width - 50;
      var pos_h = Math.floor(randomIntFromInterval((window.innerWidth/3),max_h));
      return {'top': pos_v, 'left' : pos_h, 'width' : width, 'height' : height };
    };

  }]);