/**
 * Editor object.
 * @author Mark Woodman
 */
function Editor()
{   
    Editor.instance = this;
    
    Editor.rotateButton = document.getElementById("rotateButton")
    Editor.installButton = document.getElementById("installButton");
    
    Editor.hardwareSelector = document.getElementById("hardwareSelector")
    Editor.hardwareSelector.onchange = Editor.updateHardwareSelection;
    
    Editor.createKeyButton = document.getElementById("createKeyButton");
    Editor.createKeyButton.onclick = Editor.showKey;
    
    Editor.keyLinkField = document.getElementById("keyLink");
    
    Editor.playNowButton = document.getElementById("playNowButton");
    Editor.playNowButton.onclick = Editor.playPuzzle;
    
    Editor.testPuzzleButton = document.getElementById("testPuzzleButton");
    Editor.testPuzzleButton.onclick = Editor.testPuzzle;
    
    Editor.puzzleAuthorField = document.getElementById("puzzleAuthor");
    Editor.puzzleNameField = document.getElementById("puzzleName");
    Editor.puzzleParField = document.getElementById("puzzlePar");
    
    new Grid(true);
    
    Editor.puzzleAuthorField.value = Grid.instance.puzzleAuthor;
    Editor.puzzleNameField.value = Grid.instance.puzzleName;
    Editor.puzzleParField.value = Grid.instance.puzzlePar;
}

Editor.updateHardwareSelection = function(evt)
{
    Editor.installButton.checked = true;
    return true;
}

Editor.createPlayUrl = function(testMode)
{
    var key = "key=" + Grid.instance.createKey(); 
    var authorName = "by=" + escape(Editor.puzzleAuthorField.value) + "&";
    var puzzleName = "name=" + escape(Editor.puzzleNameField.value) + "&";
    var puzzlePar = "par=" + escape(Editor.puzzleParField.value) + "&";
    var theUrl = Editor.getBaseUrl() + "index.htm?" + authorName + puzzleName + puzzlePar + key;
    if(testMode==true)
    {
        theUrl += "&test=true";   
    }
    return theUrl;
}

Editor.getBaseUrl = function()
{
    return new String(document.location).split("editor.htm")[0];
}

Editor.showKey = function()
{
    if(Editor.validatePuzzle()==false) return;
    
    if(Editor.keyLinkField)
    {
        var playUrl = Editor.createPlayUrl(false);
    	Editor.keyLinkField.value+="URL: \n" + playUrl + "\n\nHTML:\n";
        Editor.keyLinkField.value+= "Play <a href='" + playUrl + "'>";
        Editor.keyLinkField.value+= escape(Editor.puzzleNameField.value) + "</a> by ";
        Editor.keyLinkField.value+= Editor.puzzleAuthorField.value + " on ";
        Editor.keyLinkField.value+= "<a href='" + Editor.getBaseUrl() + "'>LAN Panic!</a>";
        scroll(0,2000);
    }
    
}

Editor.playPuzzle = function()
{
    if(Editor.validatePuzzle()==false) return;
    var theUrl = Editor.createPlayUrl(false);
    document.location = theUrl; 
}

Editor.testPuzzle = function()
{
    if(Editor.validatePuzzle()==false) return;
    var theUrl = Editor.createPlayUrl(true);
    document.location = theUrl; 
}

Editor.validatePuzzle = function()
{
    Grid.instance.locateServers();
    if(Grid.instance.servers.length==0)
    {
        alert("Your puzzle needs at least one Server.");
        return false;
    }   
    else
    {
        return true;
    }
}

/**
 * Swap out the hardware at a given point.
 */
Editor.prototype.pointClicked = function(x,y)
{
    debug("Editing @ " + x + "," + y);
    // What is there now?
    var currentHardware = Grid.instance.hardwareMatrix[x][y];
    
    // What is selected?
    var selectedHardware = Editor.hardwareSelector.value;
    
    // What is the action?
    var rotate = Editor.rotateButton.checked;
    
    // If it is the same hardware as the selected hardware,
    // Just rotate it.
    if(rotate || Hardware.TYPES[currentHardware.hashCode]==selectedHardware)
    {
        // Rotate
        Grid.instance.rotatePoint(x,y);
        
        // Update the connection paths
        Grid.instance.requestPing(true);
    }
    else
    {
        // Instantiate a new piece of hardware
        // TODO: Find a more elegant way to do this.
        var evalStmt = "new " + selectedHardware + "(new Array(" + x + "," + y + "), 'north')";
        debug(evalStmt);
        var inst = eval(evalStmt);
        
        // Reassign the point on the grid to the new instance
        Grid.instance.replaceHardware(x, y, inst);
        
        // Since a server might have just been placed/replaced, find them all
        Grid.instance.locateServers();
        
        // Update the connection paths.
        Grid.instance.requestPing(true);
    }
}
