'use strict';

var tinderContainer = document.querySelector('.tinder');
var allCards = document.querySelectorAll('.tinder--card');
var nope = document.getElementById('nope');
var love = document.getElementById('love');
var bilgi = null;


function initCards(card, index) {

  var newCards = document.querySelectorAll('.tinder--card:not(.removed)');

  newCards.forEach(function (card, index) {
    card.style.zIndex = allCards.length - index;
    card.style.transform = 'scale(' + (20 - index) / 20 + ') translateY(-' + 30 * index + 'px)';
    card.style.opacity = (10 - index) / 10;
  });
  
  tinderContainer.classList.add('loaded');
}

var kontrol = 0;
function kartlariCalistir() {

tinderContainer = document.querySelector('.tinder');
allCards = document.querySelectorAll('.tinder--card');
nope = document.getElementById('nope');
love = document.getElementById('love');
bilgi = null;


          initCards();

          allCards.forEach(function (el) {
            var hammertime = new Hammer(el);
            hammertime.on('pan', function (event) {
              el.classList.add('moving');
            });

            
            hammertime.on('pan', function (event) {
              if (event.deltaX === 0) return;
              if (event.center.x === 0 && event.center.y === 0) return;

              tinderContainer.classList.toggle('tinder_love', event.deltaX > 0);
              tinderContainer.classList.toggle('tinder_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) {
              el.classList.remove('moving');
              tinderContainer.classList.remove('tinder_love');
              tinderContainer.classList.remove('tinder_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)';
                
                bilgi = $(hammertime).find('div.bilgix').prevObject[0].element.nextSibling.innerText;
                //console.log(bilgi)
                //bilgi = $(hammertime).find('div.discard').prevObject[0].element.innerText;

                if(event.additionalEvent=='panright'){
                  kaydirdim('like',bilgi)
                }else if(event.additionalEvent=='panleft'){
                  kaydirdim('unlike',bilgi)
                }

                initCards();
              }
            });
          });

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

          if (kontrol == 0) {
            kontrol++;
            nope.addEventListener('click', nopeListener);
            love.addEventListener('click', loveListener);
          }


}

function createButtonListener(love) {

  return function (event) {
    var cards = document.querySelectorAll('.tinder--card:not(.removed)');
    var moveOutWidth = document.body.clientWidth * 1.5;
    if (!cards.length) return false;

    var card = cards[0];

    card.classList.add('removed');

    bilgi = $(card).find('div.bilgix').prevObject[0].nextSibling.innerText;

    if (love) {
      card.style.transform = 'translate(' + moveOutWidth + 'px, -100px) rotate(-30deg)';
    } else {
      card.style.transform = 'translate(-' + moveOutWidth + 'px, -100px) rotate(30deg)';
    }
    initCards();
    kaydirdim(love,bilgi)
    event.preventDefault();

  };

}


var sayim = 0;
function kaydirdim(value,value2){

if(value===true || value=='like'){
  console.log('beğendim = '+value2);
  begeniGonder(value2);
  console.log(sayim)
}else if(value===false || value=='unlike'){
  console.log('beğenmedim geç = '+value2)
  console.log(sayim)
}else{
  console.log('hata');
}
 sayim++;

                  if(sayim == 10){

                    $(".tinder").prepend('<center><img id="loadingx" src="http://themes.potenzaglobalsolutions.com/cupidlovedesign/intro/ios/customizing-app/assets/images/loading.gif" style="margin-top:20px; max-width: 100% !important"></center>');
                    setTimeout(function(){ 
                    $("#loadingx").remove();
                 
                      findGetir2()
                      sayim = 0;
                      
                  }, 1500);
                   
                  }

}




  function begeniGonder(value){



    var url = "assets/api.php";
    var bilgi = {
      type: 'begeni',
      begenilen_id: value
    }
    $.post(url,bilgi,function(response) {
      var json = JSON.parse(response);

if(json.result==100){
  $('#bjetonAl').modal('show'); 
  $('.tinder').attr('onClick', "$('#bjetonAl').modal('show')");
  $('.tinder--cards').css('pointer-events','none');
  $('.tinder--buttons').css('pointer-events','none');

}
      


   })

  }