<%= render "menu" -%>

<div class="swagger-section">
  <div id="message-bar" class="swagger-ui-wrap">&nbsp;</div>
  <div id="swagger-ui-container" class="swagger-ui-wrap"></div>
</div>

<div class='apidocs-param-tips apidocs-signin-message' style='display:none;'>
  <p><a href='#'>Sign in</a> to your account for quick access to useful values.</p>
</div>

<%# TODO: this is here until we get swagger-ui 2.1 to load swagger spec 1.2 and 2.0 %>
<% if @api_docs_service.specification.swagger_2_0? -%>

  <% content_for :javascripts do -%>
    <%= javascript_include_tag "swagger-ui2" -%>
    <%= javascript_include_tag "swagger-ui2/api_docs_proxy" %>
    <%= javascript_include_tag "swagger-ui2/autocomplete" %>

    <%= stylesheet_link_tag    "swagger-ui2/typography.css" %>
    <%= stylesheet_link_tag    "swagger-ui2/screen.css" -%>

    <%= stylesheet_link_tag    "swagger-ui/threescale" -%>

    <style type="text/css">
      div.swagger-section {
        font-size:110%;
      }
      div.swagger-section pre, div.swagger-section code {
        font-size: 1em;
      }
    </style>
  <% end -%>

  <script type="text/javascript">

    function log() {
      if ('console' in window) {
        console.log.apply(console, arguments);
      }
    }

    $(function () {
      var url = "<%= admin_api_docs_service_path(@api_docs_service, format: :json)-%>";

      window.swaggerUi = new SwaggerUi({
        url: url,
        validatorUrl: null,
        dom_id: "swagger-ui-container",
        supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
        onComplete: function(swaggerApi, swaggerUi) {
          $('#swagger-ui-container pre code').each(function(i, e) {
            hljs.highlightBlock(e)
          });
        },
        onFailure: function(data) {
          log("Unable to Load SwaggerUI.");
        },
        docExpansion: "list",
        transport: function(httpClient, obj) {
          log("[swagger-ui]>>> custom transport.");
          return ApiDocsProxy.execute(httpClient, obj);
        }
      });

      window.swaggerUi.load('provider');

    });
  </script>

<% else -%>

  <% content_for :javascripts do -%>
    <%= javascript_include_tag "swagger-ui" -%>
    <%= javascript_include_tag "swagger-ui/threescale" -%>
    <%= javascript_include_tag "swagger-ui/extensions" -%>
    <%= stylesheet_link_tag    "swagger-ui/screen.css" -%>
    <%= stylesheet_link_tag    "swagger-ui/threescale" -%>

    <style type="text/css">
      div.swagger-section {
        font-size:110%;
      }
    </style>
  <% end -%>

  <%# don't do this at home. %>
  <script id="swagger-notifications" type="text/x-handlebars-template">
    <div class="swagger-notifications">
      <h3>Notifications</h3>
      <ul>
        {{#each __notifications}}
          <li>{{this}}</li>
        {{/each}}
      </ul>
    </div>
  </script>

  <script type="text/javascript">
    $(function () {
      var url = "<%= preview_admin_api_docs_service_path(@api_docs_service, format: :json)-%>";
      window.swaggerUi = new SwaggerUi({
        url: url,
        dom_id: "swagger-ui-container",
        supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
        onComplete: function(swaggerApi, swaggerUi) {
          var source   = $("#swagger-notifications").html();
          var template = Handlebars.compile(source);
          var html = _.reduce( swaggerApi.apis, function(memo, api) { return memo + template(api); }, "" );
          $("#message-bar").append(html);
        },
        onFailure: function(data) {
          log("Unable to Load SwaggerUI");
        },
        docExpansion: "list"
      });
      window.swaggerUi.load("provider");
    });
  </script>
<% end -%>

