/* AutoDoc Javascript */

var conf = {
  default_hash: "home",
  openapi: "/v1",
  pageinit: {
    home: "page_load_home()"
  },
  home: {
    width: 4,
    curpage: 0,
    end: 0
  }
};

$(document).ready(function() {
  change_page();
  $(document).scroll(function() {
    page_home_scroll();
  });
  $('#autodoc_upload_zone').change(function() {
    console.log(this);
  });
});

$(window).bind("hashchange", function() {
  change_page();
});

function change_page() {
  var hash = document.location.hash.substr(1) || conf.default_hash;

  if (!$("#autodoc_tab_" + hash).length) {
    hash = conf.default_hash;
  }

  /* change menu highlights and content visibility */
  $("#autodoc_navbar")
    .find("a")
    .each(function(id, obj) {
      var curhash = $(obj)
        .attr("href")
        .substr(1);
      if (curhash == hash) {
        $(obj)
          .parent()
          .addClass("active");
        $("#autodoc_tab_" + curhash).removeClass("d-none");
      } else {
        $(obj)
          .parent()
          .removeClass("active");
        $("#autodoc_tab_" + curhash).addClass("d-none");
      }
    });

  if (conf.pageinit[hash]) {
    eval(conf.pageinit[hash]);
  }
}

function isvisible(obj) {
  var top_of_element = $(obj).offset().top;
  var bottom_of_element = $(obj).offset().top + $(obj).outerHeight();
  var bottom_of_screen = $(window).scrollTop() + $(window).innerHeight();
  var top_of_screen = $(window).scrollTop();

  if (bottom_of_screen > top_of_element && top_of_screen < bottom_of_element) {
    return true;
  }
  return false;
}

function page_load_home() {
  document_load();
}

function document_load() {
  if (conf.home.curpage == 0) {
    $("#autodoc_home_body").html("");
  }

  var tmpl = $("#autodoc_template_home");
  var row = $(tmpl)
    .find(".autodoc_template_home_row")
    .clone();

  $.getJSON(
    conf.openapi +
      "/documents?pageSize=" +
      conf.home.width +
      "&pageIndex=" +
      conf.home.curpage,
    null,
    function(data, textStatus, jqXHR) {
      // last document was loaded, stop scrolling
      if (data.length < conf.home.width) {
        conf.home.end = 1;
      }
      $.each(data, function(id, doc) {
        var col = $(tmpl)
          .find(".autodoc_template_home_col")
          .clone();
        $(col)
          .find(".autodoc_template_home_img")
          .attr(
            "src",
            conf.openapi +
              "/documents/" +
              doc.id +
              "/image?maxWidth=" +
              Math.floor(1000 / conf.home.width)
          );

        $(col)
          .find(".autodoc_template_home_owner")
          .html(doc.owner);

        $(col)
          .find(".autodoc_template_home_created")
          .html(doc.created);

        $(col)
          .find(".autodoc_template_home_name")
          .html(doc.name);

        $.each(doc.tags, function(id, tag) {
          $(col)
            .find(".autodoc_template_home_tag")
            .after(
              $(col)
                .find(".autodoc_template_home_tag")
                .clone()
                .html(tag)
            );
        });
        $(row).append(col);
      });
      $("#autodoc_home_body").append(row);
      // continue to load until out of browser.
      page_home_scroll();
    }
  );
}

function page_home_scroll() {
  // don't scroll if we've loaded the last document
  if (conf.home.end) {
    return;
  }
  if (isvisible($(".autodoc_template_home_col").last())) {
    console.log("scroll to page " + conf.home.curpage);
    conf.home.curpage++;
    document_load();
  }
}

function file_upload() {
  console.log(this.files);
}