/**
 * @class Roo.gui.CertTreeLoader
 * @extends Ext.tree.TreeLoader
 *
 * Loads a tree from JSON nodes.
 *
 * @author ssmeman
 * @version 1.0
 */

/**
 * @constructor
 * @param {Object} config Configuration options
 */
Roo.gui.CertTreeLoader = Ext.extend(Ext.tree.TreeLoader, {
    // private override
    createNode : function(node){
        var attr = { data: {} };

        console.log("building tree of node:");
        console.dir(node);

        for(prop in node) {
        	if(prop != 'children') {
        		attr.data[prop] = node[prop];
        	}
        }
        attr.children = node['children'];

        this.processAttributes(attr);

        return Roo.gui.CertTreeLoader.superclass.createNode.call(this, attr);
    },

    /*
     * Template method intended to be overridden by subclasses that need to provide
     * custom attribute processing prior to the creation of each TreeNode.  This method
     * will be passed a config object containing existing TreeNode attribute name/value
     * pairs which can be modified as needed directly (no need to return the object).
     */
    processAttributes: function(attr) {
    	attr.id = Ext.id(attr);
    	attr.iconCls = 'x-icon-certificate';
    	attr.text = attr.data.Subject.CN;
    	attr.draggable = false;

    	attr.expanded = true;

    	if(attr.children) {
    		attr.loaded = false;
    	}
    	else {
    		attr.loaded = true;
    	}
    }
});


/**
 * @class Roo.gui.CertificateInfoPanel
 * @extends Ext.Panel
 *
 * Shows the certificate tree of a certificate.
 *
 * @author ssmeman
 * @version 1.0
 */

/**
 * @constructor
 * @param {Object} config Configuration options
 */
Roo.gui.CertificateInfoPanel = Ext.extend(Ext.Panel, {
	titleText: messages.signatureWindow.certificaatPanel.title()
	,treeText: messages.signatureWindow.certificaatPanel.treeTitle()
	,detailTitle: messages.signatureWindow.certificaatPanel.detailTitle()
	,id: 'certificateInfoPanel'

	,constructor: function(config) {

		this.plan = config.plan;

		Roo.gui.CertificateInfoPanel.superclass.constructor.apply(this, arguments);
	}

	,initComponent:function() {
		var config = {
			title: this.titleText
			,id: 'certificateInfoPanel'
			,layout: 'border'
			,height: 410
			,width: 560
			,items: [{
				xtype: 'treepanel'
				,title: this.treeText
				,id: 'certificateInfoPanel_tree'
				,region: 'center'
				,height: 85
				,autoScroll: true
				,preloadChildren: true
				,rootVisible: false
				,animate: false

				,root: new Ext.tree.AsyncTreeNode( {children: [this.plan.certificate]} )

				,loader: new Roo.gui.CertTreeLoader({})
				,listeners: {
		            'render': function(tp){
	                    tp.getSelectionModel().on('selectionchange', function(tree, node){
	                        var el = Ext.getCmp('certificateInfoPanel_details');
	                        el.setSource(node.attributes.data.Certificate);
	                        el.setTitle('Details: ' + node.text);
	                    });
		            }
		        }

			},{
				xtype: 'propertygrid'
				,id: 'certificateInfoPanel_details'
				,region: 'south'
				,height: 305
				,title: this.detailTitle
				,enableHdMenu: false
				,disableSelection: true
				,hideHeaders: true
				,listeners: {
					'beforeedit': function(event) {
						//suppress editing of certificate properties
						return false;
					}
				}
			}]
		};

		Ext.apply(this, config);
		Ext.apply(this.initialConfig, config);

		// call parent initComponent
		Roo.gui.CertificateInfoPanel.superclass.initComponent.apply(this, arguments);

	} // e/o function initComponent

});	// e/o extend

// register xtype
Ext.reg('certificateInfoPanel', Roo.gui.CertificateInfoPanel);
