/**
 * Module to interface with the moderation system
 * @constructor
 */
com.ag.ReportUserSubmittedContentAbuse = function() {
	this.configObj = {};

	/**
	 * Entry point to report abuse
	 * @param configObj itemId (numeric id item to flag), typeCode (Review/Tip/Comment/ShowdownCategory), typeDesc ("review" "tip" "comment"), reportLinkDom (dom element to hide on completion)
	 */
	this.init = function(configObj) {
		this.configObj = configObj;
		if (LoginModule.getSessionUserUuid()) {
			this.launchConfirmDialog();
		} else {
			AgEvent.LOGIN.subscribeOnce(bindContext(this, this.launchConfirmDialog));
			AgEvent.REGISTER.subscribeOnce(bindContext(this, this.launchConfirmDialog));
			LoginModule.showLogin({'headerMessage':'<i>You must be logged in to access this feature.</i>'});
		}
	};

	/**
	 * Confirms user wants to submit report
	 */
	this.launchConfirmDialog = function() {
		if (LoginModule.getSessionUserUuid()) {
			var parentObject = this;
			var dialogContent = $('<div class="reportAbuse" style="height:200px;width:500px;">'+
										 '   <p style="padding:0;margin:0;">Are you sure you want to report this '+this.configObj.typeDesc+'? Abuse of this reporting tool may result in cancellation of your account.</p>'+
										 '   <div class="button_double">'+
										 '      <a id="rumSubmitButton" class="button_standard float first" href="">SUBMIT</a>'+
										 '      <a id="rumCancelButton" class="button_inactive float" href="">CANCEL</a><br class="clearFloat">'+
										 '   </div>'+
										 '</div>');
			var confirmDialog = new com.ag.ModalOverlay(dialogContent, {'headerText':'Confirm','closeCancel':'cancel'});
			$(dialogContent).find('#rumSubmitButton').click(function(event) {
				event.preventDefault();
				parentObject.sendReport();
				confirmDialog.close();
			});
			$(dialogContent).find('#rumCancelButton').click(function(event) {
				event.preventDefault();
				confirmDialog.close();
			});
			confirmDialog.show();
		} else {
			this.showMustBeLoggedIn();
		}
	};
	/**
	 * Submits report to back-end service
	 */
	this.sendReport = function() {
		var parentObj = this;
		//double-check uuid
		if (LoginModule.getSessionUserUuid()) {
			var reportObj = {'itemId':this.configObj.itemId, 'type': this.configObj.typeCode};
			$.ajax({'url': '/dynamic/rest/submittable/flag',
			        'type': 'POST',
			        'data': reportObj,
					  'dataType': 'json',
			        'success': function(data) {
				        if (data.returnString == 'AlreadyFlaggedByUser') {
							  parentObj.showAlreadySubmitted();
						  } else {
							  parentObj.showThankYou();
						  }
				        if (parentObj.configObj.reportLinkDom) $(parentObj.configObj.reportLinkDom).css({'visibility':'hidden'});
			        },
			        'error': function(data){
				        parentObj.showThankYou();
				        if (parentObj.configObj.reportLinkDom) $(parentObj.configObj.reportLinkDom).css({'visibility':'hidden'});
			        }
			});
		} else {
			this.showMustBeLoggedIn();
		}
	};
	/**
	 * Show the thank-you dialog after a user has reported abuse
	 */
	this.showThankYou = function() {
		var thankYouContent = $('<div class="reportAbuse" style="height:200px;width:500px;">'+
		                        '   <p style="padding:0;margin:0;text-align:center;">Thank you for reporting this ' +this.configObj.typeDesc+'</p>'+
		                        '   <div class="button_single">'+
                              '      <a id="rumSubmitButton" class="button_standard float first" href="">OK</a>'+
                              '   </div>'+
		                        '</div>');
		var thankYouDialog = new com.ag.ModalOverlay(thankYouContent, {'headerText':'Thank you','closeCancel':'close'});
		$(thankYouContent).find('#rumSubmitButton').click(function(event) {
			event.preventDefault();
			thankYouDialog.close();
		});
		thankYouDialog.show(8000);
	};
	/**
	 * Show a dialog stating that this report has already been submitted
	 */
	this.showAlreadySubmitted = function() {
		var alreadySubmittedContent = $('<div class="reportAbuse" style="height:200px;width:500px;">'+
		                                '   <p style="padding:0;margin:0;text-align:center;">You have already reported this ' +this.configObj.typeDesc+'</p>'+
		                                '   <div class="button_single">'+
                                      '      <a id="rumSubmitButton" class="button_standard float first" href="">OK</a>'+
                                      '   </div>'+
		                                '</div>');
		var alreadySubmittedDialog = new com.ag.ModalOverlay(alreadySubmittedContent, {'headerText':'Report already submitted','closeCancel':'close'});
		$(alreadySubmittedContent).find('#rumSubmitButton').click(function(event) {
			event.preventDefault();
			alreadySubmittedDialog.close();
		});
		alreadySubmittedDialog.show(8000);
	};
	/**
	 * Show "must be logged in" dialog in case login fails
	 */
	this.showMustBeLoggedIn = function() {
		var mustBeLoggedInContent = $('<div class="reportAbuse" style="height:200px;width:500px;">'+
		                              '   <p style="padding:0;margin:0;text-align:center;">You must be logged in to report this ' +this.configObj.typeDesc+'</p>'+
		                              '   <div class="button_single">'+
                                    '      <a id="rumSubmitButton" class="button_standard float first" href="">OK</a>'+
                                    '   </div>'+
		                              '</div>');
		var mustBeLoggedInDialog = new com.ag.ModalOverlay(mustBeLoggedInContent, {'headerText':'You must be logged in','closeCancel':'close'});
		$(mustBeLoggedInContent).find('#rumSubmitButton').click(function(event) {
			event.preventDefault();
			mustBeLoggedInDialog.close();
		});
		mustBeLoggedInDialog.show();
	}
};
