{% extends "default.html" %}

{% set active_page = "mailbox" %}
{% block title %}Mailbox {{ mailbox.email }}{% endblock %}
{% block head %}

  <style>
      div[disabled]
      {
          pointer-events: none;
          opacity: 0.7;
      }
  </style>
{% endblock %}
{% block default_content %}

  <div class="row">
    <div class="col">
      <h1 class="h3">
        {{ mailbox.email }}
        {% if mailbox.verified %}

          <span class="cursor"
                data-toggle="tooltip"
                data-original-title="Mailbox Verified">✅</span>
        {% else %}
          <span class="cursor"
                data-toggle="tooltip"
                data-original-title="Mailbox Not Verified">🚫</span>
        {% endif %}
        {% if mailbox.pgp_enabled() %}

          <span class="cursor"
                data-toggle="tooltip"
                data-original-title="PGP Enabled">🗝</span>
        {% endif %}
      </h1>
      {% if not mailbox.verified %}

        <div class="alert alert-info">
          Mailbox not verified, please check your inbox/spam folder for the verification email.
          <br />
          To receive the verification email again, you can delete and re-add the mailbox.
        </div>
      {% endif %}
      <!-- Change email -->
      <div class="card">
        <form method="post" enctype="multipart/form-data">
          <input type="hidden" name="form-name" value="update-email">
          {{ change_email_form.csrf_token }}
          <div class="card-body">
            <div class="card-title">Change Mailbox Address</div>
            <div class="form-group">
              <label class="form-label">Address</label>
              <!-- Not allow user to change mailbox if there's a pending change -->
              {{ change_email_form.email(class="form-control", value=mailbox.email, readonly=pending_email != None) }}
              {{ render_field_errors(change_email_form.email) }}
              {% if pending_email %}

                <div class="mt-2">
                  <span class="text-danger">Pending change: {{ pending_email }}</span>
                  <a href="{{ url_for('dashboard.cancel_mailbox_change_route', mailbox_id=mailbox.id) }}"
                     class="btn btn-secondary btn-sm">
                    Cancel mailbox change
                  </a>
                </div>
              {% endif %}
            </div>
            <button class="btn btn-primary">Change</button>
          </div>
        </form>
      </div>
      <!-- END Change email -->
      <div class="card">
        <div class="card-body">
          <div class="card-title">
            <div class="d-flex">
              Pretty Good Privacy (PGP)
              {% if mailbox.pgp_finger_print %}

                <form method="post">
                  <input type="hidden" name="form-name" value="toggle-pgp">
                  <label class="custom-switch cursor" style="padding-left: 1rem" data-toggle="tooltip" {% if mailbox.disable_pgp %}
                     title="Enable PGP" {% else %} title="Disable PGP" {% endif %}>
                    <input type="checkbox" class="custom-switch-input" name="pgp-enabled" {{ "" if mailbox.disable_pgp else "checked" }}>
                    <span class="custom-switch-indicator"></span>
                  </label>
                </form>
              {% endif %}
            </div>
            <div class="small-text mt-1">
              By importing your PGP Public Key into SimpleLogin, all emails sent to {{ mailbox.email }} are
              <b>encrypted</b> with your key.
              <br />
              {% if PGP_SIGNER %}All forwarded emails will be signed with <b>{{ PGP_SIGNER }}</b>.{% endif %}
            </div>
          </div>
          {% if not current_user.is_premium() %}

            <div class="alert alert-danger" role="alert">This feature is only available in premium plan.</div>
          {% endif %}
          <form method="post">
            <div class="form-group">
              <label class="form-label">PGP Public Key</label>
              <textarea name="pgp" {% if not current_user.is_premium() %} disabled {% endif %} class="form-control" rows=10 id="pgp-public-key" placeholder="-----BEGIN PGP PUBLIC KEY BLOCK-----">{{ mailbox.pgp_public_key or "" }}</textarea>
            </div>
            <input type="hidden" name="form-name" value="pgp">
            <button class="btn btn-primary" name="action" {% if not current_user.is_premium() %}
               disabled {% endif %} value="save">
              Save
            </button>
            {% if mailbox.pgp_finger_print %}

              <button class="btn btn-danger float-right" name="action" value="remove">Remove</button>
            {% endif %}
          </form>
        </div>
      </div>
      <div class="card" {% if not mailbox.pgp_enabled() %}
         disabled {% endif %}>
        <form method="post">
          <input type="hidden" name="form-name" value="generic-subject">
          <div class="card-body">
            <div class="card-title">
              Hide email subject when PGP is enabled
              <div class="small-text mt-1">
                When PGP is enabled, you can choose to use a <b>generic</b> subject for the forwarded emails.
                The original subject is then added into the email body.
                <br />
                As PGP does not encrypt the email subject and the email subject might contain sensitive information,
                this option will allow a further protection of your email content.
              </div>
            </div>
            <div class="alert alert-info">
              As the email is encrypted, a subject like "Email for you"
              will probably be rejected by your mailbox since it sounds like a spam.
              <br />
              Something like "Encrypted Email" would work much better :).
            </div>
            <div class="form-group">
              <label class="form-label">Generic Subject</label>
              <input name="generic-subject" {% if not mailbox.pgp_enabled() %}
                 disabled {% endif %} class="form-control" maxlength="78" placeholder="Generic Subject" value="{{ mailbox.generic_subject or "" }}">
              </div>
              <button class="btn btn-primary" name="action" {% if not mailbox.pgp_enabled() %}
                 disabled {% endif %} value="save">
                Save
              </button>
              {% if mailbox.generic_subject %}

                <button class="btn btn-danger float-right" name="action" value="remove">Remove</button>
              {% endif %}
            </div>
          </form>
        </div>
        <hr />
        <h2 class="h4">Advanced Options</h2>
        {% if spf_available %}

          <div class="card" id="spf">
            <form method="post">
              <input type="hidden" name="form-name" value="force-spf">
              <div class="card-body">
                <div class="card-title">
                  Enforce SPF
                  <div class="small-text">
                    To avoid email-spoofing, SimpleLogin blocks email that
                    <em data-toggle="tooltip"
                        title="Email that has your mailbox as envelope-sender address">seems</em> to come from your
                    mailbox
                    but sent from <em data-toggle="tooltip"
     title="IP Address that is not known by your mailbox email service">unknown</em>
                    IP address.
                    <br />
                    Only turn off this option if you know what you're doing :).
                  </div>
                </div>
                <label class="custom-switch cursor mt-2 pl-0" data-toggle="tooltip" {% if mailbox.force_spf %}
                   title="Disable SPF enforcement" {% else %} title="Enable SPF enforcement" {% endif %}>
                  <input type="checkbox" name="spf-status" class="custom-switch-input" {{ "checked" if mailbox.force_spf else "" }}>
                  <span class="custom-switch-indicator"></span>
                </label>
              </div>
            </form>
          </div>
        {% endif %}
        <div class="card" id="authorized-address">
          <div class="card-body">
            <div class="card-title">
              Authorized addresses
              <div class="small-text">
                Emails sent from these addresses to a <b>reverse-alias</b> are considered as being sent
                from {{ mailbox.email }}
              </div>
            </div>
            {% if mailbox.authorized_addresses | length == 0 %}

            {% else %}
              <ul>
                {% for authorized_address in mailbox.authorized_addresses %}

                  <li>
                    {{ authorized_address.email }}
                    <form method="post" action="#authorized-address" style="display: inline">
                      <input type="hidden" name="form-name" value="delete-authorized-address">
                      <input type="hidden"
                             name="authorized-address-id"
                             value="{{ authorized_address.id }}">
                      <input type="submit" class="btn btn-sm btn-outline-warning" value="Delete">
                    </form>
                  </li>
                {% endfor %}
              </ul>
            {% endif %}
            <form method="post" action="#authorized-address" class="form-inline">
              <input type="hidden" name="form-name" value="add-authorized-address">
              <input type="email" name="email" size="50" class="form-control">
              <input type="submit" class="btn btn-primary" value="Add">
            </form>
          </div>
        </div>
      </div>
    </div>
  {% endblock %}
  {% block script %}
    <script src="/static/js/utils/drag-drop-into-text.js"></script>
    <script>
    $(".custom-switch-input").change(function (e) {
      $(this).closest("form").submit();
    });
    enableDragDropForPGPKeys('#pgp-public-key');
    </script>
  {% endblock %}
