﻿///<reference path="CliPropConfig.js" />
///<reference path="CliPspLibrarySection.js" />

//--------------------------------------------------------------------------------
function CliLibraryContent( application, libraryView, containerDiv, channelOrdering,
itemRowSelectionchangedEvt )
{
    ///<summmary>Responsible for the library items content section of the
    ///library page.</summary>
    ///<param name="application"></param>
    ///<param name="libraryView"></param>
    ///<param name="containerDiv">The actual div that will contain the library
    ///content.</param>
    
    this._application = application;
    this._libraryView = libraryView;
    this._sections = null;          // map of channelId->CliLibrarySection
    this._sectionContainers = null; // map of channelId->DIV
    
    this._ctor( channelOrdering, containerDiv, itemRowSelectionchangedEvt );
}

CliLibraryContent.prototype =
{
    //--------------------------------------------------------------------------------
    _ctor : function( channelOrdering, containerDiv, itemRowSelectionchangedEvt )
    {        
        this._sections = CliCreateMap();
        this._sectionContainers = CliCreateMap();
        var self = this;

        var sortColumnEventHandler = 
            function( evthChannelId, evthSortId, evthSortOrder )
            {
                var selectedRowsMap = self._sections[ evthChannelId ].SnapshotSelectedItems();
                self.CollapseSection( evthChannelId );
                self.ExpandSection( evthChannelId, evthSortId, evthSortOrder, selectedRowsMap );
            }

        this.__CreateOrderedSectionsForChannels( itemRowSelectionchangedEvt, sortColumnEventHandler, containerDiv, channelOrdering );
    },
    
    __CreateOrderedSectionsForChannels : function( itemRowSelectionchangedEvt, sortColumnEventHandler, containerDiv, channelOrdering )
    {
        var createElement = document.createElement;
        
        for( var i = 0; i < channelOrdering.length; i++ )
        {
            var channelId = channelOrdering[ i ];
            
            var sectionDiv = createElement( "<div channel-id=\""+ channelId + "\" class=\"section-container\" />" );
            sectionDiv.style.visibility = "hidden";
            containerDiv.appendChild( sectionDiv );
            
            var newSection = new CliLibrarySection( this._libraryView, sectionDiv, channelId,
                this.__GetDisplayName( channelId ), this._application.GetLibraryCache() );
            
            newSection.SortColumnEventHandler = sortColumnEventHandler;
            newSection.ItemRowSelectionChanged = itemRowSelectionchangedEvt;
            
            this._sectionContainers.Add( channelId, sectionDiv );
            this._sections.Add( channelId, newSection );
        }
    },
    
    __GetDisplayName : function( channelId )
    {
        var iterator = this._application.GetChannelsIterator();

        while( iterator.More )
        {
            if( iterator.Name == channelId )
                return iterator.DisplayName;
            
            iterator.Next();
        }
        
        return channelId;
    },

    //--------------------------------------------------------------------------------
    Reset : function()
    {
        var sections = this._sections;
        
        for( var i = 0; i < sections.Keys.length; i++ )
        {
            var section = sections[ sections.Keys[ i ] ];
            if( section != null )
                section.Clear();
        }
    },
    
    //--------------------------------------------------------------------------------
    IsEmpty : function()
    {
        /// <summary>Get a value indicating if the library is empty</summary>
        
        var sections = this._sections;
        var nonEmptyCount = 0;
        
        for( var i = 0; i < sections.Keys.length; i++ )
        {
            var section = sections[ sections.Keys[ i ] ];
            if( section != null )
            {
                if( !section.IsEmpty() )
                    nonEmptyCount++;
            }
        }
        
        return nonEmptyCount == 0;
    },

    //--------------------------------------------------------------------------------
    CollapseSection : function( channelId )
    {
        ///<collapses the display of the specified section and destroys and objects
        ///associated with it.</summary>
        
        var section = this._sections[ channelId ];
        if( section == null )
            return;
        
        section.Clear();
        
        var sectionDiv = this._sectionContainers[ channelId ];

        while( sectionDiv.childNodes.length > 0 )
        {
            sectionDiv.childNodes[ 0 ].removeNode( true );
        }
        sectionDiv.style.visibility = "hidden";
    },
    
    //--------------------------------------------------------------------------------
    ExpandSection : function( channelId, optionalSortId, optionalSortOrder, optionalSelectedRowsMap )
    {
        ///<summary>Expands a section.  If the item is already expanded, it is first collapsed
        ///to ensure that </summary>
        
        this.CollapseSection( channelId );

        var section = this._sections[ channelId ];
        if( section == null )
            return;
        var sectionDiv = section.RenderSection( optionalSortId, optionalSortOrder, optionalSelectedRowsMap );
        
        // null if no section created, say due to no items for that channel                        
        if( sectionDiv != null )
        {
            var id = "lib-section--" + channelId;
            
            var oldDiv = document.getElementById( id );
            if( oldDiv != null )
            {
                oldDiv.parent.removeChild( oldDiv );
            }
                
            sectionDiv.setAttribute( "id", id );
            this._sectionContainers[ channelId ].appendChild( sectionDiv );
            this._sectionContainers[ channelId ].style.visibility = "visible";
        }
    },

    //--------------------------------------------------------------------------------
    RefreshItem : function( libraryItem )
    {
        ///<summary>Redraws the specified library item if it is visible.</summary>
        
        var section = this._sections[ libraryItem.ItemChannel ];
        if( section == null )
            return;

        section.RefreshItem( libraryItem );
    },
    
    //--------------------------------------------------------------------------------
    RenderLibrary : function()
    {
        ///<summary>renders the entire library, filtering appropriately</summary>
        ///<param name="channelArray">array of channel ids to display, or null</param>

        var iterator = this._application.GetChannelsIterator();
        
        if( !iterator.More )
            return;
        
        while( iterator.More )
        {
            try
            {
                this.ExpandSection( iterator.Name );
                //this._RenderSection( iterator.Name, iterator.DisplayName, null, null );
            }
            catch( e )
            {
                CliDebug( "Channel rendering issue: " + e.message );
            }
            
            iterator.Next();
        }
    },
    
    //--------------------------------------------------------------------------------
    _ArrayContains : function( array, value )
    {
        for( var i = 0; i < array.length; i++ )
        {
            if( array[ i ] == value )
                return true;
        }
        
        return false;
    },
    
    //--------------------------------------------------------------------------------
    SelectAllItems : function(selected)
    {
        ///<summary>Mark all rows as selected</summary>
        
        var sections = this._sections;
        
        for( var i = 0; i < sections.Keys.length; i++ )
        {
            var section = sections[ sections.Keys[ i ] ];
            if( section != null )
                section.SelectAllItems(selected);
        }
    },
    
    //-------------------------------------------------------------------------------
    ApplyFunctionToSelectedItems: function(func)
    {
        ///<summary>Applies the function provided to the items currently selected, filtering on state if state is not null</summary>
        ///<param name="func">Passes the MediaContentId through to the function and runs it for each selected item</param>
        var sections = this._sections;
        
        for( var i = 0; i < sections.Keys.length; i++ )
        {
            var section = sections[ sections.Keys[ i ] ];
            if( section != null )
            {
               var selection =  section.SnapshotSelectedItems();
               for(var j=0;j<selection.Keys.length;j++)
               {
                    var mediaContentId = selection.Keys[j];
                    func(mediaContentId);
               }
            }
        }
    }
}//
