window.App.notification = (() => {
  let timeoutId = null;

  return {
    show(title, animated = true) {
      this.toggle(true, title, animated);
    },

    hide(title, animated = true) {
      this.toggle(false, title, animated);
    },

    toast(title, delay = 500, animated = false) {
      if (timeoutId) {
        clearTimeout(timeoutId);
      }

      this.show(title, animated);
      timeoutId = setTimeout(() => this.hide(), delay);
    },

    toggle(visible, title = null, animated = true) {
      const $el = $('#notification');
      if (typeof title === 'string') $el.html(title);

      $el.toggle(visible).toggleClass('animated', animated);
      $(document.body).toggleClass('notification-shown', visible);
    },
  };
})();
