;(function(undefined) {

	/**
	 *
	 * @param $elem
	 * @param jackpot
	 * @constructor
	 */
	function JackpotWinnersList($elem, jackpot) {
		var self = this

		this.$container = $elem
		this.jackpot = jackpot

		this.pagination = {
			page: 1,
			direction: 'desc',
			sortBy: 'date',
			separate: 1,
			pagesCount: 1
		}

		this.template = $templateFactory.getById('jackpot-winners-list')
		this.cache = {}

		$(document).on('click', '.js-show-page', function() {
			var page = +$(this).data('page')
			self.hasPage(page) && self.show(page)

			return false
		})
			.on('click', '.js-sort', function(){
				var field = $(this).data('field'),
					direction = $(this).data('dir');
				self.sort(field, direction);
				return false;
			})
	}

	function getOppositeDirection(dir) {
		if(dir == 'asc') {
			return 'desc';
		}
		return 'asc';
	};

	JackpotWinnersList.prototype = {
		fetch: function() {

			if(this.cache[this.pagination.page]) {
				return $.Deferred().resolve(this.cache[this.pagination.page]).promise()
			}

			return this.request()
		},
		request: function() {
			var self = this,
					data = {
						type: 'jackpots',
						jackpot: this.jackpot,
						page: this.pagination.page,
						sort_direction: this.pagination.direction,
						sort: this.pagination.sortBy,
						separate: this.pagination.separate
					},
					deferred = $.Deferred()

			$.ajax('/actions/winner_list_builder?' + $.param(data), {
				dataType: 'json',
				context: this,
				success: function(response) {
					if(response.error) {
						deferred.reject(response)
					} else {
						//this.cache[self.pagination.page] = response
						deferred.resolve(response)
					}
				}
			})

			return deferred.promise()
		},
		output: function(data) {
			var data = $.extend({ list: this }, data),
				html = _.template(this.template)(data);

			this.$container.html(html)
		},
		show: function(page) {
			page || (page = 1)

			var self = this;

			this.pagination.page = page;

			this.fetch()
					.done(function(data) {
						self.pagination.pagesCount = data.count
						self.output(data)
					})
					.fail(function(data) {

					})
		},
		sort: function (field,direction) {
			var self = this;
			this.pagination.sortBy = field;
			this.pagination.direction = direction;
			this.fetch()
				.done(function(data) {
					self.pagination.pagesCount = data.count
					self.output(data)
				})
				.fail(function(data) {

				})
		},

		// helper functions
		getPages: function() {
			return _.range(1, this.pagination.pagesCount + 1)
		},
		prev: function() {
			this.show(this.pagination.page - 1)
		},
		next: function() {
			this.show(this.pagination.page + 1)
		},
		hasPage: function(page) {
			return page >= 1 && page <= this.pagination.pagesCount
		},
		hasPrev: function() {
			return this.pagination.page != 1
		},
		hasNext: function() {
			return this.pagination.page != this.pagination.pagesCount
		},
		getDirection: function(field, opposite) {
			if (this.isSorted(field)) {
				if (opposite) {
					return getOppositeDirection(this.pagination.direction)
				} else {
					return this.pagination.direction
				}
			}
			return 'asc'
		},
		isSorted: function (field){
			return this.pagination.sortBy === field
		}
	}

	window.JackpotWinnersList = JackpotWinnersList
})()