(function ($) {
  'use strict';

  /*------------ Cookie functions and color js ------------*/
  function createCookie(name, value, days) {
    var expires = '';
    if (days) {
      var date = new Date();
      date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
      expires = '; expires=' + date.toUTCString();
    }
    document.cookie = name + '=' + value + expires + '; path=/';
  }

  function readCookie(name) {
    var nameEQ = name + '=';
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) == ' ') c = c.substring(1, c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
  }

  function eraseCookie(name) {
    createCookie(name, '', -1);
  }

  var prefersDark =
    window.matchMedia &&
    window.matchMedia('(prefers-color-scheme: dark)').matches;
  var selectedNightTheme = readCookie('body_dark');

  if (
    selectedNightTheme == 'true' ||
    (selectedNightTheme === null && prefersDark)
  ) {
    applyNight();
    $('.dark_mode_switcher').prop('checked', true);
  } else {
    applyDay();
    $('.dark_mode_switcher').prop('checked', false);
  }

  function applyNight() {
    if ($('.js-darkmode-btn .ball').length) {
      $('.js-darkmode-btn .ball').css('left', '45px');
    }
    $('body').addClass('body_dark');
  }

  function applyDay() {
    if ($('.js-darkmode-btn .ball').length) {
      $('.js-darkmode-btn .ball').css('left', '4px');
    }
    $('body').removeClass('body_dark');
  }

  $('.dark_mode_switcher').change(function () {
    if ($(this).is(':checked')) {
      applyNight();
      createCookie('body_dark', true, 999);
    } else {
      applyDay();
      createCookie('body_dark', false, 999);
    }
  });

  // Filter Select
  $('select').frmxNiceSelect();

  // Sidebar Tabs [COOKIE]
  $(document).on('click', '.tab-menu .easydocs-navitem', function () {
    const target = $(this).attr('data-rel');

    if (!$(this).hasClass('is-active')) {
      // Remove active state from ALL forum items (parent and sub-forums)
      $('.tab-menu .easydocs-navitem').removeClass('is-active');
      $(this).addClass('is-active');

      $('#' + target)
        .fadeIn('slow')
        .siblings('.easydocs-tab')
        .hide();

      createCookie('eazydocs_doc_current_tab', target, 999);

      // When switching to a new forum tab, reset filters so the first
      // filter item ("All Topics") is always active for the new content.
      eraseCookie('bbpc_current_filter');
      const $activeTab = $('#' + target);
      const $firstFilter = $activeTab.find('.single-item-filter .easydocs-btn').first();
      if ($firstFilter.length) {
        $('[cookie-id]').removeClass('is-active mixitup-control-active active');
        $firstFilter.click();
      }
    }
  });


  // Restore last active tab
  function keep_last_active_doc_tab() {
    const lastTab = readCookie('eazydocs_doc_current_tab');
    if (lastTab) {
      const $tab = $(`.tab-menu .easydocs-navitem[data-rel="${lastTab}"]`);
      if ($tab.length && !$tab.hasClass('is-active')) {
        // Remove active state from ALL forum items (parent and sub-forums)
        $('.tab-menu .easydocs-navitem').removeClass('is-active');
        $tab.addClass('is-active');

        $('.easydocs-tab-content .easydocs-tab').removeClass('tab-active');
        $(`#${lastTab}`).addClass('tab-active');
      }
    }
  }
  keep_last_active_doc_tab();


  $('.tab-menu .easydocs-navitem .parent-delete').on('click', function () {
    return false;
  });

  $(document).ready(function () {

    if ($('#bbpc-search').length) {
      $('#bbpc-search').on('keyup', function () {
        var value = $(this).val().toLowerCase();
        $('.easydocs-accordion-item').filter(function () {
          $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1);
        });
      });

      // Dropdown Classic UI Filter
      let bbpc_classic_ui = document.getElementById('bbpc_classic_ui');

      function swithToLink() {
        window.location.href = this.value;
      }

      bbpc_classic_ui.onchange = swithToLink;
    }
  })

  $(document).ready(function (e) {
    function t(t) {
      e(t).bind('click', function (t) {
        t.preventDefault();
        e(this).parent().fadeOut();
      });
    }

    e('.header-notify-icon').click(function () {
      var t = e(this)
        .parents('.easydocs-notification')
        .children('.easydocs-dropdown')
        .is(':hidden');
      e('.easydocs-notification .easydocs-dropdown').hide();
      e('.easydocs-notification .header-notify-icon').removeClass('active');
      if (t) {
        e(this)
          .parents('.easydocs-notification')
          .children('.easydocs-dropdown')
          .toggle()
          .parents('.easydocs-notification')
          .children('.header-notify-icon')
          .addClass('active');
      }
    });
    e(document).bind('click', function (t) {
      var n = e(t.target);
      if (!n.parents().hasClass('easydocs-notification'))
        e('.easydocs-notification .easydocs-dropdown').hide();
    });
    e(document).bind('click', function (t) {
      var n = e(t.target);
      if (!n.parents().hasClass('easydocs-notification'))
        e('.easydocs-notification .header-notify-icon').removeClass('active');
    });

    // CREATE FORUM
    function create_forum() {
      $(document).on('click', '#bbpc-forum', function (e) {
        e.preventDefault();
        Swal.fire({
          title: forumax_local_object.create_forum_title,
          input: 'text',
          showCancelButton: true,
          inputAttributes: {
            name: 'bbp_forum_title',
          },
        }).then((result) => {
          if (result.value) {
            $.ajax({
              url: forumax_local_object.ajaxurl,
              type: 'POST',
              data: {
                action: 'forumax_create_forum',
                bbp_forum_title: result.value,
                bbpc_nonce: forumax_local_object.nonce
              },
              beforeSend: function () {
                Swal.fire({
                  title: 'Creating Forum...',
                  icon: 'question',
                  allowOutsideClick: false,
                  showConfirmButton: true,
                  didOpen: () => {
                    Swal.showLoading();
                  },
                });
              },
              success: function (response) {
                if (response.success) {
                  location.reload();
                } else {
                  Swal.fire({
                    title: 'Error!',
                    text: response.data,
                    icon: 'error',
                  });
                }
              },
              error: function () {
                Swal.fire({
                  title: 'Error!',
                  text: 'Something went wrong. Please try again.',
                  icon: 'error',
                });
              },
            });
          }
        });
      });
    }
    create_forum();

    // CREATE SUB-FORUM
    function create_subforum() {
      $(document).on('click', '.add-subforum', function (e) {
        e.preventDefault();
        e.stopPropagation();
        let parentForumID = $(this).attr('bbp_parent_forum_id');
        let parentTitle = $(this).closest('.easydocs-navitem').find('.easydocs-forums-title').text();

        Swal.fire({
          title: forumax_local_object.create_subforum_title || 'Create Sub-Forum',
          html: '<p style="color: #666; margin-bottom: 15px;">Creating sub-forum under: <strong>' + parentTitle + '</strong></p>',
          input: 'text',
          showCancelButton: true,
          inputPlaceholder: 'Enter sub-forum name...',
          inputAttributes: {
            name: 'bbp_subforum_title',
          },
          confirmButtonColor: '#7c3aed',
          confirmButtonText: 'Create Sub-Forum',
        }).then((result) => {
          if (result.value) {
            $.ajax({
              url: forumax_local_object.ajaxurl,
              type: 'POST',
              data: {
                action: 'forumax_create_subforum',
                bbp_subforum_title: result.value,
                parent_forum_id: parentForumID,
                bbpc_nonce: forumax_local_object.nonce
              },
              beforeSend: function () {
                Swal.fire({
                  title: 'Creating Sub-Forum...',
                  icon: 'info',
                  allowOutsideClick: false,
                  showConfirmButton: false,
                  didOpen: () => {
                    Swal.showLoading();
                  },
                });
              },
              success: function (response) {
                if (response.success) {
                  Swal.fire({
                    title: 'Success!',
                    text: 'Sub-forum created successfully.',
                    icon: 'success',
                    timer: 1500,
                    showConfirmButton: false
                  }).then(() => {
                    location.reload();
                  });
                } else {
                  Swal.fire({
                    title: 'Error!',
                    text: response.data || 'Failed to create sub-forum.',
                    icon: 'error',
                  });
                }
              },
              error: function () {
                Swal.fire({
                  title: 'Error!',
                  text: 'Something went wrong. Please try again.',
                  icon: 'error',
                });
              },
            });
          }
        });
      });
    }
    create_subforum();

    // IMPORT DEMO DATA
    function import_demo_data() {
      $(document).on('click', '#forumax-import-demo', function (e) {
        e.preventDefault();
        Swal.fire({
          title: 'Import Demo Data?',
          html: '<p>This will import comprehensive sample forums, topics, and replies to help you explore all Forumax features.</p><p><strong>Includes:</strong> 5 Forums, 14 Topics, 22 Replies, 1 Attachment</p><p><strong>Features:</strong> Closed topics, threaded replies, solved topics, featured images</p>',
          icon: 'question',
          showCancelButton: true,
          confirmButtonColor: '#7c3aed',
          cancelButtonColor: '#6b7280',
          confirmButtonText: 'Yes, Import Demo Data',
          cancelButtonText: 'Cancel'
        }).then((result) => {
          if (result.isConfirmed) {
            $.ajax({
              url: forumax_local_object.ajaxurl,
              type: 'POST',
              data: {
                action: 'forumax_import_demo_data',
                nonce: forumax_local_object.nonce
              },
              beforeSend: function () {
                Swal.fire({
                  title: 'Importing Demo Data...',
                  html: '<p>Please wait while we import comprehensive sample forums, topics, and replies.</p><p>This may take a moment.</p>',
                  icon: 'info',
                  allowOutsideClick: false,
                  showConfirmButton: false,
                  didOpen: () => {
                    Swal.showLoading();
                  },
                });
              },
              success: function (response) {
                if (response.success) {
                  Swal.fire({
                    title: 'Success!',
                    text: response.data.message,
                    icon: 'success',
                    timer: 2000,
                    showConfirmButton: false
                  }).then(() => {
                    location.reload();
                  });
                } else {
                  Swal.fire({
                    title: 'Error!',
                    text: response.data.message || 'Failed to import demo data.',
                    icon: 'error',
                  });
                }
              },
              error: function () {
                Swal.fire({
                  title: 'Error!',
                  text: 'Something went wrong. Please try again.',
                  icon: 'error',
                });
              },
            });
          }
        });
      });
    }
    import_demo_data();

    // CREATE TOPIC
    function create_topic() {
      $(document).on('click', '#bbpc-topic', function (e) {
        e.preventDefault();
        let forumID = $(this).attr('bbp_forum_id');
        Swal.fire({
          title: forumax_local_object.create_topic_title,
          input: 'text',
          showCancelButton: true,
          inputAttributes: {
            name: 'bbp_topic_title',
          },
        }).then((result) => {
          if (result.value) {
            $.ajax({
              url: forumax_local_object.ajaxurl,
              type: 'POST',
              data: {
                action: 'forumax_create_topic',
                bbp_topic_title: result.value,
                forum_id: forumID,
                bbpc_nonce: forumax_local_object.nonce
              },
              beforeSend: function () {
                Swal.fire({
                  title: 'Creating Topic...',
                  icon: 'question',
                  allowOutsideClick: false,
                  showConfirmButton: true,
                  didOpen: () => {
                    Swal.showLoading();
                  },
                });
              },
              success: function (response) {
                if (response.success) {
                  location.reload();
                } else {
                  Swal.fire({
                    title: 'Error!',
                    text: response.data,
                    icon: 'error',
                  });
                }
              },
              error: function () {
                Swal.fire({
                  title: 'Error!',
                  text: 'Something went wrong. Please try again.',
                  icon: 'error',
                });
              },
            });
          }
        });
      });
    }
    create_topic();

    // DELETE FORUM
    function delete_forum() {
      $('.forum-delete').on('click', function (e) {
        e.preventDefault();
        var forumID = $(this).attr('bbp_forum_id');
        Swal.fire({
          title: forumax_local_object.forum_delete_title,
          text: forumax_local_object.forum_delete_desc,
          icon: 'question',
          showCancelButton: true,
          confirmButtonColor: '#3085d6',
          cancelButtonColor: '#d33',
          confirmButtonText: 'Yes'
        }).then((result) => {
          if (result.value) {
            $.ajax({
              url: forumax_local_object.ajaxurl,
              type: 'POST',
              data: {
                action: 'forumax_delete_forum',
                forum_id: forumID,
                bbpc_nonce: forumax_local_object.nonce
              },
              beforeSend: function () {
                Swal.fire({
                  title: 'Deleting Forum...',
                  icon: 'question',
                  allowOutsideClick: false,
                  showConfirmButton: false,
                  didOpen: () => {
                    Swal.showLoading();
                  },
                });
              },
              success: function (response) {
                console.log(response);
                if (response.success) {
                  location.reload();
                } else {
                  console.log(response)
                  Swal.fire({
                    title: 'Error!',
                    text: response.data || 'Unexpected error occurred.',
                    icon: 'error',
                  });
                }
              },
              error: function () {
                Swal.fire({
                  title: 'Error!',
                  text: 'Something went wrong. Please try again.',
                  icon: 'error',
                });
              },
            });
          }
        });
      });
    }
    delete_forum();


    // DELETE TOPIC
    function delete_topic() {
      $('.section-delete').on('click', function (e) {
        e.preventDefault();
        var topicID = $(this).attr('bbp_topic_id');
        Swal.fire({
          title: forumax_local_object.forum_delete_title,
          text: forumax_local_object.topic_delete_desc,
          icon: 'question',
          showCancelButton: true,
          confirmButtonColor: '#3085d6',
          cancelButtonColor: '#d33',
          confirmButtonText: 'Yes',
        }).then((result) => {
          if (result.value) {
            $.ajax({
              url: forumax_local_object.ajaxurl,
              type: 'POST',
              data: {
                action: 'forumax_delete_topic',
                topic_id: topicID,
                bbpc_nonce: forumax_local_object.nonce
              },
              beforeSend: function () {
                Swal.fire({
                  title: 'Deleting Topic...',
                  icon: 'question',
                  allowOutsideClick: false,
                  showConfirmButton: false,
                  didOpen: () => {
                    Swal.showLoading();
                  },
                });
              },
              success: function (response) {
                console.log(response);
                if (response.success) {
                  location.reload();
                } else {
                  console.log(response)
                  Swal.fire({
                    title: 'Error!',
                    text: response.data || 'Unexpected error occurred.',
                    icon: 'error',
                  });
                }
              },
              error: function () {
                Swal.fire({
                  title: 'Error!',
                  text: 'Something went wrong. Please try again.',
                  icon: 'error',
                });
              },
            });
          }
        });
      });
    }

    delete_topic();

    // Notification pro alert
    $('.easydocs-notification.forumax-pro-notification').on(
      'click',
      function (e) {
        e.preventDefault();
        let href = $(this).attr('href');
        let assets = forumax_local_object.forumax_assets;
        Swal.fire({
          title: 'Notification is a Premium feature',
          html:
            '<span class="pro-notification-body-text">You need to Upgrade the Premium Version to use this feature</span><video height="400px" autoplay="autoplay" loop="loop" src="' +
            assets +
            '/videos/noti.mp4"></video>',
          icon: false,
          buttons: false,
          dangerMode: true,
          showCloseButton: true,
          confirmButtonText:
            '<a href="admin.php?page=forumax-pricing">Upgrade to Premium</a>',
          footer:
            '<a href="https://forumax.spider-themes.net/" target="_blank"> Learn More </a>',
          customClass: {
            title: 'upgrade-premium-heading',
            confirmButton: 'upgrade-premium-button',
            footer: 'notification-pro-footer-wrap',
          },
          confirmButtonColor: '#f1bd6c',
          Borderless: true,
        });
      }
    );
  });

  // Click pending replies count to show pending replies.
  $('[click-target]').click(function () {
    let id = $(this).attr('click-target');
    $(`[click-target=${id}]`).toggleClass('active');
    $(`[reply-target=${id}]`).toggle();
  });

  // Sidebar Tabs [COOKIE]
  $(document).on('click', '[cookie-id]', function () {
    let target = $(this).attr('cookie-id');
    let item = `[cookie-id="${target}"]`;
    $('[cookie-id]').removeClass('is-active mixitup-control-active active');
    $(item).addClass('is-active mixitup-control-active active');
    $(target).fadeIn('slow').siblings('.easydocs-tab').hide();

    let isActiveTab = $(this).hasClass('is-active');
    if (isActiveTab === true) {
      createCookie('bbpc_current_filter', target, 999);
    }

    return true;
  });

  $(document).ready(function () {
    // Keep Last filter item active
    function keepLastFilterActive() {
      let bbpcLastActiveFilter = readCookie('bbpc_current_filter');
      if (bbpcLastActiveFilter) {
        // Tab item
        $('[cookie-id]').removeClass('is-active mixitup-control-active active');
        $(`[cookie-id="${bbpcLastActiveFilter}"]`).click();
      } else {
        // On initial load with no cookie, click the first filter item
        $('.single-item-filter .easydocs-btn').first().click();
      }
    }

    keepLastFilterActive();

    // SUB-FORUM TOGGLE
    function initSubforumToggles() {
      // Restore collapsed states from cookie
      const collapsedForums = readCookie('forumax_collapsed_forums');
      const collapsedIds = collapsedForums ? collapsedForums.split(',') : [];

      collapsedIds.forEach(function (id) {
        if (id) {
          const $container = $(`.easydocs-subforum-container[data-parent="${id}"]`);
          const $toggle = $(`.easydocs-navitem[data-id="${id}"] .subforum-toggle`);
          if ($container.length) {
            $container.addClass('is-collapsed');
            $toggle.attr('aria-expanded', 'false');
          }
        }
      });

      // Toggle click handler
      $(document).on('click', '.subforum-toggle', function (e) {
        e.preventDefault();
        e.stopPropagation();

        const $navItem = $(this).closest('.easydocs-navitem');
        const forumId = $navItem.data('id');
        const $container = $(`.easydocs-subforum-container[data-parent="${forumId}"]`);
        const isExpanded = $(this).attr('aria-expanded') === 'true';

        if (isExpanded) {
          $container.addClass('is-collapsed');
          $(this).attr('aria-expanded', 'false');
        } else {
          $container.removeClass('is-collapsed');
          $(this).attr('aria-expanded', 'true');
        }

        // Save state to cookie
        saveCollapsedState();
      });
    }

    function saveCollapsedState() {
      const collapsedIds = [];
      $('.easydocs-subforum-container.is-collapsed').each(function () {
        collapsedIds.push($(this).data('parent'));
      });
      createCookie('forumax_collapsed_forums', collapsedIds.join(','), 30);
    }

    initSubforumToggles();

  });

})(jQuery);

function menuToggle() {
  const toggleMenu = document.querySelector('.easydocs-dropdown');
  toggleMenu.classList.toggle('is-active');
}