// VideoMsgConsumer.js
//
// usage:
//   var videoMsgConsumer = new VideoMsgConsumer();
//   videoMsgConsumer.setPlaybackActor(anActor);
//   videoMsgConsumer.setDelCallback(callbackfunction); // optional -- can also use dependent notification
//   videoMsgConsumer.play(); // plays ALL msgs in order.
//   videoMsgConsumer.erase(); // removes currently playing msg from users msg list
//   videoMsgConsumer.stop(); // stop msg playback

function VideoMsgConsumer()
{
  var _this = this;
  this.eventManager = EventManager;
  this.eventManager();

  var nextMsgIndex = -1;

  var actor = null;
  this.setPlaybackActor = function(anActor) // USER API
  {
    actor = anActor;
  }

  this.stop = function() // USER API
  {
    if (nextMsgIndex != -1)
    {
      actor.stop();
    }
    nextMsgIndex = -1;
    msgList = null;
    this.fireEventDescriptor(this.buildEventDescriptor('info_msgPlayStopped','Message play stopped'));
  }

  this.skip = function() // USER API
  {
    if (nextMsgIndex != -1)
    {
      actor.stop();
    }
    this.fireEventDescriptor(this.buildEventDescriptor('info_msgSkipped','Message skipped'));
  }

  var msgList = null;
  this.setMsgList = function(aMsgList) // USER API
  {
    if (isString(aMsgList))
    {
      msgList = aMsgList.split(',');
    }
    else
    {
      msgList = aMsgList;
    }
    //directorTrace('VideoMsgConsumer.setMsgList msgList: ' + msgList);
  }

  this.getNbrOfMsgs = function()
  {
    return msgList.length-1;
  }

  function defaultDelCallback(msgIndex)
  {
    this.fireEventDescriptor(this.buildEventDescriptor('info_noDelCallbackRegistered','No delete callback registered. msgIndex ' + msgIndex));
  }
  this.delCallback = defaultDelCallback;
  this.setDelCallback = function(delCallback) // USER API
  {
    this.delCallback = delCallback;
  }

  function callPlay()
  {
    _this.play();
  }

  var lastPlayTicks = 0;
  var playIntervalTicks = 2000;
  var playTimeout = null;
  this.play = function() // USER API
  {
    //directorTrace('VideoMsgConsumer.play');

    var ticks = getTicks();
    if (ticks < (lastPlayTicks + playIntervalTicks))
    {
      directorTrace('VideoMsgConsumer.play tick block');
      return;
    }

    if (nextMsgIndex != -1)
    {
      directorTrace('VideoMsgConsumer.play already playing');
      this.fireEventDescriptor(this.buildEventDescriptor('error_alreadyPlaying','Already playing'));
    }
    else
    {
      if (actor.isPlaying())
      {
	if (playTimeout == null)
	{
	  directorTrace('VideoMsgConsumer.play stop current actor playback');
	  actor.stop();
	  playTimeout = setTimeout(callPlay,1000);
	}
	else
	{
	  directorTrace('VideoMsgConsumer.play stop current actor already requested');
	}
      }
      else
      {
	if (playTimeout != null)
	{
	  directorTrace('VideoMsgConsumer.play clearTimeout');
	  clearTimeout(playTimeout);
	  playTimeout = null;
	}
	directorTrace('VideoMsgConsumer.play playMsgs');
	lastPlayTicks = ticks;
	this.playMsgs();
      }
    }
  }

  var actorConfigured = false;
  var myVmcIndex = -1;
  this.getMyVmcIndex = function()
  {
    return myVmcIndex;
  }
  
  this.playMsgs = function()
  {
    //directorTrace('VideoMsgConsumer.playMsgs');
    if (msgList === null)
    {
      this.fireEventDescriptor(this.buildEventDescriptor('info_msgListEmpty','Message list empty'));
      nextMsgIndex = -1;
      return;
    }

    if (!isArray(msgList))
    {
      this.fireEventDescriptor(this.buildEventDescriptor('error_invalidMsgList','Invalid message list'));
      nextMsgIndex = -1;
      return;
    }
    
    if (!actorConfigured)
    {
      if (myVmcIndex == -1)
      {
	myVmcIndex = VideoMsgConsumer.addConsumer(this);
      }
     this.addEventRule('playing_stopped','VideoMsgConsumer.doPlayingStopped(' + myVmcIndex + ')');
      actor.addDependent(this);
      actorConfigured = true;
    }

    nextMsgIndex = 0;
    this.playNextMsg();
  }

  var nextMsg = "";
  this.playNextMsg = function()
  {
    //directorTrace('VideoMsgConsumer.playNextMsg' + this.toString());
    if (nextMsgIndex > -1)
    {
      if (nextMsgIndex >= msgList.length)
      {
	nextMsgIndex = -1;
	this.fireEventDescriptor(this.buildEventDescriptor('info_allMsgsPlayed','All messages played'));
      }
      else
      {
	nextMsg = msgList[nextMsgIndex];
	this.fireEventDescriptor(this.buildEventDescriptor('info_nextMsg','' + nextMsgIndex + ',' + this.getNbrOfMsgs() + ',' + nextMsg));
	if ((typeof nextMsg != 'undefined') && isString(nextMsg) && (nextMsg.length > 0))
	{
	  //directorTrace('VideoMsgConsumer.playNextMsg nextMsg: ' + nextMsg);
	  //debugger;
	  actor.play(nextMsg);
	}
	else
	{
	  //directorTrace('VideoMsgConsumer.playNextMsg skipping msg.' + ' nextMsgIndex: ' + nextMsgIndex + ' nextMsg: ' + nextMsg);
	  nextMsgIndex++;
	  this.playNextMsg();
	}
      }
    }
  }

  this.playingStopped = function()
  {
    if (nextMsgIndex != -1)
    {
      nextMsgIndex++;
      this.playNextMsg();
    }
  }

  this.erase = function() // USER API
  {
    if ((nextMsgIndex > -1) && (nextMsg.length > 0))
    {
      this.delCallback(nextMsgIndex);
      this.fireEventDescriptor(this.buildEventDescriptor('info_msgErased',nextMsg));
      this.stop();
    }
  }

  this.getActorContextIndex = function()
  {
    return actor.getContextIndex();
  }

  this.getActorName = function()
  {
    return actor.getName();
  }

  this.buildEventDescriptor = function(event,optionalArgs)
  {
    return ContextEventDescriptor.create(event,this.getActorContextIndex(),this.getActorName(),optionalArgs);
  }

  this.isActive = function()
  {
    return nextMsgIndex != -1;
  }

  this.toString = function()
  {
    return ('\n nextMsgIndex   : ' + nextMsgIndex +
	    '\n actorConfigured: ' + actorConfigured +
	    '\n actorName      : ' + this.getActorName() +
	    '\n myVmcIndex     : ' + myVmcIndex +
	    '\n msgList        : ' + msgList);
  }
  
}

VideoMsgConsumer.consumers = null;

VideoMsgConsumer.addConsumer = function(aVmc)
{
  if (VideoMsgConsumer.consumers === null)
  {
    VideoMsgConsumer.consumers = new Array();
  }
  var index = VideoMsgConsumer.consumers.length;
  VideoMsgConsumer.consumers[index] = aVmc;
  return index;
}

VideoMsgConsumer.removeConsumer = function(aVmc)
{
  if (VideoMsgConsumer.consumers !== null)
  {
    var index = aVmc.getMyVmcIndex();
    VideoMsgConsumer.consumers[index] = null;
  }
}

VideoMsgConsumer.doPlayingStopped = function(index)
{
  if (VideoMsgConsumer.consumers !== null)
  {
    var consumer = VideoMsgConsumer.consumers[index];
    if (consumer !== null)
    {
      consumer.playingStopped();
    }
  }
}

