﻿Ext.namespace('FinnKodeWeb');


FinnKodeWeb.PathNavigatorToolbar = function(config) {
    config = config || {};
    this.listeners = config.listeners;
    this.addEvents({
        'navigate': true
    });
    this.bookSection = config.bookSection;
    this.category = config.category;
    FinnKodeWeb.PathNavigatorToolbar.superclass.constructor.call(this, config);

    var _objectPathRecord = Ext.data.Record.create([
        { name: 'caption', mapping: '@caption' },
        { name: 'locateId', mapping: '@locateId' }
    ]);
    var _xmlObjectPathReader = new Ext.data.XmlReader({
        record: 'pathItem',
        idPath: '@locateId'
    }, _objectPathRecord);
    var _httpProxyLoadObjectPath = new Ext.data.HttpProxy({
        url: 'FinnKodeWS/ContentService.svc/GetObjectPath',
        method: 'GET'
    });

    var _tb = config.toolbar;
    var _textItem = new Ext.Toolbar.TextItem({
        text: 'Stinavigator'
    });
    var _textItemIndex = 0;
    _tb.insert(_textItemIndex, _textItem);
    var _nextPathItemIndex = _textItemIndex + 1;

    var handlerToolClicked = function(item, eventObject) {
        MsgSvc.addMsg({
            Category: this.category,
            Action: 'Navigate',
            Lable: item.bookName + ' - ' + (item.text ? item.text : item.getText())
        });
        this.fireEvent('navigate', item.bookSectionId, item.objectId);
    };

    var callbackLoadObjectPath = function(result, options, success) {
        MsgSvc.addMsg({
            Category: 'TableView',
            Action: 'AfterLoadObjectPath',
            Lable: options,
            TrackEvent: false
        });
        if (!success) {
            MsgSvc.addMsg({
                ShowAlertInfo: true,
                Category: 'TableView',
                Action: 'AfterLoadObjectPath',
                Lable: 'Kunne ikke laste' + options,
                TrackEvent: false
            });
        }
        else {
            var toolbar = _tb;
            var textItem = _textItem;
            if (!result) {
                textItem.setText('Stinavigator kunne ikke lastes');
            }
            else {
                textItem.setText('Stinavigator');

                // Get index of Text item
                var records = result.records;
                var recordIndex = 0;
                var pathItemIndex = _textItemIndex + 1;
                if (pathItemIndex < _nextPathItemIndex) {
                    // Keep pathButtons that are valid for the new path
                    for (recordIndex = 0; recordIndex < records.length; recordIndex++) {
                        var pathButton = toolbar.get(pathItemIndex + recordIndex);
                        if (pathButton == null) break;
                        if ((pathButton.text ? pathButton.text : pathButton.getText()) != records[recordIndex].get('caption')) {
                            // Remove this and all subsequent pathButtons
                            for (var removeIndex = _nextPathItemIndex - 1; removeIndex >= pathItemIndex + recordIndex; removeIndex--) {
                                toolbar.remove(toolbar.get(removeIndex), true);
                            }
                            break;
                        }
                    }
                }
                _nextPathItemIndex = _textItemIndex + recordIndex + 1;
                // All previous records exists in toolbar, create pathButton for subsequent and current record
                for (recordIndex = recordIndex; recordIndex < records.length; recordIndex++) {
                    var splitButton = new FinnKodeWeb.PathButton({
                        text: records[recordIndex].get('caption'),
                        bookId: this.bookSection.BookId,
                        bookSectionId: this.bookSection.BookSectionId,
                        bookName: this.bookSection.Description,
                        objectId: result.records[recordIndex].get('locateId'),
                        handler: handlerToolClicked,
                        scope: this
                    });
                    toolbar.insert(_nextPathItemIndex, splitButton);
                    _nextPathItemIndex++;
                }
            }
            toolbar.doLayout();
        }
    };

    this.loadObjectPath = function(bookId, bookSectionId, objectId) {
        MsgSvc.addMsg({
            Category: 'TableView',
            Action: 'BeforeLoadObjectPath',
            Label: objectId,
            TrackEvent: false
        });
        if (_textItem) {
            var tI = _textItem;
            tI.setText('Laster stinavigator');
        }
        _httpProxyLoadObjectPath.doRequest(
        'read',
        null,
        {
            bookId: bookId,
            bookSectionId: bookSectionId,
            objectId: objectId
        },
        _xmlObjectPathReader,
        callbackLoadObjectPath,
        this,
        objectId);
    };
};

Ext.extend(FinnKodeWeb.PathNavigatorToolbar, Ext.util.Observable);
