//The main class used for handling all js elements on the play page
var PortalViewController = Class.create({
	initialize: function() {
		this.VoteController = new PortalVoteController();
		this.FavoritesController = new PortalFavoritesController();
		this.CommentsController = new PortalCommentsController();
		
		this.busy = false;
	},
	
	GetGameId: function() {
		return $('submission_id').value;
	}
});

//Handles the voting
var PortalVoteController = Class.create(PortalViewController, {
	initialize: function() {
		this.AJAX_SUBMIT_PAGE = '/votes/add';
		this.AJAX_REFRESH_PAGE = '/votes/view';
		this.AJAX_REFRESH_ID = 'vote-widget';
		
		this.busy = false;
	},
	
	GetGameId: function($super) {
		return $super();
	},
	
	Save: function(voteScore) {
		if(!NumberIsBetween(voteScore, 1, 5)){
			return;
		}
		
		if(!CurrentUser.SignedIn) {
			alert('You must be signed in to do that.');
			VoteWidget.busy = false;
			VoteWidget.DimStars();
			return;
		}
		
		var params = new Array();
		params['data[Vote][game_id]'] = this.GetGameId();
		params['data[Vote][value]'] = voteScore;
		
		this.busy = true;
		
		new Ajax.Request(this.AJAX_SUBMIT_PAGE,
		{
			method:'post',
			parameters: params,
			onSuccess: this.HandleResponse.bind(this)
		});
	},
	
	HandleResponse: function(transport) {
		//Let them be able to try again if there were errors
		this.busy = false;
		
		var response = transport.responseText.evalJSON();
		
		if(!response.signedIn){
			alert('You must be signed in to do that.');
			VoteWidget.busy = false;
			VoteWidget.DimStars();
			return;
		}
		
		if(response.hasVoted){
			alert('You have already voted on this submission.');
			VoteWidget.busy = false;
			VoteWidget.DimStars();
			
			// Let's refresh the vote display
			this.Refresh();
			return;
		}
		
		this.Refresh();
	},
	
	Refresh: function(){
		new Ajax.Updater(this.AJAX_REFRESH_ID, this.AJAX_REFRESH_PAGE+'/'+this.GetGameId());
	}
});

var PortalFavoritesController = Class.create(PortalViewController, {
	initialize: function() {
		this.AJAX_SUBMIT_PAGE = '/favorites';
		this.ADD_FAVORITE_HTML = '<a href="javascript:Submission.FavoritesController.Add();">Add to Favorites</a>';
		this.REMOVE_FAVORITE_HTML = '<a href="javascript:Submission.FavoritesController.Remove();">Remove from Favorites</a>';
		
		this.Action = '';
		
		this.busy = false;
	},
	
	GetGameId: function($super) {
		return $super();
	},
	
	Add: function() {
		if(this.busy){
			return;
		}
		
		this.Action = 'add';
		this.MakeRequest();
	},
	
	Remove: function() {
		if(this.busy){
			return;
		}
		
		this.Action = 'remove';
		this.MakeRequest();
	},
	
	MakeRequest: function() {
		if(this.busy){
			return;
		}
		
		if(!CurrentUser.SignedIn) {
			alert('You must be signed in to do that.');
			$('control-favorite').innerHTML = this.ADD_FAVORITE_HTML;
			return;
		}
		
		if (this.Action == 'add'){
			$('control-favorite').innerHTML = this.REMOVE_FAVORITE_HTML;
		} else if(this.Action == 'remove') {
			$('control-favorite').innerHTML = this.ADD_FAVORITE_HTML;
		} else {
			return;
		}
		
		var params = new Array();
		params['data[Favorite][game_id]'] = this.GetGameId();
		
		this.busy = true;
		
		new Ajax.Request(this.AJAX_SUBMIT_PAGE+'/'+this.Action,
		{
			method:'post',
			parameters: params,
			onSuccess: this.HandleResponse.bind(this)
		});
	},
	
	HandleResponse: function(transport) {
		this.busy = false;
	}
});

//Comments
var PortalCommentsController = Class.create(PortalViewController, {
	initialize: function() {
		this.AJAX_SUBMIT_PAGE = '/comments/add';
		this.AJAX_REFRESH_PAGE = '/comments/view';
		this.AJAX_DELETE_PAGE = '/comments/delete';
		this.AJAX_REPORT_PAGE = '/comments/report';
		this.AJAX_REFRESH_ID = 'comment-area';
		
		this.Page = 1;
		
		//We only want one refresh and only one save request at a time
		this.RefreshBusy = false;
		this.SaveBusy = false;
		this.DeleteBusy = false;
		this.reportBusy = false;
	},
	
	GetGameId: function($super) {
		return $super();
	},
	
	ShowNext: function() {
		if(this.RefreshBusy){
			return;
		}
		
		this.Page ++;
		this.Refresh();
	},
	
	ShowPrevious: function() {
		if(this.RefreshBusy){
			return;
		}
		
		this.Page --;
		this.Refresh();
	},
	
	Refresh: function() {
		if(this.RefreshBusy){
			return;
		}
		
		var params = new Array();
		params['data[Comment][game_id]'] = this.GetGameId();

		this.RefreshBusy = true;
		
		//We only need an onSuccess handler so we can set its RefreshBusy propery
		new Ajax.Updater(this.AJAX_REFRESH_ID, this.AJAX_REFRESH_PAGE+'/'+this.GetGameId()+'/page:'+this.Page, 
		{
			method:'post',
			parameters: params,
			onSuccess: this.HandleRefresh.bind(this)
		});
	},
	
	HandleRefresh: function(transport) {
		this.RefreshBusy = false;
	},
	
	SaveComment: function() {
		//No simultaneous requests
		if(this.SaveBusy){
			alert('Please wait until all file activity on the page finishes.');
			return;	
		}
		
		var comment = Trim($F('comment'));
		
		if(!NumberIsBetween(comment.length, 10, 4000)){
			alert('Please keep your comment between 10 and 4000 characters.');
			return;
		}
		
		$('save-comment-button').disabled = 'disabled';
		
		var params = new Array();
		params['data[Comment][game_id]'] = this.GetGameId();
		params['data[Comment][value]'] = comment;
		params['data[Comment][user_id]'] = CurrentUser.UserId;
		
		this.SaveBusy = true;
		
		new Ajax.Request(this.AJAX_SUBMIT_PAGE,
		{
			method:'post',
			parameters: params,
			onSuccess: this.HandleResponse.bind(this)
		});
	},
	
	HandleResponse: function(transport) {
		// They are allowed to comment more than once
		this.SaveBusy = false;
		$('save-comment-button').disabled = null;
		$('comment').value = '';

		var response = transport.responseText.evalJSON();
		
		if(response.commentStatus == 'pending') {
			alert('Our comment moderation system thinks that this comment might be spam.  Your comment will need to be manually processed.');
			return;
		} else if(response.commentStatus == 'spam') {
			alert('Our comment moderation system thinks that this comment is spam.  This comment will not be approved.');
			return;
		}
		
		this.Refresh();
	},
	
	ReportAbuse: function(commentId) {
		//No simultaneous requests
		if(this.reportBusy){
			return;	
		}
		
		$('report-abuse-'+commentId).innerHTML = '<span>Reported!</span>';
		$('report-abuse-'+commentId).href = 'javascript:return(false);';
		
		var params = Array();
		params['data[AbuseReport][comment_id]'] = commentId;
		
		this.reportBusy = true;
		
		new Ajax.Request(this.AJAX_REPORT_PAGE,
		{
			method:'post',
			parameters: params,
			onSuccess: function(transport) {
				this.reportBusy = false;
			}.bind(this)
		});
	},
	
	DeleteComment: function(commentId) {
		var params = Array();
		params['commentId'] = commentId;
		
		this.DeleteBusy = true;
		
		new Ajax.Request(this.AJAX_DELETE_PAGE,
		{
			method:'post',
			parameters: params,
			onSuccess: this.HandleDelete.bind(this)
		});
	},
	
	HandleDelete: function(transport) {
		this.DeleteBusy = false;
		this.Refresh();
	}
});
