<div class="repository-events-table-element">
  <div class="co-panel">
    <div class="co-panel-heading">
      <i class="fa fa-bell"></i> Events and Notifications

      <div class="heading-controls hidden-sm hidden-xs">
        <a href="/repository/{{ repository.namespace }}/{{ repository.name }}/create-notification"
           class="btn btn-primary" ng-show="canCreateNotification()">
           <span class="create-notification-btn"><i class="fa fa-plus"></i> Create Notification</span>
        </a>
      </div>
    </div>
    <div class="panel-body">
      <div class="resource-view" resource="notificationsResource"
           error-message="'Could not load repository events'">

          <div class="empty" ng-if="!notifications.length">
            <div class="empty-primary-msg">No notifications have been setup for this repository.</div>
            <div class="empty-secondary-msg hidden-sm hidden-xs" ng-if="canCreateNotification()">
              Click the "Create Notification" button above to add a new notification for a repository event.
            </div>
            <div class="empty-secondary-msg visible-sm visible-xs" ng-if="repository.can_write">
              <a href="/repository/{{ repository.namespace }}/{{ repository.name }}/create-notification">Click here</a> to add a new notification for a repository event.
            </div>
          </div>

          <table class="co-table permissions" ng-if="notifications.length">
            <thead>
              <tr>
                <td>Title</td>
                <td>Event</td>
                <td>Notification</td>
                <td>Enabled</td>
                <td class="options-col"></td>
              </tr>
            </thead>

            <tbody>
              <tr class="notification-row" ng-repeat="notification in notifications">
                <td>
                  {{ notification.title || '(Untitled)' }}
                </td>

                <td>
                  <span class="notification-event">
                    <i class="fa fa-lg" ng-class="getEventInfo(notification).icon"></i>
                    {{ getEventInfo(notification).title }}
                  </span>

                  <ul class="notification-event-fields" ng-if="getEventInfo(notification).fields.length">
                    <li ng-repeat="field in getEventInfo(notification).fields" ng-if="notification.event_config[field.name]">
                      {{ field.title }}:
                      <span ng-switch on="field.type">
                        <span ng-switch-when="enum">
                         {{ findEnumValue(field.values, notification.event_config[field.name]).title }}
                        </span>
                        <code ng-switch-when="regex">
                          {{ notification.event_config[field.name] }}
                        </code>
                        <span ng-switch-default>
                        {{ notification.event_config[field.name] }}
                        </span>
                      </span>
                    </li>
                  </ul>
                </td>

                <td>
                  <span class="notification-method">
                    <i class="fa fa-lg" ng-class="getMethodInfo(notification).icon"></i>
                    {{ getMethodInfo(notification).title }}
                  </span>
                  <ul class="notification-event-fields" ng-if="getMethodInfo(notification).fields.length">
                    <li ng-repeat="field in getMethodInfo(notification).fields" ng-if="notification.config[field.name]">
                      {{ field.title }}:
                      <span ng-switch on="field.type">
                        <span ng-switch-when="enum">
                          {{ findEnumValue(field.values, notification.config[field.name]).title }}
                        </span>
                        <code ng-switch-when="regex">
                          {{ notification.config[field.name] }}
                        </code>
                        <code ng-switch-when="entity">
                          {{ notification.config[field.name].name }}
                        </code>
                        <code ng-switch-when="url">
                          {{ notification.config[field.name] }}
                        </code>
                        <textarea class="form-control" ng-switch-when="template" rows="3" readonly>{{ notification.config[field.name] }}</textarea>
                        <span ng-switch-default>
                        {{ notification.config[field.name] }}
                        </span>
                      </span>
                    </li>
                  </ul>
                </td>

                <td>
                  <span ng-if="notification.number_of_failures >= 3">Disabled due to 3 failed attempts in a row</span>
                  <span ng-if="notification.number_of_failures < 3">Enabled</span>
                </td>

                <td>
                  <span class="cor-options-menu" ng-show="canCreateNotification()">
                    <span class="cor-option" option-click="testNotification(notification)">
                      <i class="fa fa-send"></i> Test Notification
                    </span>
                    <span class="cor-option" option-click="showNotifyInfo(notification, 'url')"
                          ng-if="getMethodInfo(notification).id == 'webhook'">
                        <i class="fa fa-link"></i>
                        View Webhook URL
                    </span>
                    <span class="cor-option" option-click="showNotifyInfo(notification, 'email')"
                          ng-if="getMethodInfo(notification).id == 'email'">
                        <i class="fa fa-envelope"></i>
                        View E-mail Address
                    </span>
                    <span class="cor-option" option-click="showWebhookInfo(notification)"
                          ng-if="getMethodInfo(notification).id == 'webhook'">
                        <i class="fa fa-book"></i>
                        Webhook Documentation
                    </span>
                    <span class="cor-option" option-click="deleteNotification(notification)">
                      <i class="fa fa-times"></i> Delete Notification
                    </span>
                    <span ng-if="notification.number_of_failures >= 3" class="cor-option" option-click="reenableNotification(notification)">
                      <i class="fa fa-adjust"></i> Re-enable Notification
                    </span>
                  </span>
                </td>
              </tr>
            </tbody>
          </table>
      </div>
    </div>
  </div>
</div>
