﻿Ext.namespace('FinnKodeWeb');

FinnKodeWeb.ConfigurationLoader =
function(config) {
    this.addEvents('loaded');
    this.listeners = config.listeners;
    FinnKodeWeb.ConfigurationLoader.superclass.constructor.call(this, config);
    // private data members

    var _configSettings = new Array(); // is populated with config settings when it has been loaded from server

    // creates a record for each book
    var _bookRecord = Ext.data.Record.create([
        { name: 'BookId', mapping: 'ID' },
        { name: 'BookName', mapping: 'Name' },
        { name: 'FrontPageUrl', mapping: 'FrontPageUrl' }
    ]);

    // creates an XmlReader which reads Book-elements and creates bookRecords
    var _xmlBookReader = new Ext.data.XmlReader({
        record: 'Books/Book',
        id: 'ID'
    }, _bookRecord);

    var _bookSectionRecord = Ext.data.Record.create([
        { name: 'BookId', mapping: 'BookID' },
        { name: 'BookSectionId', mapping: 'ID' },
        { name: 'BookSectionName', mapping: 'Name' },
        { name: 'Description', mapping: 'Description' },
        { name: 'IsSearchable', mapping: 'IsSearchable', type: 'boolean' },
        { name: 'ContentSource', mapping: 'ContentSource' }
    ]);
    var _xmlBookSectionReader = new Ext.data.XmlReader({
        record: 'BookSection',
        id: 'ID'
    }, _bookSectionRecord);


    var _httpProxyLoadObject = new Ext.data.HttpProxy({
        url: 'FinnKodeWS/ContentService.svc/GetConfiguration'
    });

    // create data store for books - uses httpProxy to load the store
    var _bookStore = new Ext.data.Store({
        proxy: _httpProxyLoadObject,
        reader: _xmlBookReader
    });
    var _bookSectionStore = new Ext.data.Store({
        reader: _xmlBookSectionReader
    });
    // private methods
    var createConfigSettingsFromStore = function() {
        var books = new Array();
        var configSettings = new Object();
        configSettings.Books = books;
        var bookIndex = 0;
        for (bookIndex = 0; bookIndex < _bookStore.getCount(); bookIndex++) {
            var bookObject = new Object();
            var bookRecord = _bookStore.getAt(bookIndex);
            for (var propertyName in bookRecord.data) {
                bookObject[propertyName] = bookRecord.get(propertyName);
            }

            bookObject.BookSections = new Array();
            _bookSectionStore.filter("BookId", bookObject.BookId);
            var bookSectionIndex = 0;
            for (bookSectionIndex = 0; bookSectionIndex < _bookSectionStore.getCount(); bookSectionIndex++) {
                var bookSectionObject = new Object();
                var bookSectionRecord = _bookSectionStore.getAt(bookSectionIndex);
                for (var propertyName in bookSectionRecord.data) {
                    bookSectionObject[propertyName] = bookSectionRecord.get(propertyName);
                }
                bookObject[bookSectionObject.BookSectionId] = bookSectionObject;
                bookObject.BookSections.push(bookSectionObject);
                bookSectionObject.Book = bookObject;
            }
            configSettings[bookObject.BookId] = bookObject;
            books.push(bookObject);
        }
        _bookSectionStore.clearFilter();
        return configSettings;
    };
    bookStoreLoadCallback = function(records, options, success) {
        if (success) {
            _bookSectionStore.loadData(_xmlBookReader.xmlData, false);
            _configSettings = createConfigSettingsFromStore();
            this.fireEvent('loaded');
        }
        else {
            this.raiseFailedEvent(records);
        }
    };

    // public
    Ext.apply(this, {
        load: function() {
            _configSettings = new Array();   // reset _configSettings when new load is initiated
            // load the _bookStore
            _bookStore.load({
                callback: bookStoreLoadCallback,
                scope: this,
                add: false
            });
        },
        getSettings: function() {
            return _configSettings;
        }
    });
};
Ext.extend(FinnKodeWeb.ConfigurationLoader,FinnKodeWeb.Observable);
