{% extends 'iconolab_base.html' %}

{% load staticfiles %}
{% load thumbnail %}
{% load iconolab_tags %}

{% block content_fluid %}

<div class="annotation-navigator">
  <div class="annotation-navigator-list">
    <div class="panel panel-default">
      {% if image.annotations.exists %}
        <div class="list-group">
        {% for annotation in image.latest_annotations %}
          <a href="#{{ annotation.annotation_guid }}" class="list-group-item"
            data-annotation-id="{{ annotation.annotation_guid }}"
            data-revision-id="{{ annotation.current_revision.revision_guid }}">
            <h3 class="small list-group-item-heading">
              {{ annotation.current_revision.title }}
              <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </h3>
            {% for tagging_info in annotation.current_revision.tagginginfo_set.all %}
            <p class="list-group-item-text">{{ tagging_info.tag.label }}</p>
            {% endfor %}
            <div class="list-group-item-footer">
              <i class="fa fa-comment"></i>
              {{ annotation.stats.comments_count }} commentaire{% if annotation.stats.comments_count > 1 %}s{% endif %}
            </div>
          </a>
        {% endfor %}
        </div>
      {% else %}
        <div class="panel-body">
          <div class="alert alert-warning text-center">Pas d'annotation pour cette image</div>
        </div>
      {% endif %}
    </div>
  </div>
  <div class="annotation-navigator-canvas">

    <image-annotator v-bind:annotation="annotation"
      ref="annotator"
      image="{% with image.media as img %}{{ img.url }}{% endwith %}"
      thumbnail="{% thumbnail image.media '100x100' crop=False as thumb %}{{ thumb.url }}{% endthumbnail %}"></image-annotator>

    <form id="form-annotation" action="{% url 'annotation_create' collection_name image.image_guid %}" method="POST">
      {% csrf_token %}
      <input type="hidden" name="{{ form.title.name }}">
      <input type="hidden" name="{{ form.description.name }}">
      <input type="hidden" name="{{ form.fragment.name }}">
      <input type="hidden" name="{{ form.tags.name }}">
    </form>
  </div>
  <div class="annotation-navigator-panel">
    <div class="panel panel-default">
      <div class="panel-body">
        <annotation-form v-bind:annotation="annotation"
          action="{% url 'annotation_edit' collection_name image_guid ':annotation_guid' %}">
          {% csrf_token %}
        </annotation-form>
        <comment-list v-bind:annotation="annotation"
          fetch="{% url 'get_annotation_comments_json' ':annotation_guid' %}"></comment-list>
        <div id="form-comment">
          <form>
              <div class="form-group">
                <textarea class="form-control input-sm" placeholder="Ajouter mon commentaire..." disabled></textarea>
              </div>
              <button type="submit" class="btn btn-block btn-sm btn-primary" disabled>Commenter</button>
          </form>
        </div>
      </div>
    </div>
  </div>

</div>
{% endblock %}

{% block footer_js %}
<script>

  var currentPath = "{{ request.path }}";
  var getCommentFormURL = "{% url 'get_comment_form' ':annotation_guid' %}"

  var vm = new Vue({
    el: '.annotation-navigator',
    data: function() {
      return {
        annotation: null
      };
    }
  });

  var annotations = {};
  {% if image.annotations %}
    {% for annotation in image.annotations.all %}
  annotations["{{ annotation.current_revision.revision_guid }}"] = {{ annotation.current_revision|json|safe }}
    {% endfor %}
  {% endif %}

  function displayAnnotation($el) {

    var annotation = $el.data('annotation-id');
    var revision = $el.data('revision-id');

    // Load the form for selected annotation via AJAX
    $.get(getCommentFormURL.replace(':annotation_guid', annotation), { next: currentPath + '#' + annotation })
      .then(function(form) {

        $('.list-group a[data-annotation-id]').removeClass('active');
        $el.addClass('active');

        $('#form-comment form').replaceWith(form);

        vm.annotation = annotations[revision];

        location.hash = '#' + annotation;

      });
  }

  $('.list-group a[data-annotation-id]').on('click', function(e) {
    e.preventDefault();
    var $el = $(e.currentTarget);
    displayAnnotation($el);
  });

  vm.$refs.annotator.$on('save', function(data) {
    $('#form-annotation').find('input[name="title"]').val(data.title);
    $('#form-annotation').find('input[name="description"]').val(data.description);
    $('#form-annotation').find('input[name="fragment"]').val(data.fragment);

    var tags = data.tags.map(function(tag) {
      return {
        tag_input: (typeof tag.tag_link === "string" && tag.tag_link.length) ? tag.tag_link : tag.tag_label,
        tag_label: tag.tag_label,
        accuracy: tag.accuracy,
        relevancy: tag.relevancy
      }
    })
    $('#form-annotation').find('input[name="tags"]').val(JSON.stringify(tags));
    $('#form-annotation').submit();
  })

  $('.list-group a[data-annotation-id] .close').on('click', function(e) {
    e.preventDefault();
    e.stopPropagation();

    var $target = $(e.currentTarget);
    $target.closest('.list-group-item').removeClass('active');

    vm.annotation = null;

    $('#form-comment form').find('textarea, [type="submit"]').attr('disabled', true);

    location.hash = '';
  });

  function scrollTo($container, $el) {
    $container.animate({
      scrollTop:  $container.scrollTop() - $container.offset().top + $el.offset().top
    }, 600);
  }

  function router () {
    var url = location.hash.slice(1);
    if (url.length) {
      var annotation = url;
      var $el = $('.list-group a[data-annotation-id="'+annotation+'"]');
      displayAnnotation($el);
      scrollTo($el.closest('.panel'), $el);
    }
  }
  // window.addEventListener('hashchange', router);
  window.addEventListener('load', router);

</script>
{% endblock %}

