﻿Ext.namespace('FinnKodeWeb');

FinnKodeWeb.PanelBookSectionContent = function(config) {
    config = config || {};
    this.bookSection = config.bookSection;

    var _isContentCreated = false;
    var _tree = null;
    var _history = null;
    var _searchField = null;
    var _leftContainer = new Ext.Container({
        layout: 'fit'
    });

    // Create a tab panel for selecting different view
    var _bookSectionViewsTabPanel = new Ext.TabPanel({
        id: NamingService.createId(this.bookSection.BookSectionId, 'TabPanel'),
        deferredRender: true,
        border: false,
        bodyBorder: false
    });

    Ext.apply(config, {
        title: this.bookSection.BookSectionName,
        id: NamingService.createId(this.bookSection.BookSectionId, 'BookSectionPanel'),
        layout: 'border',
        items: [
            {
                region: 'center',
                layout: 'fit',
                border: false,
                items: _bookSectionViewsTabPanel
            }, {
                region: 'west',
                width: 350,
                split: true,
                layout: 'fit',
                border: false,
                items: _leftContainer
            }
        ],
        navigate: function(hashManager) {
            if (!hashManager) return;
            if (hashManager.bookSectionId != this.bookSection.BookSectionId) return;
            if (!hashManager.hasContentHash()) return;
            //var bookSectionViewsTabPanel = Ext.getCmp(NamingService.createId(hashManager.bookSectionId, 'TabPanel'));
            if (!_bookSectionViewsTabPanel || !_tree) {
                return;
            }
            if (hashManager.hasContentHash()) {
                var tabIndex = 0;

                _bookSectionViewsTabPanel.items.each(function(item, index, length) {
                    if (item.navigate) {
                        item.navigate(hashManager);
                    }
                });
                if (_tree && _tree.navigate) {
                    _tree.navigate(hashManager);
                }
                if (_history && _history.navigate) {
                    _history.navigate(hashManager);
                }
            }
            var viewTab = null;
            if (!hashManager.bookSectionViewId) {
                // update hashManager with current view
                viewTab = _bookSectionViewsTabPanel.getActiveTab();
                if (viewTab) {
                    hashManager.bookSectionViewId = viewTab.getItemId();
                }
            }
            viewTab = _bookSectionViewsTabPanel.getComponent(hashManager.bookSectionViewId);
            if (viewTab) {
                _bookSectionViewsTabPanel.activate(viewTab);
            }
        },
        navigateDefault: function() {
            _bookSectionViewsTabPanel.setActiveTab(0);
            _bookSectionViewsTabPanel.items.each(function(panelView) {
                if (panelView.navigateDefault) panelView.navigateDefault();
            });
            if (_tree != null) {
                _tree.addListener(
                    'activate',
                    function() {
                        var rootNode = _tree.getRootNode();
                        //rootNode.expand(false);
                    },
                    this,
                    {
                        single: true
                    }
                );
            }
        },
        updateHash: function(hashManager) {
            hashManager.bookSectionId = this.bookSection.BookSectionId;
            var activeTab = _bookSectionViewsTabPanel.getActiveTab();
            if (activeTab && activeTab.updateHash) activeTab.updateHash(hashManager);
        },
        afterAppLoaded: function() {
            _bookSectionViewsTabPanel.items.each(function(panelView) {
                if (panelView.afterAppLoaded) panelView.afterAppLoaded();
            });
        },
        createContents: function() {
            if (!_isContentCreated) {
                _isContentCreated = true;
                if (this.bookSection.IsSearchable) {
                    // If booksection is searchable, create a dynamic tree, search panel, flow view and table view
                    var tabFlowView = new FinnKodeWeb.PanelIframeFlowView({
                        id: NamingService.createId(this.bookSection.BookSectionId, 'PanelFlowView'),
                        itemId: 'flow',
                        title: 'Bokvisning - ' + this.bookSection.Description,
                        bookSection: this.bookSection
                    });
                    var tabTableView = new FinnKodeWeb.PanelTableView({
                        id: NamingService.createId(this.bookSection.BookSectionId, 'PanelTableView'),
                        itemId: 'table',
                        title: 'Tabellvisning - ' + this.bookSection.Description,
                        bookSection: this.bookSection,
                        hidden: true
                    });
                    _bookSectionViewsTabPanel.add(tabFlowView);
                    _bookSectionViewsTabPanel.add(tabTableView);
                    _bookSectionViewsTabPanel.doLayout();

                    _tree = new FinnKodeWeb.TreeDynamicXml({
                        id: NamingService.createId(this.bookSection.BookSectionId, 'TreeDynamicXml'),
                        title: 'Trenavigator',
                        bookSection: this.bookSection
                    });
                    var searchResultGrid = new FinnKodeWeb.SearchResultGrid({
                        id: NamingService.createId(this.bookSection.BookSectionId, 'SearchResultGrid')
                    });
                    var searchSource = Ext.util.JSON.decode('[{"SearchSourceId": "' + this.bookSection.BookSectionId + '", "Selected": true }]');
                    _searchField = new FinnKodeWeb.SearchComboBox({
                        anchor: '100%',
                        fieldLabel: 'Søk ' + this.bookSection.Description,
                        labelStyle: 'font-weight: bold;font-size: 9px;font-family: tahoma,arial,verdana,sans-serif;',
                        bookSectionId: this.bookSection.BookSectionId
                    });
                    _searchField.addListener('searchClicked', function() {
                        var searchTerm = _searchField.getValue();
                        searchResultGrid.search(searchTerm, searchSource);
                    });

                    _history = new FinnKodeWeb.HistoryPanel({
                        id: NamingService.createId(this.bookSection.BookSectionId, 'HistoryPanel'),
                        title: 'Besøkte koder',
                        bookSection: this.bookSection
                    });

                    var searchPanel = new Ext.Panel({
                        title: 'Søk ' + this.bookSection.Description,
                        layout: 'border',
                        border: false,
                        bodyBorder: false,
                        listeners: {
                            'activate': function() {
                                if (_searchField) _searchField.focus('', 50);
                            },
                            'afterlayout': function() {
                                if (_searchField) _searchField.focus('', 50);
                            }
                        },
                        items: [
                            {
                                region: 'north',
                                split: false,
                                height: 30,
                                padding: 5,
                                layout: 'form',
                                border: false,
                                /*title: 'Søkeutrykk',*/
                                labelWidth: 95,
                                items: _searchField
                            },
                            {
                                region: 'center',
                                layout: 'fit',
                                border: false,
                                items: searchResultGrid
                            }
                        ]
                    });
                    var navigatorTabs = new Ext.TabPanel({
                        deferredRender: true,
                        animScroll: true,
                        enableTabScroll: true,
                        border: false,
                        bodyBorder: false,
                        activeTab: 0,
                        items: [
                            searchPanel,
                            _tree,
                            _history
                        ]
                    });
                    _leftContainer.add(navigatorTabs);
                }
                else {
                    // If booksection is not searchable, create a static tree and a help view
                    var tabHelpView = new FinnKodeWeb.PanelHelpView({
                        id: NamingService.createId(this.bookSection.BookSectionId, 'PanelHelpView'),
                        itemId: 'help',
                        title: this.bookSection.BookSectionName
                    });
                    _bookSectionViewsTabPanel.add(tabHelpView);

                    _tree = new FinnKodeWeb.TreeStaticXml({
                        id: NamingService.createId(this.bookSection.BookSectionId, 'TreeStaticXml'),
                        title: 'Trenavigator',
                        bookSection: this.bookSection
                    });
                    _leftContainer.add(_tree);
                }
                _bookSectionViewsTabPanel.doLayout();
                _leftContainer.doLayout();
                this.doLayout();
            }
        }
    });
    FinnKodeWeb.PanelBookSectionContent.superclass.constructor.call(this, config);
}

Ext.extend(FinnKodeWeb.PanelBookSectionContent, Ext.Panel);
