app.controller('ReadyMenuSecondController',['$scope','$location','$timeout','$window','$interval',function($scope,$location,$timeout,$window,$interval){
  $scope.currentPath = $location.path();

  if($scope.currentPath.indexOf('loss') > -1){
    $scope.filterBy = 'loss';
  } else if($scope.currentPath.indexOf('gain') > -1){
    $scope.filterBy = 'gain';
  } else if($scope.currentPath.indexOf('balance') > -1){
    $scope.filterBy = 'balance';
  }

  var firebaseDiets = firebase.database().ref().child('diets');
  $interval(function(){
    if(firebase.auth().currentUser){
      $scope.firebaseShoppingCart = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('shoppingCart');
    }
  },1000)
  $scope.diets = {};
  firebaseDiets.on('value', snap => {
    $timeout(function(){
      $scope.diets = snap.val();
    })

  })

  $scope.weekCount = "1";
  // $scope.selectArr = [
  //   {val: '1', name: 'week 1', isSelected:'true'},
  //   {val: '2', name: 'week 2', isSelected:'false'},
  //   {val: '3', name: 'week 3', isSelected:'false'},
  //   {val: '4', name: 'week 4', isSelected:'false'},
  // ]

  // scroll

  var JQWindow = angular.element($window);

  JQWindow.bind('scroll', function(){

    // if(window.pageYOffset >= '10'){
    //   var item = document.querySelector('.ready-menu-second .item:nth-child(1)');
    //   var JQItem = angular.element(item);
    //   JQItem.addClass('ready-menu-second-scroll-class');
    // } else{
    //   var item = document.querySelector('.ready-menu-second .item:nth-child(1)');
    //   var JQItem = angular.element(item);
    //   JQItem.removeClass('ready-menu-second-scroll-class');
    // }
    if(window.pageYOffset >= '350'){
      var item = document.querySelector('.ready-menu-second .item:nth-child(2)');
      var JQItem = angular.element(item);
      JQItem.addClass('ready-menu-second-scroll-class');
    }
    else{
      var item = document.querySelector('.ready-menu-second .item:nth-child(2)');
      var JQItem = angular.element(item);
      JQItem.removeClass('ready-menu-second-scroll-class');
    }

    if(window.pageYOffset >= '600'){
      var item = document.querySelector('.ready-menu-second .item:nth-child(3)');
      var JQItem = angular.element(item);
      JQItem.addClass('ready-menu-second-scroll-class');
    }
    else{
      var item = document.querySelector('.ready-menu-second .item:nth-child(3)');
      var JQItem = angular.element(item);
      JQItem.removeClass('ready-menu-second-scroll-class');
    }
  })

  $scope.onInit = function(){
    $timeout(function(){
      var item = document.querySelector('.ready-menu-second .item:nth-child(1)');
      var JQItem = angular.element(item);
      JQItem.addClass('ready-menu-second-scroll-class');
    },500)
  }
  // functions

  $scope.addToCart = function(diet, dietPrice, weekCount){
    if(firebase.auth().currentUser){
      var readyDiet = diet;
      readyDiet.overallPrice =  dietPrice * weekCount;
      readyDiet.weekCount = weekCount;
      $scope.firebaseShoppingCart.push().set(readyDiet);
      var shoppingCartIconJQ = angular.element(document.querySelector('.shopping-cart-icon span'));
      shoppingCartIconJQ.addClass('animate');
      $timeout(function(){
        shoppingCartIconJQ.removeClass('animate');
      },500)
    } else{
      var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
      loginPopupJQ.css('display','flex');
      $timeout(function(){
        loginPopupJQ.css('opacity','1');
      },50)
    }
  }

  $scope.deleteDietItem = function(i){
    var newDiets = Object.assign($scope.diets);
    var dietsKeys = Object.keys(newDiets);
    delete newDiets[dietsKeys[i]];
    firebase.database().ref().child('diets').set(newDiets);
  }
}])
