{% extends "base.html.j2" %}
{% import "materialize/wtf.html.j2" as wtf %}

{% block page_content %}
<div class="container">
  <div class="row">
    <div class="col s12">
      <h1 id="title">{{ title }}</h1>
    </div>

    <div class="col s12">
      
      <div class="card">
        <form method="POST" enctype="multipart/form-data">
          <div class="card-content">
            <div class="row">
              <div class="col s6">
                {{ edit_profile_settings_form.hidden_tag() }}
                {{ wtf.render_field(edit_profile_settings_form.username, material_icon='person') }}
                {{ wtf.render_field(edit_profile_settings_form.email, material_icon='email') }}
              </div>
            </div>
          </div>
          <div class="card-action">
            <div class="right-align">
              {{ wtf.render_field(edit_profile_settings_form.submit, material_icon='send') }}
            </div>
          </div>
        </form>
      </div>

      <form method="POST">
        {{ edit_privacy_settings_form.hidden_tag() }}
        <div class="card">
          <div class="card-content">
            <span class="card-title">Privacy settings</span>
            <br>
            {{ wtf.render_field(edit_privacy_settings_form.is_public, id='public-profile') }}
            <br>
            <hr>
            <br>
            {{ wtf.render_field(edit_privacy_settings_form.show_email, data_action='disable', disabled=true) }}
            <br>
            {{ wtf.render_field(edit_privacy_settings_form.show_last_seen, data_action='disable', disabled=true) }}
            <br>
            {{ wtf.render_field(edit_privacy_settings_form.show_member_since, data_action='disable', disabled=true) }}
            <br>
          </div>
          <div class="card-action">
            <div class="right-align">
              {{ wtf.render_field(edit_privacy_settings_form.submit, material_icon='send') }}
            </div>
          </div>
        </div>
      </form>

      <div class="card">
        <form method="POST" enctype="multipart/form-data">
          <div class="card-content">
            {{ edit_public_profile_information_form.hidden_tag() }}
            <div class="row">
              <div class="col s12">
                <span class="card-title">Public Profile</span>
              </div>
            </div>
            <div class="row">
              <div class="col s5">
                <div class="row">
                  <div class="col s2"></div>
                  <div class="col s8">
                    {% if current_user.avatar %}
                    <img src="{{ url_for('profile.avatar_download', user_id=user.id, avatar_id=current_user.avatar.id) }}" alt="user-image" class="circle responsive-img" id="avatar">
                    {% else %}
                    <img src="{{ url_for('static', filename='images/user_avatar.png') }}" alt="user-image" class="circle responsive-img" id="placeholder-avatar">
                    {% endif %}
                  </div>
                  <div class="col s2"></div>
                </div>
                <div class="row">
                  <div class="col s2">
                    <div class="center-align">
                      <a class="action-button btn-floating red waves-effect waves-light modal-trigger" href="#delete-request" style="margin-top:15px;"><i class="material-icons">delete</i></a>
                    </div>
                  </div>
                  <div class="col s10">
                    {{wtf.render_field(edit_public_profile_information_form.avatar, accept='image/jpeg, image/png, image/gif', placeholder='Choose an image file', id='avatar-upload', data_action='disable')}}
                  </div>
                </div>
              </div>
              <div class="col s7">
                <p></p>
                <br>
                {{ wtf.render_field(edit_public_profile_information_form.full_name, material_icon='badge') }}
                {{ wtf.render_field(edit_public_profile_information_form.about_me, material_icon='description') }}
                {{ wtf.render_field(edit_public_profile_information_form.website, material_icon='laptop') }}
                {{ wtf.render_field(edit_public_profile_information_form.organization, material_icon='business') }}
                {{ wtf.render_field(edit_public_profile_information_form.location, material_icon='location_on') }}
              </div>
            </div>
          </div>
          <div class="card-action">
            <div class="right-align">
              {{ wtf.render_field(edit_public_profile_information_form.submit, material_icon='send') }}
            </div>
          </div>
        </form>
      </div>

    </div>
  </div>
</div>
{% endblock page_content %}

{% block modals %}
<div class="modal" id="delete-request">
  <div class="modal-content">
    <h4>Confirm Avatar deletion</h4>
    <p>Do you really want to delete your Avatar?</p>
  </div>
  <div class="modal-footer">
    <a class="action-button btn modal-close waves-effect waves-light" data-action="cancel">Cancel</a>
    <a class="action-button btn modal-close red waves-effect waves-light" data-action="delete">Delete</a>
  </div>
</div>
{% endblock modals %}

{% block scripts %}
{{ super() }}
<script>
let publicProfile = document.querySelector('#public-profile');
let disableButtons = document.querySelectorAll('[data-action="disable"]');
let deleteButton = document.querySelector('[data-action="delete"]');
let cancelButton = document.querySelector('[data-action="cancel"]');
let deleteRequestModal = document.querySelector('#delete-request');
let avatar = document.querySelector('#avatar');
let placeholderAvatar = document.querySelector('#placeholder-avatar');
let avatarUpload = document.querySelector('#avatar-upload');

for (let i = 0; i < disableButtons.length; i++) {
  disableButtons[i].disabled = !publicProfile.checked;
}

publicProfile.addEventListener('change', function() {
  if (publicProfile.checked) {
    for (let i = 0; i < disableButtons.length; i++) {
      disableButtons[i].disabled = false;
    }

  } else {
    for (let i = 0; i < disableButtons.length; i++) {
      disableButtons[i].checked = false;
      disableButtons[i].disabled = true;
    }
  }
});

avatarUpload.addEventListener('change', function() {
  let file = this.files[0];
  if (avatar){
    avatar.src = URL.createObjectURL(file);
  } else {
    placeholderAvatar.src = URL.createObjectURL(file);
  }
});

deleteButton.addEventListener('click', function() {
  return new Promise((resolve, reject) => {
    let user_id = "{{ current_user.hashid }}";
    let avatar_id = "{{ current_user.avatar.hashid }}";

    fetch(`/profile/${user_id}/avatars/${avatar_id}`, {method: 'DELETE', headers: {Accept: 'application/json'}})
      .then(
        (response) => {
          if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);}
          if (response.status === 404) {app.flash('Not Found', 'error'); reject(response);}
          app.flash(`Avatar marked for deletion`, 'corpus');
          resolve(response);
        },
        (response) => {
          app.flash('Something went wrong', 'error');
          reject(response);
        }
      );
    avatar.src = "{{ url_for('static', filename='images/user_avatar.png') }}";
  });
});

cancelButton.addEventListener('click', function() {
  let modal = M.Modal.getInstance(deleteRequestModal);
  modal.close();
});

</script>
{% endblock scripts %}
