{% if title is not empty %}
<div class="col-12">
  <h1 class="text-center mb-5">{{ title }}</h1>
</div>
{% endif %}

<div hidden>
  {{ tslikeform|raw }}
  {{ tsdislikeform|raw }}
</div>

<div id="stackCompleted" class="alert alert-info" role="alert" style="display: none;">
  <h5><i class="fas fa-info-circle"></i>&nbsp;Hinweis</h5>
  <p>
      {{textComplete}}
  </p>
</div>

<div class="swiping">
  <div class="swiping--status">
    <i class="{{dislikeIcon}}" style="color: {{dislikeIconColor}};"></i>
    <i class="{{likeIcon}}" style="color: {{likeIconColor}};"></i>
  </div>

  <div class="swiping--cards">
    {% for card in cardArray %}
      {% if card.imgMode is not empty%}
        {% if card.imgMode == 0 %}
          <div class="swiping--card disabled" style='background-image: url("{{card.imgSrc}}"); background-size: cover;'>
        {% else %}
          <div class="swiping--card disabled" style='background-color: {{card.bgColor}}'>
        {% endif %}
      {% else %}
        <div class="swiping--card disabled" style='background-color: {{card.bgColor}}'>
      {% endif %}
      <div class="m-auto">
        {% if card.imgMode == 1 %}
          <img src="{{card.imgSrc}}">
        {% endif %}
        <div class="text-content">
          <h3 style='color: {{card.titleColor}}'>
            {{card.title}}
          </h3>
          <p class="cardContent" style='color: {{card.contentColor}}'>
            {{card.content}}
          </p>
        </div>
      </div>
    </div>
    {% endfor %}
  </div>
</div>

<div class="swiping--buttons">
  {% if showRepeatBtn %}
  <button style="display: none; background-color: {{repeatBtnColor}};" id="repeat-swiping" ><i class="fas fa-sync" style="color: {{repeatIconColor}};"></i></button>
  {% endif %}
  {% if showUndoBtn %}
  <button style="display: none; background-color: {{undoBtnColor}};" id="card-back" ><i class="fas fa-undo" style="color: {{undoIconColor}};"></i></button>
  {% endif %}
  <button id="nope" style="background-color: {{dislikeBtnColor}}"><i class="{{dislikeIcon}}"
      style="color: {{dislikeIconColor}};"></i></button>
  <button id="love" style="background: {{likeBtnColor}}"><i class="{{likeIcon}}"
      style="color: {{likeIconColor}};"></i></button>
</div>


<style>
    *, *:before, *:after {
  box-sizing: border-box;
  margin: 0px;
}

.swiping {
  height: 600px;
  overflow: hidden;
  display: flex;
  flex-direction: column; 
  position: relative;
  opacity: 0;
  transition: opacity 0.1s ease-in-out;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
}

.loaded.swiping {
  opacity: 1;
}

.swiping--status {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  pointer-events: none;
  position: absolute;
  z-index: 99999999999 !important;
}

.swiping--status i {
  font-size: 100px;
  opacity: 0;
  transform: scale(0.3);
  transition: all 0.2s ease-in-out;
  position: absolute;
  width: 100px;
  margin-left: -50px;
}

.swiping_love .{{likeIconHelper2}}{
  opacity: 0.7;
  transform: scale(1);
}

.swiping_nope .{{dislikeIconHelper2}}{
  opacity: 0.7;
  transform: scale(1);
}

.swiping--cards {
  flex-grow: 1;
  padding-top: 30px;
  text-align: center;
  display: flex;
  justify-content: center;
  /* align-items: flex-end; */
  z-index: 1;
  height: 600px;
}

.swiping--card {
border: 1px solid lightgray;
  display: inline-block;
  width: 90vw;
max-width: 330px;
  height: 550px;
  padding-bottom: 40px;
  border-radius: 8px;
  overflow: hidden;
  position: absolute;
  will-change: transform;
  transition: all 0.3s ease-in-out;
  cursor: -webkit-grab;
  cursor: -moz-grab;
  cursor: grab;
overflow-y: auto;
display: -webkit-inline-box;
    justify-content: center;
    align-items: center;
    background-color: #ffffff;
}

.swiping--card.disabled {
  cursor: not-allowed;
}

.moving.swiping--card {
  transition: none;
  cursor: -webkit-grabbing;
  cursor: -moz-grabbing;
  cursor: grabbing;
}

.removed.swiping--card {
  display: none;
}

.swiping--card img {
  max-width: 100%;
  pointer-events: none;
}

.swiping--card h3 {
  margin-top: 32px;
  font-size: 32px;
  padding: 0 16px;
  pointer-events: none;
}

.swiping--card p {
  margin-top: 24px;
  font-size: 20px;
  padding: 0 16px;
  pointer-events: none;
}

.swiping--card div {
  pointer-events: none;
}

.swiping--buttons {
  flex: 0 0 100px;
  text-align: center;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
}

.swiping--buttons button {
  border-radius: 50%;
  line-height: 60px;
  width: 60px;
  border: 0;
  display: inline-block;
  margin: 0 8px;
}

.swiping--buttons button:focus {
  outline: 0;
}

.swiping--buttons i {
  font-size: 32px;
  vertical-align: middle;
}

#card-back {
  text-align: center;
}

</style>

<script>
  'use strict';

var swipingContainer = document.querySelector('.swiping');
var allCards = document.querySelectorAll('.swiping--card');
var nope = document.getElementById('nope');
var love = document.getElementById('love');
var cardCounter = 0;
{% if showUndoBtn %}
var undoBtnMode = "{{undoBtnMode}}";
{% endif %}

var likes = (jQuery("#tsform-field-{{likeFormfieldId}}-0").val())

if(likes != '' && likes != undefined){
  likes = JSON.parse(jQuery("#tsform-field-{{likeFormfieldId}}-0").val());
}else{
  var likes = [];
}

var dislikes = (jQuery("#tsform-field-{{dislikeFormfieldId}}-0").val())
if(dislikes != '' && dislikes != undefined){
  dislikes = JSON.parse(jQuery("#tsform-field-{{dislikeFormfieldId}}-0").val());
}else{
  var dislikes = [];
}

cardCounter = likes.length + dislikes.length;
{% if showUndoBtn %}
if (cardCounter > 0){
  jQuery("#card-back").css("display", "initial");
}

if(undoBtnMode == "single"){
  jQuery("#card-back").css("display", "none");
}
{% endif %}

//toggle remove for already completed cards
var allCardsTemp = document.querySelectorAll('.swiping--card:not(.removed)'); 
var moveOutWidth = document.body.clientWidth * 1.5;
allCardsTemp.forEach(function (cardItem) {
  if(likes.includes(cardItem.getElementsByClassName("cardContent")[0].innerHTML.trim().replace(/&nbsp;/g, ''))){
    cardItem.classList.add('removed'); 
    cardItem.style.transform = 'translate(' + moveOutWidth + 'px, -1000px) rotate(-30deg)';
    initCards();
  }
  if(dislikes.includes(cardItem.getElementsByClassName("cardContent")[0].innerHTML.trim().replace(/&nbsp;/g, ''))){
    cardItem.classList.add('removed'); 
    cardItem.style.transform = 'translate(-' + moveOutWidth + 'px, -1000px) rotate(30deg)';
    initCards();
  }
})

if(cardCounter == allCards.length){
  jQuery("#stackCompleted").css("display", "block");
  {% if showRepeatBtn %}
  jQuery("#repeat-swiping").css("display", "initial");
  {% endif %}
  jQuery(".swiping").css("display", "none");
  jQuery("#love").css("display", "none");
  jQuery("#nope").css("display", "none");
}

function initCards(card, index) {
  var newCards = document.querySelectorAll('.swiping--card:not(.removed)'); 

  newCards.forEach(function (card, index) {
    if(index === 0) {
      card.classList.remove("disabled");
    } else {
      card.classList.add("disabled");
    }
    card.style.zIndex = allCards.length - index;
        card.style.transform = 'scale(' + (20 - index) / 20 + ') translateY(-' + 30 * index + 'px)';
  });  
  swipingContainer.classList.add('loaded');
}

initCards();

allCards.forEach(function (el) {
  var hammertime = new Hammer(el);


  hammertime.on('pan', function (event) {
    if(!el.classList.contains("disabled")) {
      el.classList.add('moving');
    }
  });

  hammertime.on('pan', function (event) {
    if(!el.classList.contains("disabled")) {
      if (event.deltaX === 0) return;
      if (event.center.x === 0 && event.center.y === 0) return;

      swipingContainer.classList.toggle('swiping_love', event.deltaX > 0);
      swipingContainer.classList.toggle('swiping_nope', event.deltaX < 0);

      var xMulti = event.deltaX * 0.03;
      var yMulti = event.deltaY / 80;
      var rotate = xMulti * yMulti;

      event.target.style.transform = 'translate(' + event.deltaX + 'px, ' + event.deltaY + 'px) rotate(' + rotate + 'deg)';
    }
  });

  hammertime.on('panend', function (event) {
    if(!el.classList.contains("disabled")) {
    el.classList.remove('moving');
    swipingContainer.classList.remove('swiping_love');
    swipingContainer.classList.remove('swiping_nope');
    var moveOutWidth = document.body.clientWidth;
    var keep = Math.abs(event.deltaX) < 80 || Math.abs(event.velocityX) < 0.5;

    event.target.classList.toggle('removed', !keep);

    if (keep) {
      event.target.style.transform = '';
    } else {
      var endX = Math.max(Math.abs(event.velocityX) * moveOutWidth, moveOutWidth);
      var toX = event.deltaX > 0 ? endX : -endX;
      var endY = Math.abs(event.velocityY) * moveOutWidth;
      var toY = event.deltaY > 0 ? endY : -endY;
      var xMulti = event.deltaX * 0.03;
      var yMulti = event.deltaY / 80;
      var rotate = xMulti * yMulti;

      event.target.style.transform = 'translate(' + toX + 'px, ' + (toY + event.deltaY) + 'px) rotate(' + rotate + 'deg)';
      initCards();

      if(event.deltaX > 0){
        likes.push(el.getElementsByClassName("cardContent")[0].innerHTML.trim().replace(/&nbsp;/g, ''))
        jQuery("#tsform-field-{{likeFormfieldId}}-0").val(JSON.stringify(likes))
      } else {
      dislikes.push(el.getElementsByClassName("cardContent")[0].innerHTML.trim().replace(/&nbsp;/g, ''))
        jQuery("#tsform-field-{{dislikeFormfieldId}}-0").val(JSON.stringify(dislikes))
      }

      cardCounter++;
      {% if showUndoBtn %}
      jQuery("#card-back").css("display", "initial");
      {% endif %}

      if(cardCounter == allCards.length){
        jQuery("#stackCompleted").css("display", "block");
        {% if showRepeatBtn %}
        jQuery("#repeat-swiping").css("display", "initial");
        {% endif %}
        jQuery(".swiping").css("display", "none");
        jQuery("#love").css("display", "none");
        jQuery("#nope").css("display", "none");
      }
    }
  }
  });
});

function createButtonListener(love) {
  return function (event) {
    var cards = document.querySelectorAll('.swiping--card:not(.removed)');
    var moveOutWidth = document.body.clientWidth * 1.5;
    if (!cards.length) return false;
    var card = cards[0];
    card.classList.add('removed'); 

    if (love) {
      card.style.transform = 'translate(' + moveOutWidth + 'px, -100px) rotate(-30deg)';
      likes.push(card.getElementsByClassName("cardContent")[0].innerHTML.trim().replace(/&nbsp;/g, ''))
        jQuery("#tsform-field-{{likeFormfieldId}}-0").val(JSON.stringify(likes))
    } else {
      card.style.transform = 'translate(-' + moveOutWidth + 'px, -100px) rotate(30deg)';
      dislikes.push(card.getElementsByClassName("cardContent")[0].innerHTML.trim().replace(/&nbsp;/g, ''))
        jQuery("#tsform-field-{{dislikeFormfieldId}}-0").val(JSON.stringify(dislikes))
    }

    initCards();

    event.preventDefault();

    cardCounter++;
    {% if showUndoBtn %}
    jQuery("#card-back").css("display", "initial");
    {% endif %}

    if(cardCounter == allCards.length){
      jQuery("#stackCompleted").css("display", "block");
      {% if showRepeatBtn %}
      jQuery("#repeat-swiping").css("display", "initial");
      {% endif %}
      jQuery(".swiping").css("display", "none");
      jQuery("#love").css("display", "none");
      jQuery("#nope").css("display", "none");
    }
  };
}

var nopeListener = createButtonListener(false);
var loveListener = createButtonListener(true);

nope.addEventListener('click', nopeListener);
love.addEventListener('click', loveListener);

{% if showUndoBtn %}
jQuery( "#card-back" ).click(function() {
  var cards = document.querySelectorAll('.removed');
  var card = cards[cards.length- 1];

  card.classList.remove('removed'); 
  var removedCard = card.getElementsByClassName("cardContent")[0].innerHTML.trim().replace(/&nbsp;/g, '');
  cardCounter--;
  if(likes.includes(removedCard)){
    likes.pop();
  jQuery("#tsform-field-{{likeFormfieldId}}-0").val(JSON.stringify(likes))
  }
  if(dislikes.includes(removedCard)){
    dislikes.pop();
  jQuery("#tsform-field-{{dislikeFormfieldId}}-0").val(JSON.stringify(dislikes))
  }
  
  if(cardCounter == 0){
  jQuery("#card-back").css("display", "none");
  }

  if(undoBtnMode == "single"){
    jQuery("#card-back").css("display", "none");
  }
  
  jQuery("#stackCompleted").css("display", "none");
  {% if showRepeatBtn %}
  jQuery("#repeat-swiping").css("display", "none");
  {% endif %}
  jQuery(".swiping").css("display", "flex");
  jQuery("#love").css("display", "initial");
  jQuery("#nope").css("display", "initial");


  initCards();

});
{% endif %}

{% if showRepeatBtn %}
jQuery("#repeat-swiping").on('click', function(e){
  var cards = document.querySelectorAll('.removed');
  for(var i = cards.length-1; i >= 0; i--){
    var card = cards[i];
    card.classList.remove('removed');
    var removedCard = card.getElementsByClassName("cardContent")[0].innerHTML.trim().replace(/&nbsp;/g, '');
    cardCounter--;
    if(likes.includes(removedCard)){
      likes.pop();
      jQuery("#tsform-field-{{likeFormfieldId}}-0").val(JSON.stringify(likes))
    }
    if(dislikes.includes(removedCard)){
      dislikes.pop();
      jQuery("#tsform-field-{{dislikeFormfieldId}}-0").val(JSON.stringify(dislikes))
    }
  }

  jQuery("#stackCompleted").css("display", "none");
  jQuery("#repeat-swiping").css("display", "none");
  {% if showUndoBtn %}
  jQuery("#card-back").css("display", "none");
  {% endif %}
  jQuery(".swiping").css("display", "flex");
  jQuery("#love").css("display", "initial");
  jQuery("#nope").css("display", "initial");


  initCards();
});
{% endif %}

</script>