//
// Copyright (C) 2007 rPath, Inc
// All Rights Reserved
//

/////////////////////////////////////////////////////////////////////////////////////////////
// ctor
function EC2Launcher() {
    connect("startButton", "onclick", this, "launchClicked");
    bindMethods(this);
}

EC2Launcher.prototype.updateStatus = function(e) {
    var callback = function(aReq) {
        this.code = aReq.code;
        if(aReq.code == "wait") {
            $('launchStatus').innerHTML = "(" + aReq.status + ")";
            callLater(20, this.updateStatus);
        } else if (aReq.code == "ready") {
            hideElement('div_during_launch');
            showElement('div_success');
            $('applianceUrl').innerHTML = '<span class="emphasis">Your session is ready</span><br/><table><tr><td>InfoView</td><td><a class="button-link" id="appliance_url" onclick="window.open(\'' + aReq.url + '/InfoViewApp/\',\'newwin\');"><img id="goToButtonImage" src="images/button_go.png" style="border: 0px;" class="button_submit"></a></td></tr><tr><td>Central Management Console</td><td><a class="button-link" id="appliance_url" onclick="window.open(\''+ aReq.url + '/CmcApp/\',\'newwin2\');"><img id="goToButtonImage" src="images/button_go.png" style="border: 0px;" class="button_submit"></a></td></tr><tr><td>Shut Down</td><td><a class="button-link" id="shutdownButton"><img id="shutdownButtonImage" src="images/button_go.png" style="border: 0px;" class="button_submit"></a></td></tr></table>';
            this.instanceId = aReq.instance;
            connect("shutdownButton", "onclick", this, "terminateAMI");
        } else if (aReq.code == "error") {
            hideElement('div_during_launch');
            showElement('div_before_launch');
            showElement('div_timeout');
            logDebug(aReq.status);
        } else if (aReq.code == "redirect") {
            $('launchStatus').innerHTML = "(Launch failed. Please refresh this page and launch again.)";
            //$('launchStatus').innerHTML = "(" + aReq.status + ")";
            //callLater(20, aReq.url);
        } else {
            callLater(20, this.updateStatus);
        }
    }

    //var req = loadJSONDoc(this.statusUrl, {"nocache": (new Date()).getTime()});
    var req = loadJSONDoc(this.statusUrl);
    req.addCallback(bind(callback, this));
}

EC2Launcher.prototype.launchClicked = function() {
    // Update status button
    this.launchAMI();

    disconnectAll('startButton');
    setNodeAttribute('startButtonImage', 'src', getNodeAttribute('startButtonImage', 'src').replace(/_[nh]\./, "_d."));
    showElement('div_during_launch');
    hideElement('div_before_launch');
    hideElement('div_timeout');
    hideElement('div_success');

    // reset location of "bottom" div after sections displayed
    resetBottomDiv($(steps[current][DIVNAME]));

}

// functions
EC2Launcher.prototype.launchAMI = function() {

    // callback for launch request
    var callback = function(aReq) {
        logDebug(aReq.url);
        this.statusUrl = aReq.url;
        this.updateStatus();
    };

    // errorback for launch request (e.g. if the backend ISEs... ugh)
    var errback = function () {
        hideElement('div_during_launch');
        showElement('div_timeout');
        setNodeAttribute('startButtonImage', 'src', getNodeAttribute('startButtonImage', 'src').replace(/_[hd]\./, "_n."));
        disconnectAll('startButton');
        connect("startButton", "onclick", this, "launchAMI");
        behavior_hover("startButtonImage");
        resetBottomDiv($(steps[current][DIVNAME]));
    };

    // request the backend to launch the AMI instance
    //var cName = urlEncode(userName + " at " + companyName);
    //var url = urlEncode("launcher.json;launch?nickname=" + cName);
    var req = loadJSONDoc("/crystalreports/launcher.py/reserveInstance");
    req.addCallback(bind(callback, this));
    req.addErrback(bind(errback, this));

};

EC2Launcher.prototype.terminateAMI = function() {

    // callback for launch request
    var callback = function(aReq) {
        var one = 1;
    };

    var instance = urlEncode(this.instanceId);
    var req = loadJSONDoc("/crystalreports/launcher.py/terminateInstance?instance=" + instance);
    alert("Session shutdown.");
    req.addCallback(bind(callback, this));

};

