"use strict";

angular.module("wuzQuizFrontendApp.controllers").controller("NewUserCtrl", function($scope, $stateParams, Session, $location, $rootScope, User) {

	$scope.submitData = function() {
		if ($scope.agreedToPrivacyPolicy) {
			console.log(username);
			var username = $scope.user;
			var email = $scope.email;
			var newsletterSubscribed = $scope.newsletter;
			var newUser = new User();
			newUser.username = username;
			newUser.email = email;
			newUser.newsletterSubscribed = true;
			newUser.$save().then(function(response) {
				Session.newIdentity();
				Session.userId = response.id;
				Session.getGamedata().then(function(gamedata) {
					$location.path("/quiz/" + gamedata.quizzes[0].id + "/detail")
				});
			}).catch(function(errorData) {
				console.log(errorData);
			});

			// Generate a new identity (flushes all local storage values)
			//Session.newIdentity();
			//console.log(Session.addUser(username, email, newsletterSubscribed));
			/*Session.addUser(username, email, newsletterSubscribed).then(function(userId) {
				console.log("Then ausgeführt.");

				/*Session.init(username).then(function() {
					$rootScope.user_id = Session.getSession().user_id;
					$location.path('/quiz/1/detail'); //Hardcoded


				});*/

			/*}).catch(function(error) {
				console.log(error);
			});*/
		}

	}
});