app.controller('AppRootController',['$scope','$window','$timeout','$location','$firebaseAuth','$interval','$firebaseObject',function($scope,$window,$timeout,$location,$firebaseAuth,$interval,$firebaseObject){
  var window = angular.element($window);
  $scope.load = function(){
    $timeout(function(){
      var preloader = angular.element(document.querySelector('.preload-screen'));
      preloader.css('opacity','0');
    },1700)
    $timeout(function(){
      var preloader = angular.element(document.querySelector('.preload-screen'));
      preloader.css('display','none');
    },2700)
  }

  $scope.isOpenMenu = false;
  $scope.toggleMenu = function(){
    if($scope.isOpenMenu){
      $scope.isOpenMenu = false;
    }else {
      $scope.isOpenMenu = true;
    }
  }

  $scope.selectedNumber = 0;
  $scope.meals = {};
  if(firebase.auth().currentUser){
    $scope.ref = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('meals');
  } else{
    $scope.ref = firebase.database().ref().child('meals');
  }
  $interval(function(){
    if(firebase.auth().currentUser){
      $scope.ref = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('meals');
      $scope.isUserSigned = true;
    } else{
      $scope.ref = firebase.database().ref().child('meals');
      $scope.isUserSigned = false;
    }
    $scope.ref.on('value', snap => {
      $timeout(function(){
        var selectedNumber = 0;
        angular.forEach(snap.val(),function(el){
          if(el.isSelected === true){
            selectedNumber++;
          }
        })
        $scope.selectedNumber = selectedNumber;
      })
      $scope.meals = snap.val();
    })
  },1000)
  // var ref = firebase.database().ref().child('meals');
  $scope.selectedNumber = 0;
  $scope.ref.on('value', snap => {
    $timeout(function(){
      var selectedNumber = 0;
      angular.forEach(snap.val(),function(el){
        if(el.isSelected === true){
          selectedNumber++;
        }
      })
      $scope.selectedNumber = selectedNumber;
    })
    $scope.meals = snap.val();
  })
  $scope.openSelectedMeals = function(){
    if(firebase.auth().currentUser){
      var selectedMeals = angular.element(document.querySelector('.selected-meals'));
      var appRoot = angular.element(document.querySelector('.app-root'));
      var overlay2 = angular.element(document.querySelector('.overlay2'));
      // selectedMeals.css({'transform': 'translateX(0)'})
      appRoot.css({'transform': 'translateX(-281px)'});
      overlay2.css({'pointer-events': 'auto','opacity':'1'});
      overlay2.on('click',function(){
        overlay2.css({'pointer-events': 'none','opacity':'0'});
        appRoot.css({'transform': 'translateX(0px)'});
      })
    } else{
      var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
      loginPopupJQ.css('display','flex');
      $timeout(function(){
        loginPopupJQ.css('opacity','1');
      },50)
    }
  }

  $scope.isShowMealsBtn = function(){
    if($location.path() == '/select-menu'){
      return true;
    } else{
      return false;
    }
  }


    if(firebase.auth().currentUser){
      $scope.isUserSigned = true;
    } else{
      $scope.isUserSigned = false;
    }


  $scope.signOut = function(){
    firebase.auth().signOut().then(function(success){
      console.log('Sign out seccessfully: ',success);
      if($location.path().indexOf('shopping-cart') > -1 || $location.path().indexOf('add-item') > -1 || $location.path().indexOf('user-page') > -1 || $location.path().indexOf('messages') > -1){
        $window.location.assign('/');
      }
    }).catch(function(error){
      console.log('Sign out error: ',error);
    })
  }

  // login-popup

  $scope.isLoginActive = true;
  $scope.confirmPasswordIncorrect = false;

  $scope.newUserObj = {};
  $scope.newUserDataObj = {};
  $scope.registerObject = {};

  $scope.createNewUser = function(){
    $scope.confirmPasswordIncorrect = false;
    firebase.auth().createUserWithEmailAndPassword($scope.newUserObj.email, $scope.newUserObj.password).then(function(success){
      firebase.database().ref().child('users').child(success.uid).child('meals').set($scope.meals);
      firebase.database().ref().child('users').child(success.uid).child('userObject').set($scope.newUserDataObj);

      var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
      loginPopupJQ.css('opacity','0');
      $timeout(function(){
        loginPopupJQ.css('display','none');
      },1000)
    }).catch(function(error){
      // $scope.emailErrorMessage = error.code.replace(/auth\//g,'');
      $scope.passwordErrorMessage = error.message;
    });
  }

  $scope.signInUserObj = {};
  $scope.signInUser = function(){
    firebase.auth().signInWithEmailAndPassword($scope.signInUserObj.email,$scope.signInUserObj.password).then(function(success){
      console.log('Signed in successfully: ',success);

      var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
      loginPopupJQ.css('opacity','0');
      $timeout(function(){
        loginPopupJQ.css('display','none');
      },1000)

    }).catch(function(error){
      console.log('Sign in error: ',error.message);
    })
  }

  $scope.onPasswordInput = function(){
    if($scope.newUserObj.password){
      $scope.isShortPassword = $scope.newUserObj.password.length < 6 ? true : false;
    }
  }

  $scope.onConfirmPasswordInput = function(){
    if($scope.registerObject.confirmPassword){
      $scope.confirmPasswordIncorrect = $scope.registerObject.confirmPassword == $scope.newUserObj.password ? false : true;
    }
  }

  $scope.cancelPopup = function(){
    var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
    loginPopupJQ.css('opacity','0');
    $timeout(function(){
      loginPopupJQ.css('display','none');
    },1000)
  }

  $scope.openPopup = function(){
    var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
    loginPopupJQ.css('display','flex');
    $timeout(function(){
      loginPopupJQ.css('opacity','1');
    },50)
  }

  console.log('current user: ',firebase.auth().currentUser ? firebase.auth().currentUser.uid : null)
  $scope.goToShoppingCart = function(){
    if(firebase.auth().currentUser){
      $window.location.assign('#!/shopping-cart');
    } else{
      var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
      loginPopupJQ.css('display','flex');
      $timeout(function(){
        loginPopupJQ.css('opacity','1');
      },50)
    }
  }

  $scope.isDeleteIconsShow = false;

  $scope.deleteItemModeToggle = function(){
    var selectMenuItemJQ = angular.element(document.querySelectorAll('.select-menu-page .meal-item-delete-btn'));
    var selectReadyMenuItemJQ = angular.element(document.querySelectorAll('.diet-item-delete-btn'));
    if($scope.isDeleteIconsShow){
      selectMenuItemJQ.css('opacity','0');
      selectReadyMenuItemJQ.css('opacity','0');
      $scope.isDeleteIconsShow = false;
    } else{
      selectMenuItemJQ.css('opacity','1');
      selectReadyMenuItemJQ.css('opacity','1');
      $scope.isDeleteIconsShow = true;
    }
  }

  $scope.isShowDeleteItemModeToggle = function(){
    if($scope.isUserSigned && (firebase.auth().currentUser.uid == 'uRkqD9GmNxUsdgF1XtmPicDJwAz1') && (($location.path().indexOf('select-menu') > -1)||($location.path().indexOf('ready-menu-') > -1))){
      return true;
    } else{
      return false;
    }
  }


}])
