ReferenceTreeNodeTypes = {
    LessonRoot: 0,
    LessonNode: 1,
    Element: 2
};
Ext.define('TreeNodeModel', {
    extend: 'Ext.data.Model',
    defaultProxyType: "memory",
    proxy: "memory",
    fields: [
        {name: 'id', type: 'string'}   ,
        {name: 'nodeType', type: 'int'},
        {name: 'text', type: 'string'}
    ]
});
Ext.define('ReferenceWindowClass', {

    constructor: function(){


        this.treeStore = Ext.create('Ext.data.TreeStore', {
            root:{
                id: "r_" + Author.LessonManager.AuthorLesson.LessonRoot.id,
                nodeType: ReferenceTreeNodeTypes.LessonRoot,
                text: Author.LessonManager.AuthorLesson.LessonRoot.Name,
                expanded: true,
                iconCls: 'treeNodeIcon',
                icon: Ext.String.format("./lib/images/{0}.GIF", Author.LessonManager.AuthorLesson.LessonRoot.Type)
            },
            model: 'TreeNodeModel'
        });

        this.LastX = ( Ext.Element.getViewportWidth() - this.DEFAULTWIDTH) / 2;
        this.LastY = Math.max(( Ext.Element.getViewportHeight() - this.DEFAULTHEIGHT) / 2, 0);

        this.InformationLabel = new Ext.form.Label({
            id: 'ReferenceWindow-InformationLabel',
            itemId: 'lbInformation',
            text: 'Click on a screen title to display that screen.'
        });
        this.IsInitialized = false;
        this.TreePanel = Ext.create('Ext.tree.Panel', {
            id: 'ReferenceWindow-TreePanel',
            region: 'center',
            margins: '1 1 1 1',
            autoScroll: true,
            rootVisible: true,
            store: this.treeStore,
            animate:false,
            listeners: {
                beforeitemcollapse: function(node, eOpts){
                    if (node.getDepth() < 3) {
                       // Author.log("cancel collapse");
                    }
                },
                itemclick: function(view, record, item, index, e, eOpts){
                    switch (record.NodeType) {
                        case ReferenceTreeNodeTypes.LessonNode:
                            Author.DisplayManager.navigateToScreen(record.LessonNodeRef.id);
                            break;
                        case ReferenceTreeNodeTypes.Element:
                            Author.DisplayManager.navigateToScreen(record.ElementRef.ParentNode.id, record.ElementRef.id);
                            break;
                    }
                },
                scope: this
            }
        });
        this.ReferenceWindow = new Ext.Window({
                title: 'Reference',
                id: 'TheReferenceWindow',
                layout: 'border',
                width: ReferenceWindowClass.DEFAULTWIDTH,
                height: ReferenceWindowClass.DEFAULTHEIGHT,
                x: window.innerWidth - ReferenceWindowClass.DEFAULTWIDTH,
                y: 50,
                minWidth: 215,
                closeAction: 'hide',
                collapseFirst: true,
                collapsible: true,
                items: [
                    Ext.create('Ext.panel.Panel', {
                        id: 'ReferenceWindow-InformationPanel',
                        region: 'north',
                        bodyBorder: false,
                        border: false,
                        bodyStyle: "background:transparent",
                        margins: '1 1 1 1',
                        height:16,
                        items: this.InformationLabel
                    }),
                    this.TreePanel
                ],
                listeners: {
                    show: this.onShown,
                    move: this.onMove,
                    scope: this
                }
            }
        )
    },
    statics: {
        DEFAULTWIDTH: 350,
        DEFAULTHEIGHT: 400
    },
    highLightCurrentScreen: function(){
        var lessonNodeID = false, elementID = false;
        if (Author.CurrentStateManager.CurrentLessonNode) {
            lessonNodeID = Author.CurrentStateManager.CurrentLessonNode.id;
        }
        if (Author.CurrentStateManager.CurrentElement) {
            elementID = Author.CurrentStateManager.CurrentElement.id;
        }
        this.selectNode(lessonNodeID, elementID);
    },
    selectNode: function(lessonNodeID, elementID, treeNode){
        lessonNodeID = lessonNodeID || false;
        elementID = elementID || false;
        var nodeType = ReferenceTreeNodeTypes.LessonRoot;
        if (elementID) {
            nodeType = ReferenceTreeNodeTypes.Element;
        } else if (lessonNodeID) {
            nodeType = ReferenceTreeNodeTypes.LessonNode;
        }
        var record = this.TreePanel.getRootNode().findChildBy(function(r){
            switch (nodeType) {
                case ReferenceTreeNodeTypes.LessonRoot:
                    if (r.get('nodeType') == nodeType) {
                        return true;
                    }
                    break;
                case ReferenceTreeNodeTypes.LessonNode:
                    if (r.get('nodeType') == nodeType && r.get('id') == lessonNodeID) {
                        return true;
                    }
                    break;
                case ReferenceTreeNodeTypes.Element:
                    if (r.get('nodeType') == nodeType && r.get('id') == elementID) {
                        return true;
                    }
                    break;
            }
        }, this, true);
        if (record) {
            this.TreePanel.getSelectionModel().select(record);
        }
    },
    loadLessonStructure: function(){
        this.StructureRoot = this.getStructureRoot(Author.LessonManager.AuthorLesson.LessonRoot);
        this.getStructureNode(this.StructureRoot, Author.LessonManager.AuthorLesson.LessonRoot.ChildNode);
        this.IsInitialized = true;
    },
    getStructureRoot: function(rootNode){
        //  var rootNode = this.TreePanel.getRootNode();
        return this.TreePanel.getRootNode();
        /* var structureRootNode = this.TreePanel.getRootNode().appendChild({
         text: rootNode.Name,
         expanded: true,
         id: rootNode.id,
         nodeType: ReferenceTreeNodeTypes.LessonRoot,
         iconCls: 'treeNodeIcon',
         children: [],
         icon: Ext.String.format("./lib/images/{0}.GIF", rootNode.Type)

         });
         structureRootNode.LessonNodeRef = rootNode;
         structureRootNode.NodeType = ReferenceTreeNodeTypes.LessonRoot;
         return structureRootNode;*/
    },
    getStructureNode: function(parentStructureNode, lessonNode){
        if (!lessonNode.ShowInReference) {
            return;
        }

        var theNode = parentStructureNode.appendChild({
            text: lessonNode.Name,
            expanded: true,
            id: "n_" + lessonNode.id,
            nodeType: ReferenceTreeNodeTypes.LessonNode,
            children: [],
            iconCls: 'treeNodeIcon',
            icon: Ext.String.format("./lib/images/{0}.GIF", lessonNode.Type)
        });

        theNode.LessonNodeRef = lessonNode;
        theNode.NodeType = ReferenceTreeNodeTypes.LessonNode;
        if (lessonNode.Type == 'MENU') {
            lessonNode.ChildrenNodes.each(function(node, index, length){
                this.getStructureNode(theNode, node);
            }, this);
        }
        if (lessonNode.Type == 'SEQUENCE' || lessonNode.Type == 'RANDOM') {
            lessonNode.Elements.each(function(element, index, length){
                this.getStructureElement(theNode, element);
            }, this);
        }
        return theNode;
    },
    getStructureElement: function(parentStructureNode, element){
        if (!element.IsShowInReference) {
            return;
        }
        var elementStructureNode = parentStructureNode.appendChild({
            text: element.Name,
            id: "e_" + element.id,
            nodeType: ReferenceTreeNodeTypes.Element,
            icon: Ext.String.format("./lib/images/{0}.GIF", element.Type),
            iconCls: 'treeNodeIcon',
            leaf: true
        });
        elementStructureNode.ElementRef = element;
        elementStructureNode.NodeType = ReferenceTreeNodeTypes.Element;
        return elementStructureNode;
    },
    onStructureNodeClicked: function(node, evt){
        if (!node.isSelected()) {
            Author.DisplayManager.navigateToScreen(node.LessonNodeRef.id);
            evt.stopEvent();
        }
    },
    onStructureElementClicked: function(elementNode, evt){
        if (!elementNode.isSelected()) {
            Author.DisplayManager.navigateToScreen(elementNode.ElementRef.ParentNode.id, elementNode.ElementRef.id);
            evt.stopEvent();
        }
    },
    setTitle: function(title){
        this.ReferenceWindow.setTitle(title);
    },
    setInformationText: function(text, align){
        this.InformationLabel.setText(Ext.String.format("<div style='text-align: {1};'>{0}</div>", text, align || 'left'), false);
    },
    onMove: function(c, x, y){
        this.LastX = Math.max(x, 0);
        this.LastY = Math.max(y, 0);
    },
    onShown: function(){
        if (!this.IsInitialized) {
            this.loadLessonStructure();
        }
        this.ReferenceWindow.setPosition(this.LastX, this.LastY);
        //   this.highLightCurrentScreen();
    },
    show: function(title, isExpended){
        title = title || 'Reference';
        isExpended = isExpended || 'false';
        if (!this.isVisible()) {
            this.ReferenceWindow.show();
        }
        this.setTitle(title);
        if (isExpended) {
            //  this.StructureRoot.expandChildNodes(true);
        }
    },
    hide: function(){
        this.ReferenceWindow.hide();
    },
    isVisible: function(){
        return (this.ReferenceWindow.isVisible());
    }
});
