/// class PD.Web.UI.Controls.TreeViewDataSource

PD.Web.UI.Controls.TreeViewDataSource =
Class.extend({

    //===================================
    // FIELDS
    //==================================

    _tree: null,
    _dataSource: null,
    _localDataSource: null,
    _remoteDataSource: null,

    //===================================
    // CONSTRUCTOR
    //===================================

    init: function (options) {
        this._tree = options.tree;
        this._initDataSource();
        this._initLocalDataSource();
        this._initRemoteDataSource();
        PD.BCL.Observable.mixin(this);
    },

    //===================================
    // PROPERTIES
    //==================================

    get_options: function () {
        return this._options;
    },
    
    get_tree: function () {
        return this._tree;
    },

    get_instance: function () {
        return this._dataSource;
    },

    //===================================
    // EVENT HANDLERS
    //===================================

    _onRead: function (options) {
    
        this._readLocalDataSource(options);
        
        if (!options.readSuccess) {
            this._readRemoteDataSource(options);
        }

    },

    //===================================
    // PUBLIC METHODS
    //===================================

    remove: function (id) {

        var ds = this.getDataSource(id),
            item = this.get(id);

        if (item) {
            ds.remove(item);
        }
        
    },

    get: function (id) {

        var ds = this.getDataSource(id);

        return ds.get(id);

    },

    getOption: function (path, defaultValue) {
        return this.get_tree().getOption(path, defaultValue);
    },

    getDataSource: function (id) {

        var item =  null,
            result = null;

        if (this._localDataSource != null) {
            item = this._localDataSource.get(id);
        }

        if (item) {
            result = this._localDataSource;
        }
        else {
            result = this._dataSource;
        }

        return result;

    },

    //===================================
    // PRIVATE METHODS
    //===================================
    
    _initDataSource: function () {
    
        this._dataSource = new kendo.data.HierarchicalDataSource({
            data: this.getOption("localData", undefined),
            transport: {
                read:  $.proxy(this._onRead, this)
            },
            schema: {
                model: {
                    id: "id",
                    hasChildren: "hasChildren"
                }
            }
        });

    },

    _initLocalDataSource: function () {

        var that = this,
            localData = this.getOption("localData", undefined);

        if (localData !== undefined) {
        
            this._localDataSource = new kendo.data.HierarchicalDataSource({
                data: PD.Type.isArray(localData) ? localData : [localData],
                schema: this._dataSource.options.schema
            });

            this._localDataSource.fetch();

        }

    },

    _initRemoteDataSource: function () {
        
        var remoteData = this.getOption("remoteData", undefined);

        if (remoteData !== undefined) {
        
            this._remoteDataSource = {
                read: this.getOption("remoteData.read", function (e) {
                    e.success([]);
                })
            };

        }

    },

    _readLocalDataSource: function (options) {

        var dataSource = this._localDataSource,
            item = null,
            data = null;

        if (dataSource) {
            if (options.data.id != null) {
                item = dataSource.get(options.data.id);
                if (item) {
                    data = item.load();
                }
            }
            else {
                data = dataSource.data();
            }
            if (data) {
                options.success(data);
                options.readSuccess = true;
            }
        }

    },

    _readRemoteDataSource: function (options) {
        
        var dataSource = this._remoteDataSource;

        if (dataSource) {
            dataSource.read({
                id: options.data.id,
                item: options.data.dataItem,
                success: options.success
            });
        }

    }

});