/**
 * Recording Manager viewer
 * Although multiple instances could be made, only one should be
 */
(function (context, $) {

  "use strict";

  context.JK = context.JK || {};

  context.JK.RecordingManager = function (app) {

    var $parentElement = $('#recording-manager-viewer');

    var logger = context.JK.logger;
    var EVENTS = context.JK.EVENTS;

    if ($parentElement.length == 0) {
      logger.debug("no $parentElement specified in RecordingManager");
    }

    var $downloadCommand = $('#recording-manager-download', $parentElement);
    var $downloadPercent = $('#recording-manager-download .percent', $parentElement);
    var $uploadCommand = $('#recording-manager-upload', $parentElement);
    var $uploadPercent = $('#recording-manager-upload .percent', $parentElement);
    var $convertCommand = $('#recording-manager-convert', $parentElement);
    var $convertPercent = $('#recording-manager-convert .percent', $parentElement);
    var $deleteCommand = $('#recording-manager-delete', $parentElement);
    var $deletePercent = $('#recording-manager-delete .percent', $parentElement);
    var $fileManager = $('#recording-manager-launcher', $parentElement);

    if($fileManager.length == 0) {throw "no file manager element";  }

    $downloadCommand.data('command-type', 'download')
    $uploadCommand.data('command-type', 'upload')
    $convertCommand.data('command-type', 'convert')
    $deleteCommand.data('command-type', 'delete')

    // keys come from backend
    var lookup = {
      SyncDownload: { command: $downloadCommand, percent: $downloadPercent},
      SyncUpload:   { command: $uploadCommand,   percent: $uploadPercent},
      SyncConvert:  { command: $convertCommand,  percent: $convertPercent},
      SyncDelete:  { command: $deleteCommand,  percent: $deletePercent}
    }

    var $self = $(this);

    if (context.jamClient.IsNativeClient()) {
      $parentElement.addClass('native-client')
    }

    function renderStartCommand($command) {
      $command.css('visibility', 'visible').addClass('running');
      $parentElement.addClass('running')
      $(document).triggerHandler(EVENTS.FILE_MANAGER_CMD_START, $command.data())
    }

    function renderEndCommand($command) {
      $command.css('visibility', 'hidden').removeClass('running')
      if ($parentElement.find('.recording-manager-command.running').length == 0) {
        $parentElement.removeClass('running')
      }
      $(document).triggerHandler(EVENTS.FILE_MANAGER_CMD_STOP, $command.data())
    }

    function renderPercentage($command, $percent, value) {
      var percentage = Math.round(value * 100)
      if(percentage > 100) percentage = 100;

      $percent.text(percentage);
      $command.data('percentage', percentage)
      $(document).triggerHandler(EVENTS.FILE_MANAGER_CMD_PROGRESS, $command.data())
    }

    function onStartCommand(id, type, metadata) {
      var command = lookup[type];
      if (!command) {
        return
      }

      var existingCommandId = command.command.data('command-id');

      if (existingCommandId && existingCommandId != id) {
        renderEndCommand(command.command);
      }

      command.command.data('command-id', id)
      command.command.data('command-metadata', metadata)
      command.command.data('command-success', null);
      command.command.data('command-reason', null);
      command.command.data('command-detail', null);

      renderStartCommand(command.command);
      renderPercentage(command.command, command.percent, 0);
    }

    function onStopCommand(id, type, success, reason, detail) {
      var command = lookup[type];
      if (!command) {
        return
      }

      var existingCommandId = command.command.data('command-id');


      if (!existingCommandId) {
        command.command.data('command-id', id);
        renderStartCommand(command.command);
      }
      else if (existingCommandId && existingCommandId != id) {
        renderEndCommand(command.command);
        command.command.data('command-id', id);
        renderStartCommand(command.command);
      }


      command.command.data('command-success', success);
      command.command.data('command-reason', reason);
      command.command.data('command-detail', detail);

      renderPercentage(command.command, command.percent, 1);
      renderEndCommand(command.command);
      command.command.data('command-id', null);
    }

    function onCommandProgress(id, type, progress) {
      var command = lookup[type];
      if (!command) {
        return
      }

      var existingCommandId = command.command.data('command-id');

      if (!existingCommandId) {
        command.command.data('command-id', id);
        renderStartCommand(command.command);
      }
      else if (existingCommandId && existingCommandId != id) {
        renderEndCommand(command.command);
        command.command.data('command-id', id);
        renderStartCommand(command.command);
      }

      renderPercentage(command.command, command.percent, progress);
    }

    function onCommandsChanged(id, type) {

    }

    function onAsapCommandStatus(id, type, metadata, reason) {
      $(document).triggerHandler(EVENTS.FILE_MANAGER_CMD_ASAP_UPDATE, {commandMetadata: metadata, commandId: id, commandReason: reason})
    }

    function onClick() {
      app.layout.showDialog('all-syncs-dialog')
      return false;
    }

    $fileManager.click(onClick)
    $convertCommand.click(onClick)
    $downloadCommand.click(onClick)
    $uploadCommand.click(onClick)


    context.JK.RecordingManagerCommandStart = onStartCommand;
    context.JK.RecordingManagerCommandStop = onStopCommand;
    context.JK.RecordingManagerCommandProgress = onCommandProgress;
    context.JK.RecordingManagerCommandsChanged = onCommandsChanged;
    context.JK.RecordingManagerAsapCommandStatus = onAsapCommandStatus;

    context.jamClient.RegisterRecordingManagerCallbacks(
      "JK.RecordingManagerCommandStart",
      "JK.RecordingManagerCommandProgress",
      "JK.RecordingManagerCommandStop",
      "JK.RecordingManagerCommandsChanged",
      "JK.RecordingManagerAsapCommandStatus"
    )

    return this;
  }
})(window, jQuery);