WebModule.controller('AuctionsController', ['$scope', 'WMService', 'LotService', 'FilterService', '$location', '$filter', 'Auction', function($scope, WMService, LotService,FilterService, $location, $filter, Auction) {
	$scope.WMService = WMService;
	$scope.LotService = LotService;
	$scope.FilterService = FilterService;
	$scope.auctionsRange = viewVars.auctionsRange;
	$scope.auctions = viewVars.auctions;
	$scope.Auction = Auction;
	$scope.subtitle = function(){
		var numLots = 0;
		for (var i=0;i<$scope.auctions.result_page.length;i++){
			if ($scope.auctionIsVisible($scope.auctions.result_page[i])){
				numLots += 1;
			}
		}
		var label = numLots + ' Auction';
		if (numLots != 1){
			label += 's';
		}
		return label;
	};
	$scope.refreshAuctionTimer = function(auction){
		if(!$scope.$$phase){	
			$scope.$apply();
		}
	}
	$scope.isShowingUpcomingBidsOnly = function(){
		return $scope.auctionsRange == 'my-upcoming-bids';
	};
	$scope.isShowingPastBidsOnly = function(){
		return $scope.auctionsRange == 'my-past-bids';
	};
	$scope.isShowingPastOnly = function(){
		return $scope.auctionsRange == 'my-past-bids' || $scope.auctionsRange == 'my-past-watched';
	};
	$scope.auctionIsVisible = function(auction){
		return 	true;
	};
	$scope.isShowingWatched = function(auction){
		return $scope.auctionsRange == 'my-upcoming-watched' || $scope.auctionsRange == 'my-past-watched';
	}
	$scope.isShowingBids = function(auction){
		return $scope.auctionsRange == 'my-upcoming-bids' || $scope.auctionsRange == 'my-past-bids';
	}
	$scope.numBidLots = function(){
		var count = 0;
		for (var i=0;i<$scope.auctions.result_page.length;i++){
			count += WMService.auctionBidsLotCount($scope.auctions.result_page[i]);
		}
		return count;
	};
	$scope.numPastBidLots = function(){
		return $scope.numBidLots();
	}
	$scope.numPastBidAuctions = function(){
		return $scope.numBidAuctions();
	}
	$scope.numBidAuctions = function(){
		var count = 0;
		for (var i=0;i<$scope.auctions.result_page.length;i++){
			if (WMService.auctionBidsLotCount($scope.auctions.result_page[i])){
				count += 1;
			}
		}
		return count;
	}
	$scope.viewLots = function(auctionId){
		window.location.href = $scope.WMService.endpoints.auctionLots + auctionId;	
	};
	$scope.title = function(){
		var numLots = 0;
		if(viewVars.currentRouteName == 'my-past-bids'){
			for (var i=0;i<$scope.auctions.result_page.length;i++){
				if ($scope.auctionIsVisible($scope.auctions.result_page[i])){
					numLots += 1;
				}
			}
		}else{
			numLots = $scope.auctions.query_info.total_num_results;
		}
		

		if ('upcoming' == $scope.auctionsRange){
			return 'Upcoming Auctions';
		}
		else if ('past' == $scope.auctionsRange){
			var label = numLots + ' Past Auction';
			if(numLots != 1)
				label = label + 's';
			return label;
		}
		else if ($scope.isShowingPastBidsOnly() || $scope.isShowingUpcomingBidsOnly()){
			//var label = numLots + ' ';
			var label = "My ";
			if ($scope.isShowingPastOnly()){
				label += 'Past';
			}
			else {
				label += 'Upcoming';
			}
			label += ' Bid';
			//if(numLots != 1)
				label = label + 's';
			return label;
		}
	};
	$scope.pluralize = function(value){
		if(value != 1)
			return true;
		return false;
	}
	$scope.advancedTitle = function(){

		var numAuctions = 0;
		for (var i=0;i<$scope.auctions.result_page.length;i++){
			if ($scope.auctionIsVisible($scope.auctions.result_page[i])){
				numAuctions += 1;
			}
		}
		var numBidLots = $scope.numBidLots();
		//todo change this after we have the server support
		var numWatchedLots = '###';

		var label = "";
		if($scope.isShowingBids()){
			//bids
			/*var activeText = " Active";
			var pastText = " Past";*/
			if($scope.isShowingPastOnly()){
				//activeText = '';
				label = $filter('translate')('BIDSINPASTAUCTIONCOUNT',"{BIDCOUNT:"+numBidLots+", AUCTIONCOUNT:"+numAuctions+"}");
			}else{
				//pastText = '';
				label = $filter('translate')('BIDSINAUCTIONCOUNT',"{BIDCOUNT:"+numBidLots+", AUCTIONCOUNT:"+numAuctions+"}");
			}
			/*label = numBidLots + activeText + ' Bid';
			if($scope.pluralize(numBidLots)) label = label + 's';
			label = label + " in " + numAuctions + pastText + ' Auction';
			if($scope.pluralize(numAuctions)) label = label + 's';*/
			
			//label = $filter('translate')('BIDSINAUCTIONCOUNT',"{BIDCOUNT:"+numBidLots+", AUCTIONCOUNT:"+numAuctions+"}");
			console.log(label)
			
		}else if($scope.isShowingWatched()){
			//watched auctions
			var pastText = " Past";
			if(!$scope.isShowingPastOnly()){
				pastText = '';
			}
			label = numWatchedLots + ' Watched Lot'
			if($scope.pluralize(numWatchedLots)) label = label + 's';
			label = label + " in " + numAuctions + pastText  + ' Auction';
			if($scope.pluralize(numAuctions)) label = label + 's';
		}
		return label;
	};
	$scope.breadcrumb = function(){
		return {title: $scope.title(), url: window.location.href};
	};

	$scope.FilterService.init($scope.auctions.query_info, 'auctions');
}]);