jQuery(document).ready(function($) {
    function updateTabIndicator($tabsWrap) {
        var $tabsContainer = $tabsWrap.find('.griffinforms-settings-tabs');
        var $indicator = $tabsWrap.find('.griffinforms-settings-tab-indicator');
        var $activeTab = $tabsWrap.find('.griffinforms-settings-tab.active').not('.is-overflow');

        if (!$activeTab.length) {
            $indicator.css({ width: 0, opacity: 0 });
            return;
        }

        var left = $activeTab.position().left;
        var width = $activeTab.outerWidth();
        $indicator.css({
            width: width,
            opacity: 1,
            transform: 'translateX(' + left + 'px)'
        });
    }

    function layoutSettingsTabs($tabsWrap) {
        var $tabsContainer = $tabsWrap.find('.griffinforms-settings-tabs');
        var $tabs = $tabsContainer.find('.griffinforms-settings-tab');
        var $overflowWrap = $tabsWrap.find('.griffinforms-settings-tabs-overflow');
        var $moreBtn = $tabsWrap.find('.griffinforms-settings-tabs-more');
        var $menu = $tabsWrap.find('.griffinforms-settings-tabs-more-menu');

        $tabs.removeClass('is-overflow').show();
        $menu.empty().hide();
        $tabsWrap.removeClass('has-overflow');
        $moreBtn.attr('aria-expanded', 'false');

        var availableWidth = $tabsContainer.innerWidth();
        var overflowItems = [];
        var usedWidth = 0;

        $tabs.each(function() {
            var $tab = $(this);
            var tabWidth = $tab.outerWidth(true);

            if (usedWidth + tabWidth > availableWidth) {
                overflowItems.push($tab);
            } else {
                usedWidth += tabWidth;
            }
        });

        if (overflowItems.length) {
            $tabsWrap.addClass('has-overflow');
            $moreBtn.show();

            overflowItems.forEach(function($tab) {
                $tab.addClass('is-overflow').hide();
                var isActive = $tab.hasClass('active') ? ' active' : '';
                var $item = $('<a />', {
                    'class': 'griffinforms-settings-tabs-more-item' + isActive,
                    href: $tab.attr('href'),
                    role: 'menuitem',
                    text: $tab.text()
                });
                $menu.append($item);
            });
        } else {
            $moreBtn.hide();
        }

        updateTabIndicator($tabsWrap);
    }

    function bindSettingsTabs() {
        var $tabsWrap = $('.griffinforms-settings-tabs-wrapper');
        if (!$tabsWrap.length) {
            return;
        }

        layoutSettingsTabs($tabsWrap);

        $tabsWrap.on('mouseenter focus', '.griffinforms-settings-tab', function() {
            var $tab = $(this);
            if ($tab.hasClass('is-overflow')) {
                return;
            }
            var $indicator = $tabsWrap.find('.griffinforms-settings-tab-indicator');
            var left = $tab.position().left;
            var width = $tab.outerWidth();
            $indicator.css({
                width: width,
                opacity: 1,
                transform: 'translateX(' + left + 'px)'
            });
        });

        $tabsWrap.on('mouseleave', function() {
            updateTabIndicator($tabsWrap);
        });

        $tabsWrap.on('click', '.griffinforms-settings-tabs-more', function(e) {
            e.preventDefault();
            var isOpen = $(this).attr('aria-expanded') === 'true';
            $(this).attr('aria-expanded', isOpen ? 'false' : 'true');
            $tabsWrap.find('.griffinforms-settings-tabs-more-menu').toggle(!isOpen);
        });

        $(document).on('click', function(e) {
            if (!$(e.target).closest('.griffinforms-settings-tabs-overflow').length) {
                $tabsWrap.find('.griffinforms-settings-tabs-more-menu').hide();
                $tabsWrap.find('.griffinforms-settings-tabs-more').attr('aria-expanded', 'false');
            }
        });

        $(window).on('resize', function() {
            layoutSettingsTabs($tabsWrap);
        });
    }

    bindSettingsTabs();
    showComplianceDefaultsSuboptions();

    $(document).on('click', '.gf-dismiss-notice', function() {
        $(this).closest('.gf-is-dismissible').hide();
    });

    $(document).on('click', '.gf-settings-history-view-full', function(e) {
        e.preventDefault();
        var fullValue = $(this).attr('data-gf-full-value') || '';
        if (fullValue) {
            alert(fullValue);
        }
    });

    $(document).on('click', '.gf-settings-history-toggle', function(e) {
        e.preventDefault();
        var toggle = $(this);
        var list = toggle.closest('.griffinforms-settings-option-history').find('.gf-settings-history-list');
        if (!list.length) {
            return;
        }
        list.slideToggle(150, function() {
            var isOpen = list.is(':visible');
            toggle.attr('aria-expanded', isOpen ? 'true' : 'false');
            var text = isOpen ? toggle.attr('data-gf-expanded-text') : toggle.attr('data-gf-collapsed-text');
            if (text) {
                toggle.text(text);
            }
        });
    });

    $(document).on('click', '#griffinforms-settings-compliance-compliancedefaultsenabled', showComplianceDefaultsSuboptions);
    $(document).on('change', '#griffinforms-settings-compliance-compliancedefaultprofile', showComplianceHipaaNoticeOption);
    showComplianceDefaultsSuboptions();
    showComplianceHipaaNoticeOption();

    function showComplianceDefaultsSuboptions() {
        var $toggle = jQuery("#griffinforms-settings-compliance-compliancedefaultsenabled");
        if (!$toggle.length) {
            return;
        }
        var isOn = $toggle.is(":checked");
        if (isOn) {
            jQuery("#griffinforms-settings-compliance-compliancedefaultprofile-wrap").show(200);
            jQuery("#griffinforms-settings-compliance-compliancedefaultstoreip-wrap").show(200);
            jQuery("#griffinforms-settings-compliance-compliancedefaultstoreuseragent-wrap").show(200);
            jQuery("#griffinforms-settings-compliance-compliancedefaultretentiondays-wrap").show(200);
            jQuery("#griffinforms-settings-compliance-compliancetrustproxyheaders-wrap").show(200);
        } else {
            jQuery("#griffinforms-settings-compliance-compliancedefaultprofile-wrap").hide(200);
            jQuery("#griffinforms-settings-compliance-compliancedefaultstoreip-wrap").hide(200);
            jQuery("#griffinforms-settings-compliance-compliancedefaultstoreuseragent-wrap").hide(200);
            jQuery("#griffinforms-settings-compliance-compliancedefaultretentiondays-wrap").hide(200);
            jQuery("#griffinforms-settings-compliance-compliancetrustproxyheaders-wrap").hide(200);
        }
        showComplianceHipaaNoticeOption();
    }

    function showComplianceHipaaNoticeOption() {
        var $row = jQuery("#griffinforms-settings-compliance-complianceshowipnotice-wrap");
        if (!$row.length) {
            return;
        }
        var defaultsEnabled = jQuery("#griffinforms-settings-compliance-compliancedefaultsenabled").is(":checked");
        var profile = jQuery("#griffinforms-settings-compliance-compliancedefaultprofile").val() || "";
        if (defaultsEnabled && profile === "hipaa") {
            $row.show(200);
        } else {
            $row.hide(200);
        }
    }
});
