var resetting = false;
var initialCard = true;
var skipCard = false;
var showingSecondCard = false;
const audioDice = new Audio("audio/dice.mp3");
const PREVIOUS = true;
const INITIALRESET = -1;

$(document).ready(function() {
  // when ready
  $("#btn_showfirst").click(function() {
    showingSecondCard = false;
    getCard();
  });
  $("#btn_showsecond").click(function() {
    showingSecondCard = true;
    getCard();
  });
  $("#cardcount").on("click", function() {
    if ( $("#cardcount").html() == "" ) return;
    if ( !resetting ) {
      $("#cardcount").addClass("resetting");
      resetting = true;
    } else {
      resetPage();
    }
  });
  $("#mana_sun_img").on("click", function() { rollDice(); });
  $("#mana_water_img").on("click", function() { $(".diceroll").addClass("hidden"); });
  $("#mana_fire_img").on("click", function() {
    skipCard = true;
    getCard();
  });
  $("#mana_tree_img").on("click", function() { window.open("manage.php", "_blank"); });
  /*
  $("#mana_tree_img").longPress({
    holdTime: 1000,
    onHoldStart: function() {
      window.open("manage.php", "_blank");
    }
  });
  */
  $("#mana_skull_img").on("click", function() {
    getCard(PREVIOUS);
  });
  $(".diceroll").on("click", function() { rollDice(); });
  $(".card-image").longPress({
    holdTime: 1000,
    onHoldStart: function() {
      toggleState($(this));
    }
  });
  resetPage(INITIALRESET);
});

function resetPage(count = 0) {
  pageReset = false;
  if ( count == INITIALRESET ) {
    initialReset = true;
    count = 0;
  }
  $.ajax({
    url: 'ajax/resetpage.php',
    data: {cardcount: count},
    dataType: 'json',
    success: function(data, stat, jqo) {
      $("#firstimg").attr("src", "");
      $("#secondimg").attr("src", "");
      $("#firstimg").data("id", "0");
      $("#secondimg").data("id", "0");
      $("#secondimg").addClass("hidden");
      $("#btn_showsecond").addClass("hidden");
      $("#cardcount").html("");
      $("#cardcount").removeClass("resetting");
      $(".diceroll").addClass("hidden");
      resetting = false;
      initialCard = true;
      skipCard = false;
      showingSecondCard = false;
      if ( !initialReset ) {
        showCardCount(data.cardcount);
      } else {
        initialReset = false;
      }
    }
  });
}

function getCardCount() {
  $.ajax({
    url: 'ajax/getcardcount.php',
    data: {},
    dataType: 'json',
    success: function(data, stat, jqo) {
      showCardCount(data.cardcount);
    }
  });
}

function rollDice() {
  audioDice.play();
  $(".diceroll").addClass("hidden");
  var currentRoll = Math.floor(Math.random() * 4) + 2;
  var img = $("#mana_sun_img");
  var dicediv = $("#diceroll");
  dicediv.css('top', img.offset().top);
  dicediv.css('left', img.offset().left);
  dicediv.css('height', img.height());
  dicediv.css('width', img.width());
  dicediv.css('font-size', (img.height()/4) + "px");
  dicediv.html(currentRoll);
  dicediv.removeClass("hidden");
  initialCard = true;
  resetPage(currentRoll);
}

function showCardCount(count) {
  var img = $("#mana_diamond_img");
  var ccdiv = $("#cardcount");
  ccdiv.css('top', img.offset().top);
  ccdiv.css('left', img.offset().left);
  ccdiv.css('height', img.height());
  ccdiv.css('width', img.width());
  ccdiv.css('font-size', (img.height()/6) + "px");
  ccdiv.removeClass("resetting");
  resetting = false;
  ccdiv.html(count);
}

function getCard(reverse = false) {
  if ( !showingSecondCard ) {
    $("#firstimg").attr("src", "");
    $("#firstimg").data("id", "0");
    $("#firstimg").removeClass("disabledfade");
  }
  $.ajax({
    url: 'ajax/getcard.php',
    data: {
       second: showingSecondCard,
       initialcard: initialCard,
       skip: skipCard,
       reverse: reverse
    },
    dataType: 'json',
    success: function(data, stat, jqo) {
      skipCard = false;
      if ( data.error ) {
         $("#firstimg").attr("src", "");
         $("#secondimg").attr("src", "");
         $("#firstimg").data("id", "0");
         $("#secondimg").data("id", "0");
         $("#secondimg").addClass("hidden");
         $("#btn_showsecond").addClass("hidden");
         return;
      }
      initialCard = false;
      $("#firstimg").attr("src", "images/"+data.cards[0].filename);
      $("#firstimg").data("id", data.cards[0].id.toString());
      if ( data.cards[0].enabled ) {
        $("#firstimg").removeClass("disabledfade");
      } else {
        $("#firstimg").addClass("disabledfade");
      }
      if ( data.cards.length == 1 ) {
        $("#firstimg").removeClass("cardimg_double");
        $("#firstimg").addClass("cardimg_single");
        $("#firstimg").removeClass("padright");
        $("#secondimg").addClass("hidden");
        $("#secondimg").attr("src", "");
        $("#secondimg").data("id", "0");
        $("#btn_showsecond").removeClass("hidden");
      } else {
        $("#firstimg").removeClass("cardimg_single");
        $("#firstimg").addClass("cardimg_double");
        $("#firstimg").addClass("padright");
        $("#secondimg").attr("src", "images/"+data.cards[1].filename);
        $("#secondimg").data("id", data.cards[1].id.toString());
        $("#secondimg").removeClass("hidden");
        if ( data.cards[1].enabled ) {
          $("#secondimg").removeClass("disabledfade");
        } else {
          $("#secondimg").addClass("disabledfade");
        }
      }
      showCardCount(data.cardcount);
    }
  });
}

function toggleState(el) {
  $.ajax({
    url: 'ajax/togglestate.php',
    dataType: 'json',
    data: {id: el.data("id")},
    success: function(data, stat, jqo) {
      if ( data.image.enabled ) {
        el.removeClass("disabledfade");
      } else {
        el.addClass("disabledfade");
      }
    }
  });
}

// vim:ts=2 sw=2 et mouse-=a:
