app.ng.controller("App.Controller.Admin.Indicator", ["$scope", "$rootScope", "$http", "$state", "routeInfo", "entityController", "$timeout", "growl", "$uibModal", "$sce", function ($scope, $rootScope, $http, $state, routeInfo, entityController, $timeout, growl, $uibModal, $sce) {

    $scope.routeInfo = routeInfo;
    $scope.sce = $sce;
    $scope.isSubIndicator = routeInfo.parentIndicatorId;
    $scope.enums = window.enums;

    angular.extend($scope, entityController);
    $scope.entity = entityController.load(routeInfo.indicatorId, 'Indicator').then(function (entity) { 
        $scope.entity = entity;
        if (!$scope.entity.StageId) {
            $scope.entity.StageId = routeInfo.stageId;
        }
        if ($scope.isSubIndicator) {
            $scope.entity.ParentIndicatorId = routeInfo.parentIndicatorId;
        }
    });


    $scope.addAction = function () {
        if (!$scope.entity.Actions) {
            $scope.entity.Actions = [];
        }
        $scope.entity.Actions.push({ editing: true });
    }

    $scope.saveAction = function (action) {
        action.editing = false;
    }

    $scope.save = function () {
        $rootScope.$broadcast('loaderStart', {});
        var url = '/Api/Indicator/Update'
        $http.post(url, $scope.entity).success(function (result) {
            $rootScope.$broadcast('indicatorTreeChange', {});
            $rootScope.$broadcast('loaderEnd', {});
            if (!parseInt(routeInfo.indicatorId, 10)) {
                if ($scope.isSubIndicator) {
                    $state.go('Admin.SubIndicator', { parentIndicatorId: $scope.entity.ParentIndicatorId, indicatorId: result.Id });
                } else {
                    $state.go('Admin.Indicator', { indicatorId: result.Id });
                }
            }
            growl.addSuccessMessage('Indicator saved successfully');
        });
    }

    $scope.deleteIndicator = function (entity) {
        entityController.delete(entity).then(function () {
            $rootScope.$broadcast('indicatorTreeChange', {});
            if ($scope.isSubIndicator) {
                $state.go('Admin.Indicator', {stageId: entity.StageId, indicatorId: entity.ParentIndicatorId });
            } else {
                $state.go('Admin.Stage', { stageId: entity.StageId });
            }
        });
    }

    $scope.addQuestion = function () {
        $scope.entity.IndicatorQuestions.push({ editing: true, IsDeleted: false, IndicatorId: $scope.entity.Id });
    }

    $scope.saveQuestion = function (question) {

        var url = '/Api/Indicator/UpdateQuestion';
        $http.post(url, question).success(function (result) {
            question.Id = result.Id;
            question.QuestionTypeString = result.QuestionTypeString;
            question.DisplayOrder = result.DisplayOrder;
            question.editing = false;
        });

    }

    $scope.deleteQuestion = function (question) {
        if (confirm("Really delete this question?")) {
            question.IsDeleted = true;
            $scope.saveQuestion(question)
        }
    }

    $scope.moveUp = function () {
        var url = '/Api/Indicator/MoveUp';
        $http.post(url, $scope.entity).success(function (result) {
            $rootScope.$broadcast('indicatorTreeChange', {});
        });
    }

    $scope.moveDown = function () {
        var url = '/Api/Indicator/MoveDown';
        $http.post(url, $scope.entity).success(function (result) {
            $rootScope.$broadcast('indicatorTreeChange', {});
        });
    }


    $scope.moveQuestionUp = function (question) {
        var sorted = _.sortBy($scope.entity.IndicatorQuestions, 'DisplayOrder'); 
        var idx = _.findIndex(sorted, function (q) { return q.Id == question.Id });
        if (idx > 0) {
            var prev = sorted[idx - 1];
            var old = question.DisplayOrder;
            question.DisplayOrder = prev.DisplayOrder;
            prev.DisplayOrder = old;
            $scope.saveQuestion(question);
            $scope.saveQuestion(prev);
        }
    }

    $scope.moveQuestionDown = function (question) {
        var sorted = _.sortBy($scope.entity.IndicatorQuestions, 'DisplayOrder');
        var idx = _.findIndex(sorted, function (q) { return q.Id == question.Id });
        if (idx < sorted.length - 1) {
            var next = sorted[idx + 1];
            var old = question.DisplayOrder;
            question.DisplayOrder = next.DisplayOrder;
            next.DisplayOrder = old;
            $scope.saveQuestion(question);
            $scope.saveQuestion(next);
        }
    }



}]);
app.ng.controller("App.Controller.Admin.PageLoads", ["$scope", "$rootScope", "$http", "urls", function ($scope, $rootScope, $http, urls) {

   $scope.resetFilter = function () {
        $scope.filter = { SortBy: 'DateCreated', SortDescending:true, PageIndex: 1, PageSize: 50 };
        $scope.search();
    }


   
    $scope.search = function () {
        $rootScope.$broadcast('loaderStart', {});
        $http.get(urls.pageLoad.api.search(), { params: $scope.filter }).success(function (result) {
            $scope.results = result.PageLoads;
            $scope.resultCount = result.TotalCount;
            $rootScope.$broadcast('loaderEnd', {});
        });
    }
  
   
    $scope.resetFilter();

}]);
app.ng.controller("App.Controller.Admin.Stage", ["$scope", "$rootScope", "$http", "$state", "routeInfo", "entityController", "$timeout", "growl", function ($scope, $rootScope, $http, $state, routeInfo, entityController, $timeout, growl) {

    $scope.isNew = !parseInt(routeInfo.stageId, 10);
    angular.extend($scope, entityController);
    $scope.entity = entityController.load(routeInfo.stageId, 'Stage').then(function (entity) { $scope.entity = entity; });

    $scope.save = function () {
        $rootScope.$broadcast('loaderStart', {});
        var url = '/Api/Stage/Update'
        $http.post(url, $scope.entity).success(function (result) {
            $rootScope.$broadcast('indicatorTreeChange', {});
            $rootScope.$broadcast('loaderEnd', {});
            if (!parseInt(routeInfo.stageId, 10)) {
                $state.go('Admin.Stage', { stageId: result.Id });
            }
        });
    }

    $scope.delete = function () {
        if (confirm("Are you sure you want to delete this stage?")) {
            $rootScope.$broadcast('loaderStart', {});
            var url = '/Api/Stage/Delete'
            $http.post(url, $scope.entity).success(function (result) {
                $rootScope.$broadcast('indicatorTreeChange', {});
                $rootScope.$broadcast('loaderEnd', {});
                growl.addSuccessMessage('Stage deleted')
                $state.go('Admin.Stages', { stageId: result.Id });
            });
        }
    }

}]);
app.ng.controller("App.Controller.Admin.User", ["$scope", "$rootScope", "$http", "urls", "routeInfo", function ($scope, $rootScope,$http, urls, routeInfo) {

    $scope.routeInfo = routeInfo;
    $scope.urls = urls;

    $scope.load = function () {
        $rootScope.$broadcast('loaderStart', {});
        $http.get(urls.user.api.get(routeInfo.userId), {  }).success(function (result) {
            $scope.user = result;
            $rootScope.$broadcast('loaderEnd', {});
        });
    }

    $scope.load();

    $scope.getActivityUrl = function () {
        return urls.user.api.getActivityData(routeInfo.userId);
    }

    $scope.save = function () {
        $rootScope.$broadcast('loaderStart', {});
        $http.post(urls.user.api.update(), $scope.user).success(function (result) {
            $scope.user = result;
            $rootScope.$broadcast('loaderEnd', {});
        });
    }
}]);
