{% extends 'base.html' %}

{% block title %}{{ _('Your Profile') }} - MetaBrainz Foundation{% endblock %}

{% block content %}
  <h1 class="page-title">{{ _('Your Profile') }}</h1>

  <p>
    <strong>Type:</strong>
    {{ 'Commercial' if current_user.is_commercial else 'Non-commercial' }}
    <br />
    {% if current_user.is_commercial %}
      <strong>Tier:</strong> {{ current_user.tier.name }}<br />
    {% endif %}
    <strong>State:</strong>
    <span class="{{ 'text-success' if current_user.state == 'active'   else
                    'text-danger'  if current_user.state == 'rejected' else
                    'text-primary' if current_user.state == 'pending'  else
                    'text-warning' }}">
      {{ current_user.state|upper }}
    </span>
  </p>

  {% if not current_user.is_commercial %}
     <p>
       NOTE: If you would like to change your account from non-commercial to commercial, please
       <a href="{{ url_for("index.contact") }}">contact us</a>.
     </p>
  {% endif %}

  <div class="row">
    {% if current_user.is_commercial %}
      <div class="col-md-7">
        <h3>{{ _('Organization information') }}</h3>
        <p>
          <strong>{{ _('Name:') }}</strong> {{ current_user.org_name or
                                    '<em class="text-muted">Unspecified</em>'|safe }}<br />
          <strong>{{ _('Website URL:') }}</strong> {{ current_user.website_url or
                                           '<em class="text-muted">Unspecified</em>'|safe }}<br />
          <strong>{{ _('API URL:') }}</strong> {{ current_user.api_url or
                                       '<em class="text-muted">Unspecified</em>'|safe }}<br />
        </p>
        <p>
          {{ _('Please <a href="%(contact_url)s">contact us</a>
          if you wish for us to update this information.', contact_url=url_for('index.contact')) }}
        </p>
      </div>
    {% endif %}

    <div class="col-md-12">
      <h3>{{ _('Contact information') }}</h3>
      <p>
        <strong>{{ _('Name:') }}</strong> {{ current_user.contact_name }}<br />
        <strong>{{ _('Email:') }}</strong> {{ current_user.contact_email }}<br />
        {% if not current_user.is_commercial %}
          <strong>{{ _('Datasets used:') }}</strong>
          {% if current_user.datasets %}
            {{ current_user.datasets|map(attribute="name")|join(", ") }}
          {% endif %}
          <br/>
        {% endif %}
      </p>
      <p>
        <a href="{{ url_for('supporters.profile_edit') }}" class="btn btn-lg btn-primary">
          {% if current_user.is_commercial %}
             Edit contact information
          {% else %}
             Edit datasets/contact information
          {% endif %}
        </a>
      </p>
    </div>
  </div>

  {% if current_user.is_commercial %}
    {% if current_user.state in ('active', 'limited') and current_user.good_standing %}
      <h3>{{ _('Data use permission granted') }}</h3>
        <div style="font-size: 13pt">
        <p>
          <b>Your support agreement has been completed -- thank you!</b>
        </p>
        <p>
          {{ _('You have permission to use any of the data published by the MetaBrainz
          Foundation. This includes data dumps released under a Creative Commons
          non-commercial license. Thank you for your support!') }}
        </p>
        <p>
          {{ _('Note 1: If your support falls behind by more than 60 days, this
          permission may be withdrawn. You can always check your current permission
          status on this page.') }}
        </p>
        <p>
          {{ _('Note 2: The IP addresses from which replication packets for the Live Data Feed are downloaded are logged.') }}
        </p>
      </div>
    {% else %}
      <h3>Limited/no data use permission granted</h3>
      <div style="font-size: 13pt">
        {% if current_user.state == 'rejected' %}
          <p>
            <b>Your application for using the Live Data Feed has been rejected.</b>
          </p>
          <p>
            You do not have permission to use our data in a public commercial product.
          </p>
        {% elif current_user.state == 'pending' %}
          <p>
            <b>Your application for using the Live Data Feed is still pending.</b>
          </p>
          <p>
            You may use our data and APIs for evaluation/development purposes
            while your application is pending.
          </p>
        {% elif current_user.state == 'waiting' %}
          <p>
            <b>Your application for using the Live Data Feed is waiting to finalize our support agreement.</b>
          </p>
          <p>
            You may use our data and APIs for evaluation and development purposes, but you may not use the data in a public
            commercial product. Once you are nearing the public release of a product that contains our data, please
            <a href="/contact">contact us</a> again to finalize our support agreement.
          </p>
        {% elif not current_user.good_standing %}
          <p>
            <b>Your use of the Live Data Feed is pending suspension.</b>
          </p>
          <p>
            Your account is in bad standing, which means that you are more than 60 days behind in support payments. If you think
            this is a mistake, please <a href="/contact">contact us</a>.
          </p>
        {% else %}
          {{ _('Unknown. :(') }}
        {% endif %}
      </div>
    {% endif %}
  {% endif %}

  {% if current_user.state in ('active', 'limited') and current_user.good_standing %}
    <h2>{{ _('Data Download') }}</h2>

    <div style="font-size: 13pt">
      {% if not current_user.is_commercial %}
        <p>
           Thank you for registering with us -- we really appreciate it!
        </p>
      {% endif %}

      <p>
        Please proceed to our download page to download our datasets:
      </p>

      <p>
        <a href="{{ url_for("index.download") }}" class="btn btn-lg btn-primary">Download Datasets</a>
      </p>
    </div>
  {% endif %}

  {% if current_user.state == 'active' %}
    <h2>{{ _('Live Data Feed Access token') }}</h2>
    <p style="font-style: italic; color: #444">
      {{ _('This access token should be considered private. Don\'t check this token
      into any publicly visible version control systems and similar places.
      If the token has been exposed, you should immediately generate a new one!
      When you generate a new token, your current token is revoked.') }}
    </p>
    <div class="dataset-summary">
      <p>
       {% if  current_user.token %}
          <div id="token" style="font-size:14pt; padding: .5em; color: #AA0000">{{ current_user.token.value }}</div>
       {% else %}
          <div id="token" style="font-size:14pt; padding: .5em; color: #AA0000">[ there is no valid token currently ]</div>
       {% endif %}
      </p>
      <p>
        <a id="btn-generate-token" class="btn btn-default" href="#" role="button">{{ _('Generate new token') }}</a>
      </p>
    </div>
    <p>{{ _('See the <a href="%(api_docs_url)s">API documentation</a> for more information.', api_docs_url=url_for('api_index.info')) }}</p>

  {% endif %}
{% endblock %}

{% block scripts %}
  {{ super() }}
  {% if current_user.state == 'active' %}
    <script>
      $(document).ready(function () {
        var token = $("#token");
        {# Not showing confirmation dialog if there's no active token. #}
        var ignoreConfirmation = {{ 'false' if current_user.token else 'true' }};

        $("#btn-generate-token").click(function () {
          if (ignoreConfirmation || confirm("{{ _('Are you sure you want to generate new access token? Current token will be revoked!') }}")) {
            $.ajax({
              type: "POST",
              url: "{{ url_for('supporters.regenerate_token') }}",
              success: function (data) {
                token.html(data.token);
                token.show();
                ignoreConfirmation = false;
              },
              error: function (jqXHR, textStatus, errorThrown) {
                var msg = "{{ _('Failed to generate new access token!') }}";
                if (jqXHR.status == 429) {
                  msg += '\n' + JSON.parse(jqXHR.responseText).error
                }
                alert(msg);
              }
            });
          }
        });
      });
    </script>
  {% endif %}
{% endblock %}
