/**
 * @fileoverview
 * This script defines a widget for interacting with expression data from flyatlas.
 * @author <a href="http://purl.org/net/aliman">Alistair Miles</a>
 * @version $Revision:538 $ on $Date: 2008-08-27 09:08:41 +0100 (Wed, 27 Aug 2008) $ by $Author: aliman $
 * @requires flykit.util
 * @requires flykit.sparql.Service
 * @requires flykit.flyatlas.Service
 * @requires YAHOO.util.Connect
 * @requires jQuery
 * For license terms see http://flykit.googlecode.com
 */
 
flykit.namespace("flykit.flyatlas");
 
 
 
 
 

/*
 * ----------------------------------------------------------------
 *                             WIDGET
 * ----------------------------------------------------------------
 */







flykit.flyatlas.ProfileWidget = function( service, renderer ) {
    var _context = "flykit.flyatlas.ProfileWidget";
    try {
        
        flykit.debug("call private constructor", _context);
        this.__init__(service, renderer);
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
    
}



 
 



/** @private */
flykit.flyatlas.ProfileWidget.prototype._controller = null;

/**
 * @private
 * @type flykit.mvcutils.GenericModel2
 */ 
flykit.flyatlas.ProfileWidget.prototype._model = null;

/**
 * @private
 */
flykit.flyatlas.ProfileWidget.prototype._renderer = null;

/**
 * @private
 */
flykit.flyatlas.ProfileWidget.prototype._service = null;










/**
 * @private
 */
flykit.flyatlas.ProfileWidget.prototype.__init__ = function( service, renderer ) {
    var _context = "flykit.flyatlas.ProfileWidget.prototype.__init__";
    try {
        
        this._service = service;
        this._renderer = renderer;
 
        this._resultsReceivedEvent = new YAHOO.util.CustomEvent("RESULTSRECEIVED", this);
        
        flykit.debug("create a model", _context);
        this._model = new flykit.mvcutils.GenericModel2();
        this._model.setDefinition(flykit.flyatlas.ProfileWidget.modelDefinition);
        
        flykit.debug("create a controller", _context);
        this._controller = new flykit.flyatlas.ProfileWidget.Controller(this._model, service, this);
        
        flykit.debug("connect the renderer to the model", _context);
        this._renderer.connect(this._model);
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};







/**
 * @public
 * @param {Array<String>} querynames
 * @param {Array<String>} flybaseids
 */
flykit.flyatlas.ProfileWidget.prototype.getProbesAndGenesByExpressionProfile = function( profile, limit ) {
    var _context = "flykit.flyatlas.ProfileWidget.prototype.getProbesAndGenesByExpressionProfile";
    try {
        
        flykit.debug("pass through to controller", _context);
        this._controller.getProbesAndGenesByExpressionProfile(profile, limit);
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};






/**
 * Subscribe to a custom event.
 * @param {String} type the type of the event
 * @param {Function} listener the callback function
 * @param {Object} obj a custom object, passed back to the callback function 
 */
flykit.flyatlas.ProfileWidget.prototype.subscribe = function(type, listener, obj) {
    var _context = "flykit.flyatlas.ProfileWidget.prototype.subscribe";
    try {
        if (type == "RESULTSRECEIVED") {
            this._resultsReceivedEvent.subscribe(listener, obj);
        }
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }    
};





/*
 * ----------------------------------------------------------------
 *                             CONTROLLER
 * ----------------------------------------------------------------
 */






/**
 * @class
 */
flykit.flyatlas.ProfileWidget.Controller = function( model, service, controllee ) {
    var _context = "flykit.flyatlas.ProfileWidget.Controller";
    try {
        
        this._model = null;
        this._service = null;
        this._controllee = null;
        
        flykit.debug("call private constructor", _context);
        this.__init__(model, service, controllee);        
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};








/**
 * @private
 */
flykit.flyatlas.ProfileWidget.Controller.prototype.__init__ = function( model, service, controllee ) {
    var _context = "flykit.flyatlas.ProfileWidget.Controller.prototype.__init__";
    try {
        
        this._model = model;
        this._service = service;
        this._controllee = controllee;
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};








/**
 * @public
 */
flykit.flyatlas.ProfileWidget.Controller.prototype.getProbesAndGenesByExpressionProfile = function( profile, limit ) {
    var _context = "flykit.flyatlas.ProfileWidget.Controller.prototype.findGenesByAnyNameBatch";
    try {
        
        flykit.info("getProbesAndGenesByExpressionProfile: "+profile, _context);
        flykit.debug("pass through to private implementation", _context);
        this._getProbesAndGenesByExpressionProfile(profile, this._getProbesAndGenesByExpressionProfileSuccess(), this._getProbesAndGenesByExpressionProfileFailure(), limit);
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};








/**
 * @private
 */
flykit.flyatlas.ProfileWidget.Controller.prototype._getProbesAndGenesByExpressionProfile = function( profile, success, failure, limit ) {
    var _context = "flykit.flyatlas.ProfileWidget.Controller.prototype._getProbesAndGenesByExpressionProfile";
    try {
        
        flykit.debug("set state pending", _context);
        this._model.set("STATE", "PENDING");
        
        flykit.debug("set model property query", _context);
        this._model.set("QUERY", profile);

        flykit.debug("set result null", _context);
        this._model.set("RESULTS", null);
                
        flykit.debug("set message null", _context);
        this._model.set("MESSAGE", null);
                
        flykit.debug("set limit "+limit, _context);
        this._model.set("LIMIT", limit);
                
        flykit.debug("kick off request", _context);
        this._service.getProbesAndGenesByExpressionProfile(profile, success, failure, limit);        
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};









/**
 * @private
 */
flykit.flyatlas.ProfileWidget.Controller.prototype._getProbesAndGenesByExpressionProfileSuccess = function() {
    var _context = "flykit.flyatlas.ProfileWidget.Controller.prototype._getProbesAndGenesByExpressionProfileSuccess";
    var self = this;
    /**
     * @param {Object<String,<Array<AssayGroup>>} results a map from flybaseids to arrays of AssayGroup objects
     */
    return function( results ) {
        try {
            
            flykit.info("request success", _context);
            
            flykit.debug("set model results", _context);
            self._model.set("RESULTS", results);
            
            flykit.debug("set model state", _context);
            self._model.set("STATE", "READY");
            
            var limit = self._model.get("LIMIT");
            
            flykit.debug("limit "+limit+" results.length "+results.length, _context);
            
            if (results.length == limit) {
                var explanation = "At most " + limit + " results are shown.";
                self._model.set("MESSAGE", explanation);
            }
            
            flykit.debug("fire event", _context);
            self._controllee._resultsReceivedEvent.fire(results);
            
        } catch (unexpected) {
            flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
            throw unexpected;    
        }
    };
};







/**
 * @private
 */
flykit.flyatlas.ProfileWidget.Controller.prototype._getProbesAndGenesByExpressionProfileFailure = function() {
    var _context = "flykit.flyatlas.ProfileWidget.Controller.prototype._getProbesAndGenesByExpressionProfileFailure";
    var self = this;
    /**
     * @param {ResponseObject} response
     */
    return function( response ) {
        try {
            
            flykit.err("request failed: "+response.status+" "+response.statusText, _context);
            self._model.set("MESSAGE", "there was an error retrieving data from the server, see the logs for more info");
            self._model.set("STATE", "SERVERERROR");
            
        } catch (unexpected) {
            flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
            throw unexpected;    
        }
    };
};







/*
 * ----------------------------------------------------------------
 *                             MODEL DEFINITION
 * ----------------------------------------------------------------
 */





/** 
 * @type Object
 */
flykit.flyatlas.ProfileWidget.modelDefinition = {

    properties : [ "STATE", "RESULTS", "MESSAGE", "QUERY", "LIMIT" ],
    
    values : {
        "STATE" : [ "READY", "PENDING", "SERVERERROR", "UNEXPECTEDERROR" ]
    },
    
    initialize : function( data ) {
        data["STATE"] = "READY";
        data["RESULTS"] = null;
        data["MESSAGE"] = null;
        data["QUERY"] = null;
        data["LIMIT"] = 200;
    }

};







/*
 * ----------------------------------------------------------------
 *                             DEFAULT RENDERER
 * ----------------------------------------------------------------
 */







flykit.flyatlas.ProfileWidget.DefaultRenderer = function() {};


/** 
 * @private 
 * @type Element
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._canvas = null;

/**
 * @private 
 * @type Element
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._pendingPane = null;

/**
 * @private 
 * @type Element
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._resultsSummaryPane = null;

/**
 * @private 
 * @type Element
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._resultsPane = null;

/**
 * @private 
 * @type Element
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._messagePane = null;





/**
 * Set the DOM element to which this renderer applies and initialise it.
 * @param {Element} canvas
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype.setCanvas = function( canvas ) {
    var _context = "flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype.setCanvas";
    try {
        
        flykit.debug("set canvas", _context);
        this._canvas = $(canvas);
        this._initCanvas();
        
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};






/**
 * @private
 * @param {Element} canvas
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._initCanvas = function() {
    var _context = "flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._initCanvas";
    try {
        
        flykit.debug("begin init canvas", _context);
        
        var canvas = this._canvas;
        canvas.addClass("flyatlasProfileWidget");

        var pp = $("<p class='pendingPane'>pending (please wait, this can take a while) ...</p>").hide();
        canvas.append(pp);
        this._pendingPane = pp;

        var rsp = $("<p class='resultsSummaryPane'>this text should never be displayed</p>").hide();
        canvas.append(rsp);
        this._resultsSummaryPane = rsp;
        
        var rp = $("<div class='resultsPane'></div>").hide();
        canvas.append(rp);
        this._resultsPane = rp;
        
        var mp = $("<p class='messagePane'>this text should never be displayed</p>").hide();
        canvas.append(mp);
        this._messagePane = mp;            

        flykit.debug("init canvas done", _context);
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};





/**
 * @public
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype.connect = function( model ) {
    var _context = "flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype.connect";
    try {
        
        flykit.debug("connect to model", _context);
        model.subscribeAll(this._onModelChanged, this);
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};





/**
 * @private
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._onModelChanged = function( type, args, self ) {
    var _context = "flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._onModelChanged";
    try {
        
        var handlers = {
            "STATE":"_onStateChanged",
            "QUERY":"_onQueryChanged",
            "RESULTS":"_onResultsChanged",
            "MESSAGE":"_onMessageChanged",
            "LIMIT": "_onLimitChanged"
        };
        var handler = handlers[type];
        flykit.debug("call model changed handler: "+handler, _context);
        self[handler](args[0], args[1], args[2]);
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};






/**
 * @private
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._onQueryChanged = function( from, to, get ) {
    var _context = "flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._onQueryChanged";
    try {
        
        // do nothing, we will access the value later
        flykit.debug("query changed from "+from+" to "+to, _context);
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};






/**
 * @private
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._onStateChanged = function( from, to, get ) {
    var _context = "flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._onStateChanged";
    try {
        
        flykit.debug("state changed from "+from+" to "+to, _context);

        if ( to == "PENDING" ) {
            this._pendingPane.show();
            this._messagePane.hide();
            this._resultsSummaryPane.hide();
            this._resultsPane.hide();
        }
        else if ( to == "READY" ) {
            this._pendingPane.hide();
            this._messagePane.show();
            this._resultsSummaryPane.show();
            this._resultsPane.show();         
        } 
        else if ( to == "SERVERERROR" || to == "UNEXPECTEDERROR" ) {
            this._pendingPane.hide();
            this._messagePane.show();
            this._resultsSummaryPane.hide();
            this._resultsPane.hide();         
        } 
        else {
            // this should never happen
            throw {message:"invalid state: "+to};
        }
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};






/**
 * @private
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._onResultsChanged = function( from, to, get ) {
    var _context = "flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._onResultsChanged";
    try {
        
        flykit.debug("results changed to: "+to, _context);
        
        if (to == null) {
            flykit.debug("empty results summary pane", _context);
            this._resultsSummaryPane.empty();
            flykit.debug("empty results pane", _context);
            this._resultsPane.empty();
        } 
        
        else {

            flykit.debug("render results summary", _context);
            this._renderResultsSummary(to);
    
            flykit.debug("render results", _context);
            this._renderResults(to);        
            
        }
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};







/**
 * @private
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._onMessageChanged = function( from, to, get ) {
    var _context = "flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._onMessageChanged";
    try {
        
        flykit.debug("message changed to: "+to, _context);
        
        if (to == null) {
            flykit.debug("empty messsage pane", _context);
            this._messagePane.empty();
        } 
        
        else {

            this._messagePane.html(to);
            
        }
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};



/**
 * @private
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._onLimitChanged = function( from, to, get ) {
    var _context = "flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._onLimitChanged";
    try {
        
        // do nothing, we will access the value later
        flykit.debug("limit changed from "+from+" to "+to, _context);
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};





/**
 * @private
 * @param {Object<String,<Array<AssayGroup>>} results a map from flybaseids to arrays of assay group objects
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._renderResultsSummary = function( results ) {
    var _context = "flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._renderResultsSummary";
    try {
        
        flykit.debug("render results summary", _context);

        var content = "found "+results.pairs.length+" results ("+results.probes.length+" distinct probe IDs, "+results.genes.length+" distinct gene IDs) from <a href='http://flyatlas.org'>flyatlas.org</a> ("+flykit.flyatlas.provenance+") ...";

        this._resultsSummaryPane.html(content);
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};








/**
 * @private
 */
flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._renderResults = function( results ) {
    var _context = "flykit.flyatlas.ProfileWidget.DefaultRenderer.prototype._renderResults";
    try {
        flykit.debug("render results", _context);
        
        var content = "<strong>matching probes: </strong>";

        for (var i=0; i<results.probes.length;i++) {
            var probe = results.probes[i];
            content += "<strong><a href='http://flyatlas.org/probeset.cgi?name="+probe+"'>"+probe+"</a></strong> (";
            var genecount = 0;
            for (var j=0; j<results.pairs.length; j++) {
                var pair = results.pairs[j];
                if (pair[0] == probe) {
                    if (genecount > 0) {
                        content += ", ";
                    }
                    content += "<a href='http://flybase.org/reports/" + pair[1] + ".html'>" + pair[1] + "</a>";
                    genecount++;
                }
            }
            content += "); ";
        }        
        
        this._resultsPane.html(content);
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};












