/**
 * @author Tran Quoc Huy
 */
isc.defineClass("OPINIATOR_SurveyResponseSelector", isc.Canvas);
OPINIATOR_SurveyResponseSelector.addClassProperties({
    lastSelectedRow:null
    });

OPINIATOR_SurveyResponseSelector.addProperties({

    // Constructor properties   
    /**
     *  Called when a survey has been selected or a new survey is
     *  created.
     *  
     *  @param survey   May either be a survey object or a survey
     *  id. Check type to determine what to do with the function
     */
    onSurveyOpen: function ( survey ) {},
    
    /**
     * Called when there is not currently a selection
     */
    onNothingOpen: function () {}, 


    // Superclass properties
    showResizeBar:true,
    styleName:"defaultCanvas",


    // Private Properties
    _treegrid: null,
    _surveyTree: null,	    	

    initWidget : function ()
        {	   
		
        this._treegrid = OPINIATOR_SurveyTree.create({
            width:"100%",
            height:"100%",
            target:this,
            nodeClick: function( tree, node, recordNum )     
                { this.target._openNode(node); }
            });		
		this.children = [this._treegrid];	
        this.Super("initWidget", arguments);
       
        },

	 // override hide method
	hide: function() {		
		_hide(this);
	},
	
	// override show method	
	show: function() {
		_show(this);
	},
	
    destroy: function()
        {
        delete this._surveyTree;
        delete this._treegrid.target;
        delete this._treegrid;       
        this.Super("destroy", arguments);
        },

    /**
     * Show the busy indicator in the tree grid
     */
    showBusy: function ( state, delay )
        { this._treegrid.showBusy(state, delay); },

    refreshSurveyNode : function( survey )
        {
       		var node = this._findSurveyNode(survey);        
            node.levelName = survey.name;
            node.status = survey.status;
            this._treegrid.refreshRow(this._treegrid.getRecordIndex(node));
           
        },

    /**
     *  Re-loads the entire from the server
     */
    refresh : function()
        {
        this.showBusy(true, OPINIATOR.BUSY_DELAY);
        WebClientSvc.getSurveyTree(this.callback("_setSurveyTree"), this.callback("error"));
        },
	
	_getTreeGrid: function() {
		return this._treegrid;	
	},		
	
	setSurveyTree: function() {
		this._surveyTree = globalTree;
		this._surveyTree.nodes = globalData;
	},
	
    /**
     *  Sets a survey tree 
     */
    _setSurveyTree: function ( surveyTree )
        {
        this.showBusy(false);
        this._surveyTree = isc.isAn.Object(surveyTree) ? surveyTree : {nodes:[]};
        this._surveyTree.nodes = OPINIATOR.toArray(this._surveyTree.nodes);

        var tree = isc.Tree.create({
            modelType: "parent",
            rootValue: 0,
            nameProperty: "levelName",
            titleProperty: "levelName",
            idField: "id",
            parentIdField: "parentId",
            showRoot: false,			
            data: surveyTree.nodes
            });
		
        this._treegrid.setData(tree);
		
		globalTree = tree;
		globalData = this._surveyTree.nodes;
		
		// expand all, then collapse on so that all icon will be loaded correctly.
		tree.openAll();
		tree.closeAll();
		
		// open again at level 1
		tree.openFolder(tree.data[0]);			
		
        if ( OPINIATOR_SurveySelector.lastSelectedRow !== null )
            { 
            this._treegrid.selectSingleRecord(OPINIATOR_SurveySelector.lastSelectedRow); 
            this._openNode(this._treegrid.getRecord(OPINIATOR_SurveySelector.lastSelectedRow)); 
            }
        },

    _findSurveyNode: function( survey )
        {
        var nodes = this._surveyTree.nodes;
        for ( var i = 0; i < nodes.length; i++ )
            {
            var node = nodes[i];
            if ( !node.isFolder && (node.recordId == survey.id) )
                { return node; }
            }
        return null;
        },
  
    /**
     *  Opens the node.  If the node is not specified the selection
     *  will be used.
     */
    _openNode: function ( node )
        {
        if ( !isc.isAn.Object(node) )
            { 
            node = this._treegrid.getSelectedRecord(); 
            }

        OPINIATOR_SurveySelector.lastSelectedRow = this._treegrid.getRecordIndex(node);

        ViewPortID.setSubtitle(node.levelName);

        if ( isc.isAn.Object(node) )
            {
            if ( node.isFolder )
                { 
               		this.onNothingOpen();				 
                }
            else
                { 
                	this.onSurveyOpen(node.recordId);				
                }
            }
        else
            {
            	this.onNothingOpen();			
            }
        },	 

    selectSurvey: function( survey )
        {
        // Find the surveys node
        var node = this._findSurveyNode(survey);
        if ( isc.isAn.Object(node) )
            {
            this._treegrid.deselectAllRecords();
            this._treegrid.selectRecord(node);
            }
        },

    getSurveyTree: function()
        { return this._surveyTree; },

   

   
    error : function( code, msg )
        {
        this._treegrid.showBusy(false);
        switch ( msg )
            {
            case "DeletedState":
                OPINIATOR.warn("This survey has been deleted by another user.");
                this.refresh();
                break;

            default:
                OPINIATOR.warn("Unhandled Exception: " + msg);
                break;
            }
        },

    callback: function (methodName)
        { return {target:this, methodName:methodName}; }

    });

