///<reference path=".#ts/client.d.ts" />

interface Templates {
	lotteryHeader: Template;
}

Template.lotteryHeader.helpers({
	counterTime: function(){
		var diff = moment((<Lotteries.LotteryInstance>this).drawDate).diff(moment(), 'seconds');
		if(diff <= 0) return;
		counterApi.dep.depend();
		var days 	= Math.floor(diff / 86400);
		var hours 	= Math.floor((diff - days * 86400) / 3600);
		var minutes = Math.floor((diff - days * 86400 - hours * 3600) / 60);
		var seconds = Math.floor((diff - days * 86400 - hours * 3600 - minutes * 60));

		if(days>0) return days + ' Days ' + ([1e2]+''+hours).slice(-2) + ':' + ([1e2]+''+minutes).slice(-2) + ':' + ([1e2]+''+seconds).slice(-2);
		if(hours>0) return hours + ' Hours ' + ([1e2]+''+minutes).slice(-2) + ':' + ([1e2]+''+seconds).slice(-2);
		if(minutes>0) return minutes + ' Minutes ' + ([1e2]+''+seconds).slice(-2);
		return seconds + ' seconds';
	},
	showEventDescription: function(){
		counterApi.dep.depend();
		return !(<Lotteries.LotteryInstance>this).drawn && moment((<Lotteries.LotteryInstance>this).drawDate).diff(moment(), 'seconds') <= 0;
	},

	usableTickets: function(){
		return Lotteries.Ticket.db.find({lottery: {$exists: false}}).count();
	},
	showHistory: function(){
		return Lotteries.LotteryInstance.db.find({active: false}).count() > 0;
	},
	totalTickets: function(){
		var self = <Lotteries.LotteryInstance>this;
		Meteor.call('lotteries-admin/Tickets/total', {lotteryInstance: self._id}, function (err, res) {
			Session.set('totalTickets'+self._id, res);
		});
		return Session.get('totalTickets'+self._id);
	},
	ticketsOwned: function(){
		return Lotteries.Ticket.db.find({lotteryInstance: (<Lotteries.LotteryInstance>this)._id}).count();
	},

	totalPrizePool: function(){
		var totalPrize = 0;
		var prefix = ' m';
		if((<Lotteries.LotteryInstance>this).prizePoolType == Lotteries.PrizePoolType.FIXED) {
			totalPrize = (<Lotteries.LotteryInstance>this).prizePool;
		}else{
			(<Lotteries.LotteryInstance>this).prize.forEach(function (p) {
				//if (p.type == Lotteries.PrizeType.MONETARY)
				totalPrize += p.amount;
			});
		}
		if(totalPrize>=100000 && totalPrize<100000000 ) {
			totalPrize = Math.round(totalPrize / 100) /10;
			prefix = ' '
		}
		else if(totalPrize>=100000000) {
			totalPrize = Math.round(totalPrize / 100000) /10;
			prefix = ' K'
		}
		return totalPrize.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + prefix;
	},
});

Template.lotteryHeader.events({

	'click .buyTicket': function(){

		Session.set('opened-modal', 'buyTicketModal');
		Modals.open('buyTicketModal', this);
	},
	'click .useTicket': function(){

		Session.set('opened-modal', 'useTicketModal');
		Modals.open('useTicketModal', this);
	},
	'click .fillTicket': function(){

		Session.set('opened-modal', 'fillTicketModal');
		Modals.open('fillTicketModal', this.tickets.fetch({numbers: {$exists: false}})[0]);
	},
	'click .ticketsOwned': function(){
		$('html, body').animate({
			scrollTop: $('#tickets').offset().top - 110
		}, 300);
		//window.scrollTo(0, $('#tickets').offset().top - 110);
	}
});

Template.lotteryHeader.created = function () {
	Meteor.setInterval(function(){
		counterApi.dep.changed();
	}, 1000);

	this.autorun(() => {
		var handle = Session.get('lotteryHandle');
		if(handle && !Session.get('lotteryInstance')) {
			if (Users.Profile.active()) {
				var lotteryInstance = Lotteries.LotteryInstance.db.findOne();
				if (lotteryInstance) {
					var tSelector = {
						lotteryInstance: lotteryInstance._id
					};
					var tOptions = {
						sort: {'acquired': -1},
					};
					this.subscribe('Lotteries.Ticket', tSelector, tOptions);
				}
				this.subscribe('Lotteries.Ticket', {lotteryInstance: {$exists: false}});
			}
		}

	})
};

