<?xml version="1.0"?>

<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<?xml-stylesheet href="chrome://zotero/skin/errorReport.css" type="text/css"?>

<!DOCTYPE window SYSTEM "chrome://zotero/locale/zotero.dtd">

<wizard xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
	id="zotero-error-report" title="&zotero.errorReport.title;"
	width="550" height="450">
	
	<script>
	<![CDATA[
		Components.utils.import("resource://zotero/config.js");
		
		var Zotero_Error_Report = new function() {
			this.init = init;
			this.sendErrorReport = sendErrorReport;
			
			var obj = window.arguments[0].wrappedJSObject;
			var Zotero = obj.Zotero;
			var data = obj.data;
			var msg = data.msg;
			var errorData = data.errorData;
			var extraData = data.extraData ? data.extraData : '';
			var diagnosticInfo = false;
			
			
			function init() {
				var wizard = document.getElementById('zotero-error-report');
				var continueButton = wizard.getButton('next');
				continueButton.disabled = true;
				
				Zotero.getSystemInfo(function(info) {
					var errorDataText = errorData.length
						? data.errorData.join('\n')
						: Zotero.getString('errorReport.noErrorsLogged', Zotero.appName);
					
					diagnosticInfo = info;
					
					var logText = errorDataText + '\n\n'
						+ (extraData !== '' ? extraData + '\n\n' : '')
						+ diagnosticInfo;
					
					if (document.getElementById('zotero-failure-message').hasChildNodes()) {
						var textNode = document.getElementById('zotero-failure-message').firstChild;
						document.getElementById('zotero-failure-message').removeChild(textNode);
					}
					
					document.getElementById('zotero-failure-message').appendChild(document.createTextNode(msg));
					document.getElementById('zotero-error-message').value = logText;
					
					continueButton.disabled = false;
					continueButton.focus();
					var str = Zotero.getString(
						'errorReport.advanceMessage', continueButton.getAttribute('label')
					);
					document.getElementById('zotero-advance-message').setAttribute('value', str);
				});
			}
			
			function sendErrorReport() {
				var wizard = document.getElementById('zotero-error-report');
				var continueButton = wizard.getButton('next');
				continueButton.disabled = true;
				
				var parts = {
					error: "true",
					errorData: errorData.join('\n'),
					extraData: extraData,
					diagnostic: diagnosticInfo
				};
				
				var body = '';
				for (var key in parts) {
					body += key + '=' + encodeURIComponent(parts[key]) + '&';
				}
				body = body.substr(0, body.length - 1);
				var url = ZOTERO_CONFIG.REPOSITORY_URL + "report";
				Zotero.HTTP.promise('POST', url,
					{ body: body, successCodes: false, foreground: true })
				.then(_sendErrorReportCallback)
				.done();
			}
			
			
			function _sendErrorReportCallback(xmlhttp) {
				var wizard = document.getElementById('zotero-error-report');
				if (!wizard) {
					return;
				}
				
				var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
					.getService(Components.interfaces.nsIPromptService);
				
				if (!xmlhttp.responseXML){
					try {
						if (xmlhttp.status>1000){
							ps.alert(
								null,
								Zotero.getString('general.error'),
								Zotero.getString('errorReport.noNetworkConnection')
							);
						}
						else {
							ps.alert(
								null,
								Zotero.getString('general.error'),
								Zotero.getString('errorReport.invalidResponseRepository')
							);
						}
					}
					catch (e){
						ps.alert(
							null,
							Zotero.getString('general.error'),
							Zotero.getString('errorReport.repoCannotBeContacted')
						);
					}
					
					wizard.rewind();
					return;
				}
				
				
				var reported = xmlhttp.responseXML.getElementsByTagName('reported');
				if (reported.length != 1) {
					ps.alert(
						null,
						Zotero.getString('general.error'),
						Zotero.getString('errorReport.invalidResponseRepository')
					);
					wizard.rewind();
					return;
				}
				
				wizard.advance();
				wizard.getButton('cancel').disabled = true;;
				wizard.canRewind = false;
				var reportID = reported[0].getAttribute('reportID');
				document.getElementById('zotero-report-id').setAttribute('value', reportID);
				document.getElementById('zotero-report-result').hidden = false;
			}
		}
	]]>
	</script>
	
	<wizardpage onpageshow="Zotero_Error_Report.init()" label=" ">
		<description id="zotero-failure-message"/>
		<textbox id="zotero-error-message" class="plain" readonly="true" multiline="true" flex="1"/>
		<description id="zotero-unrelated-message">&zotero.general.note; &zotero.errorReport.unrelatedMessages;</description>
		<description id="zotero-advance-message"/>
	</wizardpage>
	
	<wizardpage onpageshow="Zotero_Error_Report.sendErrorReport()" label=" ">
		<description>&zotero.errorReport.submissionInProgress;</description>
	</wizardpage>
	
	<wizardpage label=" ">
		<description>&zotero.errorReport.submitted;</description>
		<description id="zotero-report-result" hidden="true">
			&zotero.errorReport.reportID;
			<textbox id="zotero-report-id" class="plain" readonly="true"/>
		</description>
		<description>&zotero.errorReport.postToForums;</description>
		<description>&zotero.errorReport.notReviewed;</description>
	</wizardpage>
</wizard>
