﻿///<reference path="..\..\core\jscript\CliApplication6.js" />

//--------------------------------------------------------------------------------
function CliPspApplication( propositionInstance, propositionName, authToken, authSignature, configXml )
{
    this._propositionInstance = propositionInstance;
    this._propositionName     = propositionName;

    this._pageLoader = new CliPspPageLoader( authToken, authSignature );
    this._base = new CliApplication( propositionInstance, this._pageLoader );
    this._base.Init( propositionName, authToken, authSignature, configXml );

    try
    {
        var offlineObj = this._propositionInstance.Offline;
        this._pageLoader.SetOfflineObject( offlineObj );
    }
    catch( e ){}
    
    this._continuationManager = null;
}

//--------------------------------------------------------------------------------
CliPspApplication.prototype =
{
    //--------------------------------------------------------------------------------
    Start : function()
    {
        ///<summary>starts the application if launched for no specific page</summary>
        
        this._base.Start();
    },
    
    //--------------------------------------------------------------------------------
    PageLoader : function()
    {
        ///<summary>Get the page laoder class for the application</summary>
        
        return this._pageLoader;
    },
    
    //--------------------------------------------------------------------------------
    GetNamedStateItem : function( name )
    {
        /// <summary>Get the named state item for this application.  Item supports
        /// Get( name ), Set( name, value ) and SequenceId()</summary>
        
        //TODO: introduce caching of 'wrap'
        var itemName = this._propositionName + "_" + name + "_814B7CA5-11D0-48da-A1D4-1FC7772BE10E";
        
        var stateItem = this.GetLibraryCache().GetItem( itemName );
        if( stateItem == null )
        {
            stateItem = this.GetLibraryCache().AddItem( itemName );
            stateItem.CacheAttribute( "seq", Math.random() );
        }
        
        var wrap = new Object();
        wrap[ "_item" ] = stateItem;
        wrap[ "Set" ] = function( name, value )
        {
            this._item.CacheAttribute( "seq", Math.random() );
            this._item.CacheAttribute( name, value );
        };
        wrap[ "Get" ] = function( name )
        {
            return this._item.GetAttribute( name );
        };
        wrap[ "SequenceId" ] = function()
        {
            return this._item.GetAttribute( "seq" );
        };
        
        return wrap;
    },
    
    //--------------------------------------------------------------------------------
    ListLibrary : function( channelId, statefilter)
    {
        return this._propositionInstance.VideoLibraryActions.ListLibrary( channelId, statefilter );
    },

    //--------------------------------------------------------------------------------
    GetContinuationManager : function()
    {
        if( this._continuationManager == null )
            this._continuationManager = new CliContinuationManager();
            
        return this._continuationManager;
    },
        
    //--------------------------------------------------------------------------------
    GetChannelsIterator : function() // : IChannelListIterator
    {
        ///<summary>Get an iterator for the known channels</summary>
        
        var iterator = this._propositionInstance.Settings.Channels;
        return iterator;
    },
    
    //--------------------------------------------------------------------------------
    SwitchDomainIfNecessary : function( url )
    {
        ///<summary>if the client is installed for this app, and were not in the client
        ///we should open in the client, unless we are configured not to.</summary>
        ///<param name="url"></param>

        try{ this.WriteTrace( "js:app.SwitchDomainIfNecessary: " + url ); }catch( e ){}
        
        this._base.SwitchDomainIfNecessary( url );
    },
    
    //--------------------------------------------------------------------------------
    GetLibraryCache : function() // : ILibraryCache
    {
        ///<summary>Gets an ILibraryCache object</summary>
        
        return this._propositionInstance.LibraryCache;

    },
    
    //--------------------------------------------------------------------------------
    DownloadManagerNewItemEvent : function( mediaContentId )
    {
        ///<summary>called by the client when a new item is detected by the download
        ///manager</summary>
        
        var libraryItem = this.GetItem( mediaContentId );
        
        if( libraryItem != null )
        {
            this._ImportMissingMetadataForItem( libraryItem );
        }
    },
    
    //--------------------------------------------------------------------------------
    GetItem : function( mediaContentId )
    {
        return this._base.GetItem( mediaContentId );
    },
    
    //--------------------------------------------------------------------------------
    DownloadManagerCompletionEvent : function( mediaContentId )
    {
        ///<summary>not implemented. Called by the client when an item finishes
        ///downloading</summary>
    },

    //--------------------------------------------------------------------------------
    GetSynchronisationObserver : function()
    {
        return this._propositionInstance.SynchronisationObserver.SyncInProgress;
    },
    
    //-------------------------------------------------------------------------------
    TraceVerbose : function()
    {
        /// <summary>Returns true if verbose tracing is enabled</summary>
        
        return this._propositionInstance.Tracing.TraceVerbose;
    },

    //-------------------------------------------------------------------------------
    TraceError : function()
    {
        /// <summary>Returns true if error tracing is enabled</summary>
        
        return this._propositionInstance.Tracing.TraceError;
    },
    
    //-------------------------------------------------------------------------------
    WriteTrace : function( msg )
    {
        this._propositionInstance.Tracing.Write( "VERBOSE", "HybridClient", "", msg );
    },

    //-------------------------------------------------------------------------------
    WriteTraceError : function( msg )
    {
        this._propositionInstance.Tracing.Write( "ERROR", "HybridClient", "", msg );
    },
    
    //-------------------------------------------------------------------------------
    _ImportMissingMetadataForItem : function( libraryItem )
    {
        if( libraryItem.VideoId == null )
            return;
        
        var url = CliBuilOnlineUrl( "/page/videoMetadata.do?videoId=" +
            libraryItem.VideoId +
            "&deliveryMethod=dl" +
            "&moid=" + libraryItem.MediaContentId );
   
        iokoAjaxAnywhere.getAJAX( url, 'content' );
    }

////-------------------------------------------------------------------------------
//    //
//    CliController.prototype.ImportMissingMetadata = function()
//    {
//        var itemsIterator = _propositionInstance.VideoLibraryActions.ListItemsWithoutMetadata();
//        
//        while( itemsIterator.More )
//        {
//            this._ImportMissingMetadataForItem( itemsIterator.Current );
//            itemsIterator.Next();
//        }
//    }
}//

