﻿///<reference path="CliPspLibraryItemRowRenderer.js" />

// CliLibraryView
// CliLibraryItemRowSelectionChangedEventArgs

//--------------------------------------------------------------------------------
function CliLibraryView( application )
{
    /// <summary>Represents the view of the library displayed on a page</summary>

    this._libraryActionButtons = null; // CliLibraryActionButtons();
    this._application = application;
    this._libraryContent = null;
    
    this.SelectedChannels = null; // public array of channel ids to show. should be updated externally whenever the visible channels are changed
    this.StateFilterCondition = ""; // public state filter string. should be updated externally whenever the filters are changed
}

CliLibraryView.prototype =
{
    //--------------------------------------------------------------------------------
    GetLibraryCache : function()
    {
        return this._application.GetLibraryCache();
    },
    
    //--------------------------------------------------------------------------------
    GetLibraryItemIterator : function( channelId, sortCriteria, sortAscending )
    {
        var libraryData = this._application.ListLibrary( channelId, this.StateFilterCondition );
        
        if( sortCriteria != null )
        {
            var promoteDownloading = false;
            libraryData.Sort( sortCriteria, sortAscending, promoteDownloading );
        }
        
        return libraryData;
    },
    
    //--------------------------------------------------------------------------------
    Init : function( channelOrdering, channelArray, itemRowSelectionChanged, libraryActionButtons )
    {
        ///<summary>Call this function once to initialise the library view.  It should be called
        ///before any other method on the object.</summary>
        
        this._libraryActionButtons = libraryActionButtons;
    
        var itemRowSelectionChangedEvt =
        function( eventArgs )
        {
            var mediaContentId = eventArgs.getMediaContentId();
            var checked = eventArgs.getSelected();
            libraryActionButtons.ItemRowSelectionChanged( mediaContentId, checked );
            itemRowSelectionChanged(  mediaContentId, checked );
        };
    
        var self = this;
    
        this._libraryContent = new CliLibraryContent( this._application, this, document.getElementById( "cli-libview-container" ), channelOrdering, itemRowSelectionChangedEvt );
        this.Refresh( channelArray );
    },
    
    //--------------------------------------------------------------------------------
    Refresh : function()
    {
        ///<summary>Refreshes the library view.  This is equivalent to rendering the 
        //library for the first time</summary>
        
        if( this.SelectedChannels == null )
        {
            CliDebug( "lib refresh without channels." );
            return;
        }
            
        var libraryActionButtons = this._libraryActionButtons;
        
        var libraryContent = this._libraryContent;
        var self = this;
        
        this._libraryContent.Reset();
        this._libraryActionButtons.Reset();
        this._libraryActionButtons.EnableButtons();
        
        for( var i = 0; i < this.SelectedChannels.length; i++ )
        {
            this.Expand( this.SelectedChannels[ i ] );
        }
    },
    
    //--------------------------------------------------------------------------------
    RefreshIfItemNotDisplayed : function( mediaContentId )
    {
        ///<summary>Refreshes the library view if the specified item is not currently
        ///displayed on the screen.  Preserves user selections etc.</summary>
        
        var rowRend = new CliLibraryItemRowRenderer( this );
        var rowId = rowRend.GenerateRowId( mediaContentId );
        var rowDiv = document.getElementById( rowId );
        
        if( rowDiv == null )
        {
            var libraryItem = this._application.GetItem( mediaContentId );
            
            if( libraryItem != null )
            {
                var channelId = libraryItem.ItemChannel;
                this.Collapse( channelId );
                this.Expand( channelId );
            }
        }
    },
    
    //--------------------------------------------------------------------------------
    RefreshIfItemDisplayed : function( mediaContentId )
    {
        ///<summary>Refreshes the library view if the specified item is currently
        ///displayed on the screen.  Preserves user selections etc.</summary>

        var rowRend = new CliLibraryItemRowRenderer( this );
        var rowId = rowRend.GenerateRowId( mediaContentId );
        var rowDiv = document.getElementById( rowId );
        
        if( rowDiv != null )
        {
            var libraryItem = this._application.GetItem( mediaContentId );
            
            if( libraryItem != null )
            {
                var channelId = libraryItem.ItemChannel;
                this.Collapse( channelId );
                this.Expand( channelId );
            }
        }
    },
    
    //--------------------------------------------------------------------------------
    RefreshItem : function( mediaContentId )
    {
        ///<summary>Requests that the row for an item is refreshed</summary>
        
        this._libraryContent.RefreshItem( this._application.GetItem( mediaContentId ) );
    },
    
    //--------------------------------------------------------------------------------
    Expand : function( channelId )
    {
        ///<summary>Expands a section of the library specific to a channel</summary>
       
        this._libraryContent.ExpandSection( channelId );
        this._EnsureNoItemsDisplayedCorrectly();
    },
    
    //--------------------------------------------------------------------------------
    Collapse : function( channelId )
    {
        ///<summary>Collapses a section of the library specific to a channel</summary>
        this._libraryContent.CollapseSection( channelId );
        this._EnsureNoItemsDisplayedCorrectly();
    },
    
    //--------------------------------------------------------------------------------
    SelectAllClick : function( selected )
    {
        ///<summary>Should be called when all items in the library are to be selected</summary>
        
        this._libraryContent.SelectAllItems( selected );
    },
    
    //--------------------------------------------------------------------------------
    ApplyFunctionToSelectedItems : function( predicate )
    {
        ///<summary>Applies the supplied predicate to all items currently selected.
        ///The predicate should take a single parameter that is the mediaContentId
        ///of the selected item.</summary>
        
        this._libraryContent.ApplyFunctionToSelectedItems( predicate );
    },
    
    //--------------------------------------------------------------------------------
    _EnsureNoItemsDisplayedCorrectly : function()
    {
        CliLibShowNoItems( this._libraryContent.IsEmpty() );
    }
}//






//--------------------------------------------------------------------------------
function CliLibraryItemRowSelectionChangedEventArgs( mediaContentId, selected, state )
{
    /// <summary>Holds the data representing a row selection event</summary>
    /// <param name="state">represents the state of the item when rendered</param>

    this._mediaContentId = mediaContentId;
    this._selected = selected;
    this._state = state;
}

CliLibraryItemRowSelectionChangedEventArgs.prototype =
{
    getMediaContentId : function() { return this._mediaContentId; },
    getSelected : function() { return this._selected; },
    getState : function() { return this._state; }
}//
