angular.module('stoute.ng-user', ['stoute.ng-baseApp'])

    .controller('UserCtrl',function($scope,$rootScope,$translate,state,AppSettings) {
        console.log('UserCtrl');

        $scope.config = AppSettings;
        $scope.state = state.state;

        if(!AppSettings.loggedIn) {
            $scope.state = 'login';
        }else{
            $scope.state = 'profile';
        }
        $scope.$on('user-state-change', function(event,authData) {
            console.log('user-state-change');
            if(authData===null){
                $scope.state = 'login';
            }else{
                $scope.state = 'profile';
                $scope.hideModal();
            }
        });
        $scope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
        });

        $scope.$watch('state', function () {
            console.log('state='+$scope.state);
            if($scope.state==='register'){

            }
        });

        $scope.hideModal = function(){
            // fixme: move to directive
            angular.element('#userModal').modal('hide');
        };
        $scope.saveDb = function(){
            AppSettings.saveDb();
        };
        $scope.loginToggle = function(){
            AppSettings.user_email = $scope.email;
            AppSettings.user_password = $scope.password;
            $rootScope.loginToggle().then(function(userObj){
                //if(userObj) {
                //    $scope.state = 'profile';
                //}else{
                //    $scope.state = 'login';
                //}
            });
        };
        $scope.register = function(){
            $rootScope.register({email:$scope.email,password:$scope.password}).then(function(){
                $scope.state = 'profile';
            });
        };
    })

    .directive('user', ['$compile','AppSettings', function($compile,AppSettings) {
        return {
            restrict: 'E',
            link: function(scope,element,attrs) {

            },
            templateUrl: 'app/components/base-app/ng-user/ng-user.html',
            controller: "UserCtrl",
            scope:{
                collection:'=',
                fields:'@',
                onSave:'=',
                onDelete:'=',
                onCancel:'='
            },
            transClude:true
        };
    }])
    .factory('state', function () {
        var state = {};
        return {
            state: state,
        };
    })
    .config(function($stateProvider) {
        var resolve = {
            app: ['$rootScope',function ($rootScope) {
                console.log($stateProvider.state.get());
                return $rootScope.bootResolve();
            }]
        };
        $stateProvider
            .state('user', {
                url: '/user/'
            })
            .state('user.login', {
                url: '/user/login/',
                views: {
                    'user': {
                        controller: 'UserCtrl',
                        templateUrl: 'ng-user/ng-user.html'
                        //template: '<h2>TESTING LOGIN</h2>'
                    }
                },
                resolve: resolve.app
            })
            .state('user.logout', {
                url: '/user/logout/',
                views: {
                    'user': {
                        controller: 'UserCtrl',
                        templateUrl: 'ng-user/ng-user.html'
                        //template: '<h2>TESTING LOGOUT</h2>'
                    }
                },
                resolve: resolve.app
            });
    })

;