<script>
  window.EASY = window.EASY || {};
  EASY.trialExpiration = EASY.trialExpiration || {};

  /**
   *
   * @param {number} projectLimit
   */
  EASY.trialExpiration.loadProjectSelection = function (projectLimit) {

    $("#project_selection table tbody").on("click", "input", function (event) {
      checkRelatives($(this));
      event.stopPropagation();
    });

    $('.easy-trial-expiration__table.projects').on('click', 'span.project-parent-expander', function (event) {
      event.stopPropagation();
    });

    $("#project_selection").on("click", "tr", function () {
      var $checkbox = $(this).find('input');
      $checkbox.prop('checked', !$checkbox.prop('checked'));
      checkRelatives($checkbox);
    });

    function checkedProjectsCount() {
      return $("#project_selection").find("input[type=checkbox]:checked").length;
    }

    function updateSelectedProjectsCount() {
      var checked_count = checkedProjectsCount();
      $('#label_selected_projects_count').text(checked_count + '/' + projectLimit);
      if (checked_count > projectLimit) {
        $('#label_selected_projects_count').addClass('red');
        EASY.trialExpiration.disableSaveButton();
      } else {
        $('#label_selected_projects_count').removeClass('red');
        EASY.trialExpiration.enableSaveButton();
      }
    }

    function checkRelatives($checkbox) {
      if ($checkbox.prop("checked")) {
        ensureParents($checkbox);
      } else {
        uncheckChildren($checkbox);
      }
      updateSelectedProjectsCount();
    }

    function ensureParents($checkbox) {
      if (!($checkbox.closest('tr').attr('data-level') == '0')) {
        var level = parseInt($checkbox.closest('tr').attr('data-level'));
        $parent_checkbox = $checkbox.closest('tr').prevAll('tr[data-level=' + (level - 1) + ']:first').find('input[type=checkbox]');
        if ($parent_checkbox.length == 0) return;
        $parent_checkbox.prop('checked', true);
        ensureParents($parent_checkbox);
      }
    }

    function uncheckChildren($checkbox) {
      var level = parseInt($checkbox.closest('tr').attr('data-level'));
      $checkbox.closest('tr').nextUntil(function () {
        return $(this).attr("data-level") <= level;
      }).each(function (k, v) {
        $(v).find('input[type=checkbox]').first().prop('checked', false)
      });
    }

    $(function () {
      $('.easy-trial-expiration__table.projects').easytreeview();
    });
  };

  /**
   *
   * @param {number} userLimit
   */
  EASY.trialExpiration.loadUserSelection = function (userLimit) {

    var $userCheckboxes = $('#user_selection').find('input[type=checkbox]');

    $userCheckboxes.on('click', function (event) {
      updateSelectedUsersCount();
      event.stopPropagation();
    });

    var $userRows = $('#user_selection').find('tr');

    $userRows.click(function () {
      var $checkbox = $(this).find('input');
      if (!($checkbox.is(':disabled'))) {
        $checkbox.prop('checked', !$checkbox.prop('checked'));
        updateSelectedUsersCount();
      }
    });

    function checkedUsersCount() {
      return $('#user_selection').find('input[type=checkbox]:checked').length;
    }

    function updateSelectedUsersCount() {
      var checkedCount = checkedUsersCount();
      $('#label_selected_users_count').text(checkedCount + '/' + userLimit);
      if (checkedCount > userLimit) {
        $('#label_selected_users_count').addClass('red');
        EASY.trialExpiration.disableSaveButton();
      } else {
        $('#label_selected_users_count').removeClass('red');
        EASY.trialExpiration.enableSaveButton();
      }
    }
  };

  EASY.trialExpiration.enableSaveButton = function () {
    $('.ui-dialog-buttonset').find('#modal_button_save, #modal_button_next').attr('disabled', false);
  };

  EASY.trialExpiration.disableSaveButton = function () {
    $('.ui-dialog-buttonset').find('#modal_button_save, #modal_button_next').attr('disabled', true);
  };

  EASY.trialExpiration.showProjectSelection = function (){
    $('#user_selection').hide();
    $('#title_for_user_selection').hide();

    $('#project_selection').show();
    $('#title_for_project_selection').show().addClass('ui-dialog-titlebar_selection').attr('style',''); // ? ask daria why

    $('#modal_button_activate_all_users').hide();
    $('#modal_button_activate_all_projects').show();

    $('#modal_button_next').hide();
    $('#modal_button_save').show();
  };

  EASY.trialExpiration.postponeActivation = function() {
    var expiration_date = new Date();
    expiration_date.setDate(expiration_date.getDate() + 1);
    document.cookie = "activation_postponed=true; expires=" + expiration_date.toUTCString();
  }

</script>
