
(function(){
 
angular.module('heuerStartseite', ['ngSanitize','iz.util','apiUserAuthModule', 'heuer.core', 'heuer.analyticsTracking'])
  .controller('StartseiteController', ['$scope', function($scope){}])
  .directive('heuerNewsList',heuerNewsListDirective)
  .directive('heuerNewsAdminList',heuerNewsAdminListDirective)
  .directive('heuerNews',heuerNewsDirective)
  
/*
  .config(['$locationProvider', function($locationProvider) {  
  //$location.path();
  //$location.path('/someNewPath');
  //$location.replace();
  //$locationProvider.html5Mode({enabled:true,requireBase:false});

}]);
*/


/*
	@name heuerNewsDirective
	@desc fetches the news data (#n) with category and renders it in a dynamic template specified in one of the attributes
	@attr category TYPE enum(Top Aktuell,Nachlese,Dialog Aktuell)
	@attr template TYPE string enum( existing templates ) (e.g"heuer.news.teaser.2spalten.bigpic")
	@attr limit TYPE number (e.g "2")
*/
function heuerNewsDirective(){
  return {
	restrict: 'EA',
	scope:{
	  priority:'@',category:'@',template:'@'
	},
	link:function(scope,ele,attr){
	  scope.getTemplate = function() {
		return '/backend/resources/api/templates/name/'+scope.template+'/render';
	  }
	},
	template:'<div ng-include="getTemplate()"></div>',
	replace:true,
	controller: function($scope,$http){
	  //console.log("news-by-category-priority");
	  
	  $scope.newsData=[];

	  
	  // do we really need this function in scope?
	  $scope.getNewsData=getNewsData;
	  
	  init();
	  
	  function init(){
        if ($scope.priority && $scope.category) {
          getNewsData();
        } else {
            getTopTwoNewsByPriorityAndCategoryTopAktuell();
        }
	  }
	  
	  
	  /*
		@name getNewsData
		@desc fetches the news data (#1) with priority, the promise of $http returns response with data of type Object
	  */
	  function getNewsData(){
		var req = {
		  method: 'POST',
		  url:'/backend/resources/api/heuer/news/news-by-category-priority',
		  data:{priority:$scope.priority,category:$scope.category}
		};
		
		// the promise returns response of which data is of type object (!)
		$http(req).then(function(res){
		  $scope.newsData.push(res.data);
		},function(res){
		  console.error('ERROR OCURRED');
		});
      }
      
	  /*
		@name getTopTwoNewsByPriorityAndCategoryTopAktuell
		@desc fetches the top two news data by priority from the category "Top Aktuell", the promise of $http returns response with data of type Object
	  */
	  function getTopTwoNewsByPriorityAndCategoryTopAktuell(){
		var req = {
		  method: 'GET',
		  url:'/backend/resources/api/heuer/news/limited-news-top-two-aktuell',
		};
		
		// the promise returns response of which data is of type object (!)
		$http(req).then(function(res){
		  $scope.newsData = res.data;
		},function(){
		  console.error('ERROR OCURRED');
		});
	  }
	  
	}
  };
}

  
/*
	@name heuerNewsListDirective
	@desc fetches the news data (#n) with category and renders in dynamic template specified in one of the attributes
	@attr category TYPE enum(Top Aktuell,Nachlese,Dialog Aktuell)
	@attr template TYPE enum(existing templates) e.g "heuer.news.teaser.2spalten.bigpic_inner"
	@attr priority TYPE enum("Top1","Top2",...)
*/
function heuerNewsListDirective(){

  return {
	restrict: 'EA',
	scope:{
	  category:'@',template:'@',limit:'@'
	},
	link:function(scope,ele,attr){
	  scope.getTemplate = function() {
		return '/backend/resources/api/templates/name/'+scope.template+'/render';
	  }
	},
	template:'<div ng-include="getTemplate()"></div>',
	replace:true,
	controller:function($scope,$http,UtilServices){
	  console.log("limited-news-by-category");

	  $scope.newsData;
	  $scope.newsDataChunks = [];
	  
	  // do we really need this function in scope?
	  $scope.getNewsData=getNewsData;
	  
	  init();
	  
	  function init(){
	  	getNewsData();
	  }
	  
	  
	  /*
		@name getNewsData
		@desc fetches the news data (#n) with category, the promise of $http returns response with data of type Array
	  */
	  function getNewsData(){
		var req = {
		  method: 'POST',
		  url:'/backend/resources/api/heuer/news/limited-news-by-category',
		  data:{
			category:$scope.category,
			limit:$scope.limit
		  }
		};
		$http(req).then(function(res){
		  $scope.newsData=res.data;
		  if(angular.isArray(res.data)){
			  // we chunk the news data in order to be able to display it in rows
			  $scope.newsDataChunks = UtilServices.chunkArray(res.data,2);
		  }
		},function(res){
		  console.log('ERROR OCURRED');
		});
	  }

	}
  };

}

 /*
	@name heuerNewsAdminListDirective
	@desc fetches the news data (#n) with category and renders for slider-test
	@attr category TYPE enum(Top Aktuell,Nachlese,Dialog Aktuell)
	@attr template TYPE enum(existing templates) e.g "heuer.news.teaser.2spalten.bigpic_inner"
	@attr priority TYPE enum("Top1","Top2",...)
*/
function heuerNewsAdminListDirective(){

  return {
	restrict: 'EA',
	scope:{
	  category:'@',template:'@',limit:'@'
	},
	link:function(scope,ele,attr){
	  scope.getTemplate = function() {
		return '/backend/resources/api/templates/name/'+scope.template+'/render';
	  }
	},
	template:'<div ng-include="getTemplate()"></div>',
	replace:true,
	controller:function($scope,$http,UtilServices){
	  console.log("limited-news-by-category");

	  $scope.newsData;
	  $scope.newsDataChunks = [];
	  
	  // do we really need this function in scope?
	  $scope.getNewsAdminData=getNewsAdminData;
	  
	  init();
	  
	  function init(){
	  	getNewsAdminData();
	  }
	  
	  
	  /*
		@name getNewsData
		@desc fetches the news data (#n) with category, the promise of $http returns response with data of type Array
	  */
	  function getNewsAdminData(){
		var req = {
		  method: 'POST',
		  url:'/backend/resources/api/heuer/news/limited-news-by-category-admin',
		  data:{
			category:$scope.category,
			limit:$scope.limit
		  }
		};
		$http(req).then(function(res){
		  $scope.newsData=res.data;
		  if(angular.isArray(res.data)){
			  // we chunk the news data in order to be able to display it in rows
			  $scope.newsDataChunks = UtilServices.chunkArray(res.data,2);
		  }
		},function(res){
		  console.log('ERROR OCURRED');
		});
	  }

	}
  };

}
 
  
}());
