﻿var RectangleTreeController=Class.create();
RectangleTreeController.prototype={
    m_tree: null,
    m_wmsId : null,
    m_wmsLayers : null,
    
    getTree : function()
    {
        return this.m_tree;
    },
    
    //Returns item based on index in internal storage array.
    getItem : function(t_index, t_wmsId)
    {
        if( this.m_tree != null &&
            this.m_tree.get(t_wmsId).items.length > t_index )
        {    
            return this.m_tree.get(t_wmsId).items[t_index];
        }
        return null;
    },
    
    findByHash : function(t_hashCode)
    {
        var i, t_length = this.m_wmsLayers.length;
        for( i=0; i<t_length; i++ )
        {
            var t_tree = this.m_tree.get(this.m_wmsLayers[i].id);
            if(t_tree != null)
            {
                var i, t_length = t_tree.items.length;
                for(i=0; i<t_length; i++)
                {
                    var t_item = t_tree.items[i];
                    if(t_item.getHashCode() == t_hashCode)
                    {
                        return t_item;
                    }
                }
            }
        }
        return null;
    },
    
    items : function(t_wmsId)
    {
        if( this.m_tree != null )
        {
            return this.m_tree.get(t_wmsId).items;
        }
        return [];
    },

    initialize: function(t_wmsId, t_tree, t_mapLayersController) 
    {
        this.m_tree=t_tree;
        this.m_wmsId = t_wmsId;
        
        var t_wmss = t_mapLayersController.getWmss().values();
        var t_wmsLayers = [];
        t_wmss.each(
            function( t_wms )
            {
                if( t_wms.id == t_wmsId )
                {
                    t_wmsLayers.push(t_wms.layers);
                }
            }
        );
        this.m_wmsLayers = t_wmsLayers.flatten();
        t_mapLayersController.addEventListener(
            "onwmslayertoggled",
            this._onWMSLayerToggled.bind(this)
            );

//        if (false) //for debugging
//        {
//            $attemptInvoke(
//                (function(t_tree) {
//                    var drawCanvas=new WAPICanvas();
//                    g_kartController.getMap().addDrawCanvas(drawCanvas);

//                    var centerLine=new PolyLine([], { 'rgba': "rgba(200,0,0,0.7)", 'lineWidth': 7 });
//                    centerLine.addPoint(new Coordinate(t_tree.root.bbox.west, t_tree.root.bbox.south));
//                    centerLine.addPoint(new Coordinate(t_tree.root.bbox.west, t_tree.root.bbox.north));
//                    centerLine.addPoint(new Coordinate(t_tree.root.bbox.east, t_tree.root.bbox.north));
//                    centerLine.addPoint(new Coordinate(t_tree.root.bbox.east, t_tree.root.bbox.south));
//                    centerLine.addPoint(new Coordinate(t_tree.root.bbox.west, t_tree.root.bbox.south));


//                    drawCanvas.addPolyLine(centerLine);
//                    // Redraw the canvas
//                    drawCanvas.redraw();
//                }).bind(this, t_tree)
//            );
//        }

    },
    
    _onWMSLayerToggled : function(t_wms, t_layer)
    {
        if( t_wms.id != this.m_wmsId ) return;
        
        //Toggle local layer-array item copy.
        this.m_wmsLayers.each(
            function(t_collectionLayer)
            {
                if( t_collectionLayer.id == t_layer.id )
                {
                    t_collectionLayer.isSelected = t_layer.isSelected;
                }
            }
        );
    },
    
    _isSelected : function(t_layerId)
    {
        var t_zoomlevel = g_kartController.getMap().getZoomLevel();
        var t_layers = this.m_wmsLayers;
        var i, t_length = t_layers.length;
        for( i=0; i<t_length; i++ )
        {
            if( t_layers[i].id == Number(t_layerId) )
            {
                return t_layers[i].isSelected && 
                    (t_layers[i].upperZoom >= t_zoomlevel) && 
                    (t_layers[i].lowerZoom <= t_zoomlevel);
            }
        }
        return false;
    },
    
    findAll: function(t_bbox) {
        var t_list=[];

        var t_trees = this.m_tree;
        var t_keys = t_trees.keys();
        var i, t_length = t_keys.length;
        for(i=0; i<t_length; i++)
        {
            var t_root = t_trees.get(t_keys[i]).root;
            if( this._isSelected(t_keys[i]) && t_root != null )
            {
                this._findAllBase(t_bbox, t_list, t_root, t_keys[i]);
            }
        }
        return t_list;
    },

    _findAllBase: function(t_bbox, t_list, t_root, t_layerId)
    {
        //Offset bbox
        var t_offsetBBox = new BoundingBox(
            t_bbox.west-t_root.offsetX,
            t_bbox.south-t_root.offsetY,
            t_bbox.east-t_root.offsetX,
            t_bbox.north-t_root.offsetY);
        if (t_root.bbox.intersectsWith(t_offsetBBox)) {
            if (t_root.itemIndex != -1) 
            {
                //Check if layer is selected.
                t_list.push(this.getItem(t_root.itemIndex, t_layerId));
            }
            else 
            {
                this._findAllBase(t_bbox, t_list, t_root.childA, t_layerId);
                this._findAllBase(t_bbox, t_list, t_root.childB, t_layerId);
            }
        }
    }
};