// ==UserScript==
// @name           Repeat Crafting
// @namespace      http://www.dealtinlead.com
// @description    Gives you 1-5 x Craft option
// @include        http://www.dealtinlead.com/Game
// @include        http://dealtinlead.com/Game
// ==/UserScript==

if (!GM_setValue) {
    alert('Please upgrade to the latest version of Greasemonkey.');
    return;
}

// if (GM_getValue("dil_recipe")) {
//     alert("Set to recipe: " + GM_getValue("dil_recipe"));
// }
//if (GM_getValue("dil_craft")) {
//    alert("Craft counter: " + GM_getValue("dil_craft"));
//}

var craftForms = document.evaluate(
     '//form[@action="/Game/Craft"]',
      document,
      null,
      XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
      null);

// If we didn't find a craft button, reset count and return...
if (craftForms.snapshotLength != 1) {
    GM_setValue("dil_craft", 0);
    // GM_log("No craft button found");
    return;
}

// GM_log("Found craft button");

var craftForm = craftForms.snapshotItem(0);

// If you need to extend this to craft more than 5 at a time, add the HTML to the innerHTML value
var newSelect = document.createElement('select');
newSelect.setAttribute("name", "gm_quantity");
newSelect.setAttribute("id", "gm_quantity");
newSelect.innerHTML = "";
for (var loop=1;loop<=20;loop++) {
    newSelect.innerHTML = newSelect.innerHTML + '<option value="' + loop + '">' + loop + 'x</option>';
}

// Be careful - this button acts just like the Craft button without the onclick="return false" javascript!
var forgetButton = document.createElement('input');
forgetButton.setAttribute("id", "gm_forget");
forgetButton.setAttribute("type", "submit");
forgetButton.setAttribute("value", "Clear Recipe");
forgetButton.setAttribute("onclick", "return false");

// the Craft button is [1]
// the select dropdown is [2]
// the recipe dropdown is [4]
// FIXME: relying on these numbered nodes is dangerous...
var recipe_idx = -1;
var qty_idx = -1;
var craft_idx = -1;
for (var idx = 0; idx < craftForm.childNodes.length; idx++) {
    if (craftForm.childNodes[idx].name == "recipe") {
        recipe_idx = idx;
        craftForm.childNodes[idx].setAttribute("id", "gm_recipe");
        craftForm.insertBefore(newSelect, craftForm.childNodes[idx]);
        qty_idx = idx-1;
    } else if (craftForm.childNodes[idx].value == "Craft") {
        craft_idx = idx;
        craftForm.childNodes[idx].setAttribute("id", "gm_craft");
        craftForm.insertBefore(forgetButton, craftForm.childNodes[idx]);
    }
}

// didn't get all the indexes? abort! abort!
if ((craft_idx == -1) || (qty_idx == -1) || (recipe_idx == -1)) {
    GM_log("Something went wrong - and index wasn't found...");
    GM_log("Indexes c:" + craft_idx + " q:" + qty_idx + " r:" + recipe_idx); 
    return;
}

// change the dropdown to the last recipe
var recipe_select = craftForm.childNodes[recipe_idx];
var select_idx = 0;
var found_recipe = 0;
for (var idx = 0; idx < recipe_select.childNodes.length; idx++) {
    if ((GM_getValue("dil_recipe","").length > 0) && (recipe_select.childNodes[idx].value == GM_getValue("dil_recipe"))) {
        recipe_select.selectedIndex = select_idx;
        found_recipe = 1;
//        alert("recipe found: " + GM_getValue("dil_recipe") + " = " + recipe_select.childNodes[idx].value);
    }
    if (recipe_select.childNodes[idx].innerHTML) {
        select_idx ++;
    } 
}

// We might have all the other things, but didn't find the recipe we
// want to repeat (can craft, but not this recipe) so abort
if (! found_recipe) {
    GM_setValue("dil_craft", 0);
}

// setup the listener for the button.
// When you click it, store the number of loops and the recipe...
document.getElementById("gm_craft").addEventListener('click',
    function(event) {
        var quantity = document.getElementById("gm_quantity");
        if (quantity) {
            quantity = quantity.value;
        } else {
            quantity = 0;
        }
        GM_setValue("dil_craft", quantity);
        GM_setValue("dil_recipe", document.getElementById("gm_recipe").value);
//        GM_log(GM_getValue("dil_craft") + " " + GM_getValue("dil_recipe"));
    },
true);

document.getElementById("gm_forget").addEventListener('click',
    function(event) {
        GM_setValue("dil_recipe","");
        GM_setValue("dil_craft",0);
        // alert("Multi-Crafting values cleared");
        recipe_select.selectedIndex = 0;
        return;
    },
true);

if (GM_getValue("dil_craft",0) > 0) {
    GM_setValue("dil_craft", GM_getValue("dil_craft",0) - 1);

    if (GM_getValue("dil_craft", 0) > 0) {
        // submit id_craft form
        // alert("Resubmitting...");
        document.getElementById("gm_craft").form.submit();
    }
}

