// ==UserScript==
// @name           LP_StockReplies
// @namespace      http://err.no/src/
// @description    (Launchpad) Stock replies
// @include        https://bugs.launchpad.net/*
// @include        https://bugs.beta.launchpad.net/*
// @include        http://bugs.launchpad.net/*
// @include        https://bugs.edge.launchpad.net/*
// @include        https://launchpad.net/*
// @include        http://launchpad.net/*
// @include	   https://beta.launchpad.net/*

// ==/UserScript==

function xpath(query, context) {
  context = context ? context : document;
  return document.evaluate(query, context, null,
			   XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
}


function magicHandler2(formname, shortdesc, longdesc) {

  var element = document.createElement('a');
  element.href = document.location + "?blah";
  var innerTextElement = document.createTextNode(shortdesc);
  element.appendChild(innerTextElement);
  element.addEventListener('click', function(e) { 
    e.preventDefault(); 
    xpath("//textarea[@id='"+  formname + ".comment_on_change']").snapshotItem(0).value = longdesc;

    var i = 0;
    switch (shortdesc) {
    case "Needs Backtrace":
    case "Needs Details":
    case "Needs Valgrind":
    case "Needs New Version Testing":
	i = 8;
 	break;
    case "Forwarded":
	i = 4;
	break;
    case "Support Request":
    case "Default Change":
    case "Duplicate":
    case "No Reply":
    case "Use Apport":
	i = 7;
	break;
    default:
	break;
    }

    if (i != 0) {
    var statusSelect = xpath("//select[@id='"+  formname + ".status']").snapshotItem(0);
    statusSelect.selectedIndex = statusSelect.length - i;
    }
    
    return false;
  }, false);
  return element;
}

window.addEventListener("load", function(e) {

  var prevalues = new Object;

  prevalues["Use Apport"] = "Thank you for taking the time to report this bug and helping to make Ubuntu better. However, your crash report is either missing or challenging to deal with as a '.crash' file. Please follow these instructions to have apport report a new bug about your crash that can be dealt with by the automatic retracer. \n \n If you are running the Ubuntu Stable Release you might need to enable apport in /etc/default/apport and restart. \n \n If you are using Ubuntu with the Gnome desktop environment - launch nautilus and navigate to your /var/crash directory and double click on the crash report you wish to submit. \n \n If you are using Kubuntu or Xubuntu you can file the crash using /usr/share/apport/apport-qt --crash-file=/var/crash/_my_crash_report.crash in a terminal - where _my_crash_report.crash is the crash you would like to report. \n I'm closing this bug report since the process outlined above will automatically open a new bug report which can then dealt with more efficiently. Thanks in advance for your cooperation and understanding."
  prevalues["Needs Backtrace"] = "Thank you for taking the time to report this bug and helping to make Ubuntu better. Please try to obtain a backtrace following the instructions at http://wiki.ubuntu.com/DebuggingProgramCrash and upload the backtrace (as an attachment) to the bug report. This will greatly help us in tracking down your problem.";
  prevalues["Needs Details"] = "Thank you for taking the time to report this bug and helping to make Ubuntu better. Please answer these questions: \n \n * Is this reproducible? \n * If so, what specific steps should we take to recreate this bug? \n \n This will help us to find and resolve the problem.";
  prevalues["Needs Valgrind"] = "Thank you for taking the time to report this bug and helping to make Ubuntu better. Please try to obtain a valgrind log following the instructions at https://wiki.ubuntu.com/Valgrind and attach the file to the bug report. This will greatly help us in tracking down your problem. ";
  prevalues["Needs New Version Testing"] = "Thanks for your bug report. A new version of the package has been uploaded, could you try if you still get the issue and attach a new crash file if that's the case?";
  prevalues["No Reply"] = "We are closing this bug report as it lacks the information, described in the previous comments, we need to investigate the problem further. However, please reopen it if you can give us the missing information and don't hesitate to submit bug reports in the future.";
  prevalues["Forwarded"] = "Thanks for your bug report. This bug has been reported to the developers of the software. You can track it and make comments here:";
  prevalues["GNOME"] = "Thank you for your bug report. The issue is an upstream one and it would be nice if somebody having it could send the bug the to the people writting the software (https://wiki.ubuntu.com/Bugs/Upstream/GNOME)"
  prevalues["Support Request"] = "Thanks for your comments. This does not appear to be a bug report as such. We appreciate the difficulties you are facing, but it would make more sense to raise your question in the support tracker. http://launchpad.net/support"
  prevalues["Default Change"] = "Thanks for your suggestion. The changes you are requesting require more discussion, which should be done on an appropriate mailing list or forum. http://www.ubuntu.com/community/forums/ might be a good start."
  prevalues["Duplicate"] = "Thanks for the bug report. This particular bug has already been reported, but feel free to report any other bugs you find."

  var allForms, thisForm, thisSubmit, thisInput;

  allForms = xpath("//form");

	for (var i = 0; i < allForms.snapshotLength; i++) {
	  thisForm = allForms.snapshotItem(i);
	  thisInput = xpath(".//input[contains(@name, '.sourcepackagename') or contains(@name, '.product')]", thisForm);
	  if (thisInput.snapshotLength == 0) {
	    continue;
	  }
	  var formname = thisInput.snapshotItem(0).name;
	  formname = formname.substr(0, formname.lastIndexOf("."));
	  
	  thisSubmit = xpath(".//label[contains(@for, '.comment_on_change')]", thisForm);
	  thisSubmit = thisSubmit.snapshotItem(0);
	  
	  for (var i in prevalues) {
	      var newElement = magicHandler2(formname, i, prevalues[i]);
	      var leftBrace = document.createTextNode('[');
	      var rightBrace = document.createTextNode('] ');
		/*		var brNode = document.createElement('br');*/
	      thisSubmit.parentNode.insertBefore(leftBrace, leftBrace.nextSibling);
	      thisSubmit.parentNode.insertBefore(newElement, newElement.nextSibling);
	      thisSubmit.parentNode.insertBefore(rightBrace, rightBrace.nextSibling);
		/*		thisSubmit.parentNode.insertBefore(brNode, brNode.nextSibling);*/
	      }
	}
}, false);

