/**
 * Service which defines the various kinds of external notification and provides methods for
 * easily looking up information about those kinds.
 */
angular.module('quay').factory('ExternalNotificationData', ['Config', 'Features',

function(Config, Features) {
  var externalNotificationData = {};

  var events = [
    {
      'id': 'repo_push',
      'title': 'Push to Repository',
      'icon': 'fa-upload'
    }
  ];

  if (Features.BUILD_SUPPORT) {
    var buildEvents = [
      {
        'id': 'build_queued',
        'title': 'Dockerfile Build Queued',
        'icon': 'fa-tasks'
      },
      {
        'id': 'build_start',
        'title': 'Dockerfile Build Started',
        'icon': 'fa-circle-o-notch'
      },
      {
        'id': 'build_success',
        'title': 'Dockerfile Build Successfully Completed',
        'icon': 'fa-check-circle-o'
      },
      {
        'id': 'build_failure',
        'title': 'Dockerfile Build Failed',
        'icon': 'fa-times-circle-o'
      }];

    for (var i = 0; i < buildEvents.length; ++i) {
      events.push(buildEvents[i]);
    }
  }

  var methods = [
    {
      'id': 'quay_notification',
      'title': Config.REGISTRY_TITLE + ' Notification',
      'icon': 'quay-icon',
      'fields': [
        {
          'name': 'target',
          'type': 'entity',
          'title': 'Recipient'
        }
      ]
    },
    {
      'id': 'email',
      'title': 'E-mail',
      'icon': 'fa-envelope',
      'fields': [
        {
          'name': 'email',
          'type': 'email',
          'title': 'E-mail address'
        }
      ],
      'enabled': Features.MAILING
    },
    {
      'id': 'webhook',
      'title': 'Webhook POST',
      'icon': 'fa-link',
      'fields': [
        {
          'name': 'url',
          'type': 'url',
          'title': 'Webhook URL'
        }
      ]
    },
    {
      'id': 'flowdock',
      'title': 'Flowdock Team Notification',
      'icon': 'flowdock-icon',
      'fields': [
        {
          'name': 'flow_api_token',
          'type': 'string',
          'title': 'Flow API Token',
          'help_url': 'https://www.flowdock.com/account/tokens'
        }
      ]
    },
    {
      'id': 'hipchat',
      'title': 'HipChat Room Notification',
      'icon': 'hipchat-icon',
      'fields': [
        {
          'name': 'room_id',
          'type': 'regex',
          'title': 'Room ID #',
          'regex': '^[0-9]+$',
          'help_url': 'https://hipchat.com/admin/rooms',
          'regex_fail_message': 'We require the HipChat room <b>number</b>, not name.'
        },
        {
          'name': 'notification_token',
          'type': 'string',
          'title': 'Room Notification Token',
          'help_url': 'https://hipchat.com/rooms/tokens/{room_id}'
        }
      ]
    },
    {
      'id': 'slack',
      'title': 'Slack Room Notification',
      'icon': 'slack-icon',
      'fields': [
        {
          'name': 'url',
          'type': 'regex',
          'title': 'Webhook URL',
          'regex': '^https://hooks\\.slack\\.com/services/[A-Z0-9]+/[A-Z0-9]+/[a-zA-Z0-9]+$',
          'help_url': 'https://slack.com/services/new/incoming-webhook',
          'placeholder': 'https://hooks.slack.com/service/{some}/{token}/{here}'
        }
      ]
    }
  ];

  var methodMap = {};
  var eventMap = {};

  for (var i = 0; i < methods.length; ++i) {
    methodMap[methods[i].id] = methods[i];
  }

  for (var i = 0; i < events.length; ++i) {
    eventMap[events[i].id] = events[i];
  }

  externalNotificationData.getSupportedEvents = function() {
    return events;
  };

  externalNotificationData.getSupportedMethods = function() {
    var filtered = [];
    for (var i = 0; i < methods.length; ++i) {
      if (methods[i].enabled !== false) {
        filtered.push(methods[i]);
      }
    }
    return filtered;
  };

  externalNotificationData.getEventInfo = function(event) {
    return eventMap[event];
  };

  externalNotificationData.getMethodInfo = function(method) {
    return methodMap[method];
  };

  return externalNotificationData;
}]);