/**
 * Login view module controller.
 * 
 * @author imoiseyenko93@gmail.com
 */
define(["angular", "app/view/login/login"], function (angular, loginModule) {

	loginModule.controller("loginController", ["$scope", "$rootScope", "$state", "accountService",
			function ($scope, $rootScope, $state, accountService) {

				/** Errors directive model */
				$scope.errors = [];

				var successHandler = function (data) {

					console.log(data);
					$rootScope.tokenData = data.tokenData;
					$rootScope.profile = data.profileVO;
					$rootScope.email = $scope.model.email;
					$state.go("app.main.profile.overview", {
						email: $scope.model.email
					});
				};

				var errorHandler = function (error) {

					console.log(error);
					$scope.errors.push(error.data.error);
				};

				$scope.model = {

					email: "",
					password: "",

					login: function () {

						accountService.login($scope.model.email, $scope.model.password, successHandler, errorHandler);
					}
				};
			}]);
});
