/*global document, jQuery */
jQuery(document).ready(function () {
    
    showLimitEntriesSuboptions();
    showAvailabilityWindowSuboptions();
    showTogglePublicAccessSuboptions();
    showToggleRoleAccessSuboptions();
    showAlertEmailsToSuboptions();
    showUserActionSuboptions();
    showToggleAutoresponderSuboptions();
    showComplianceFlagModeSuboptions();
    showComplianceSuboptions();
    // Temporary workaround: Delayed recheck due to AJAX timing dependency
    setTimeout(showToggleAutoresponderSuboptions, 500);
    
    jQuery("#griffinforms-togglelimitentries-togglecheckbox").click(showLimitEntriesSuboptions);
    jQuery("#griffinforms-toggleavailabilitywindow-togglecheckbox").click(showAvailabilityWindowSuboptions);
    jQuery(".griffinforms-togglepublicaccess").click(showTogglePublicAccessSuboptions);
    jQuery("#griffinforms-toggleroleaccess-togglecheckbox").click(showToggleRoleAccessSuboptions);
    jQuery(".griffinforms-entryalertsto").click(showAlertEmailsToSuboptions);
    jQuery(".griffinforms-actiontype").click(showUserActionSuboptions);
    jQuery("#griffinforms-toggleautoresponder-togglecheckbox").click(showToggleAutoresponderSuboptions);
    jQuery("#griffinforms-compliancemode-togglecheckbox").click(showComplianceSuboptions);
    jQuery(document).on("change", "#griffinforms-complianceflagmode-selectfield", showComplianceFlagModeSuboptions);
    jQuery(document).on("click", ".gf-reset-lifetime-count", resetLifetimeCount);
    
    function showLimitEntriesSuboptions() {
        var isLimitOn = jQuery("#griffinforms-togglelimitentries-togglecheckbox").is(":checked");
        if (isLimitOn) {
            jQuery("#griffinforms-limitentriescount-wrap").show(200);
            jQuery("#griffinforms-limitentriesmessage-wrap").show(200);
        } else {
            jQuery("#griffinforms-limitentriescount-wrap").hide(200);
            jQuery("#griffinforms-limitentriesmessage-wrap").hide(200);
        }
    }

    function showAvailabilityWindowSuboptions() {
        var isOn = jQuery("#griffinforms-toggleavailabilitywindow-togglecheckbox").is(":checked");
        if (isOn) {
            jQuery("#griffinforms-availabilitywindowstart-wrap").show(200);
            jQuery("#griffinforms-availabilitywindowend-wrap").show(200);
            jQuery("#griffinforms-availabilitywindowpremessage-wrap").show(200);
            jQuery("#griffinforms-availabilitywindowpostmessage-wrap").show(200);
        } else {
            jQuery("#griffinforms-availabilitywindowstart-wrap").hide(200);
            jQuery("#griffinforms-availabilitywindowend-wrap").hide(200);
            jQuery("#griffinforms-availabilitywindowpremessage-wrap").hide(200);
            jQuery("#griffinforms-availabilitywindowpostmessage-wrap").hide(200);
        }
    }
    
    function showTogglePublicAccessSuboptions() {
        var fieldValue = jQuery("input[name='toggle_public_access']:checked").val();
        if (fieldValue === "registered_access") {
            jQuery("#griffinforms-toggleroleaccess-wrap").show(200);
        } else {
            jQuery("#griffinforms-toggleroleaccess-togglecheckbox").prop("checked", false);
            jQuery("#griffinforms-toggleroleaccess-wrap").hide(200);
            jQuery("#griffinforms-selectuserroles-wrap").hide(200);
        }
    }
    
    function showToggleRoleAccessSuboptions() {
        var isOn = jQuery("#griffinforms-toggleroleaccess-togglecheckbox").is(":checked");
        if (isOn) {
            jQuery("#griffinforms-selectuserroles-wrap").show(200);
        } else {
            jQuery("#griffinforms-selectuserroles-wrap").hide(200);
        }
    }
    
    function showAlertEmailsToSuboptions() {
        var fieldValue = jQuery("input[name='entry_alerts_to']:checked").val();
        if (fieldValue === "specific_emails") {
            jQuery("#griffinforms-entryalertsemails-wrap").show(200);
        } else {
            jQuery("#griffinforms-entryalertsemails-wrap").hide(200);
        }
    }
        
    function showUserActionSuboptions() {
        var actionType = jQuery("input[name='action_type']:checked").val();
        
        if (actionType === "do_nothing") {
            jQuery("#griffinforms-successmessage-wrap").hide(200);
            jQuery("#griffinforms-redirectionurl-wrap").hide(200);
            return;
        } 
        
        if (actionType === "show_success_message") {
            jQuery("#griffinforms-successmessage-wrap").show(200);
            jQuery("#griffinforms-redirectionurl-wrap").hide(200);
            return;
        }        
        
        if (actionType === "redirect_user") {
            jQuery("#griffinforms-redirectionurl-wrap").show(200);
            jQuery("#griffinforms-successmessage-wrap").hide(200);
            return;
        }
    }
    
        
    function showToggleAutoresponderSuboptions() {
        var isOn = jQuery("#griffinforms-toggleautoresponder-togglecheckbox").prop("checked") &&
                   !jQuery("#griffinforms-toggleautoresponder-togglecheckbox").prop("disabled");
        if (isOn) {
            jQuery("#griffinforms-autoresponder-wrap").show(200);
            jQuery("#griffinforms-map-emailfields-wrap").show(200);
        } else {
            jQuery("#griffinforms-autoresponder-wrap").hide(200);
            jQuery("#griffinforms-map-emailfields-wrap").hide(200);
        }
    }

    function showComplianceSuboptions() {
        var isOn = jQuery("#griffinforms-compliancemode-togglecheckbox").is(":checked");
        var mode = jQuery("#griffinforms-complianceflagmode-selectfield").val() || "inherit";
        if (mode !== "custom") {
            jQuery("#griffinforms-compliance-custom-options-wrap").hide(200);
            jQuery("#griffinforms-complianceprofile-wrap").hide(200);
            jQuery("#griffinforms-complianceflags-custom-wrap").hide(200);
            return;
        }

        jQuery("#griffinforms-compliance-custom-options-wrap").show(200);
        if (isOn) {
            jQuery("#griffinforms-complianceprofile-wrap").show(200);
            jQuery("#griffinforms-complianceflags-custom-wrap").show(200);
        } else {
            jQuery("#griffinforms-complianceprofile-wrap").hide(200);
            jQuery("#griffinforms-complianceflags-custom-wrap").hide(200);
        }
    }

    function showComplianceFlagModeSuboptions() {
        showComplianceSuboptions();
    }

    function resetLifetimeCount(e) {
        e.preventDefault();
        var $btn = jQuery(this);
        var formId = $btn.data("gfFormId");
        var nonce = $btn.data("gfNonce");
        var confirmText = $btn.data("gfConfirm") || "Are you sure?";
        var successText = $btn.data("gfSuccess") || "Lifetime count reset.";

        if (!formId || !nonce) {
            alert("Missing form data.");
            return;
        }

        if (!window.confirm(confirmText)) {
            return;
        }

        $btn.prop("disabled", true);

        jQuery.post(ajaxurl, {
            action: "griffinforms_reset_lifetime_submission_count",
            form_id: formId,
            nonce: nonce
        })
        .done(function(response) {
            if (response && response.success && response.data) {
                var count = response.data.count !== undefined ? response.data.count : 0;
                jQuery("[data-gf-lifetime-count][data-gf-form-id='" + formId + "']")
                    .text(count)
                    .attr("data-gf-lifetime-count-value", count);
                alert(successText);
            } else {
                var message = response && response.data && response.data.message ? response.data.message : "Unable to reset count.";
                alert(message);
            }
        })
        .fail(function() {
            alert("Unable to reset count.");
        })
        .always(function() {
            $btn.prop("disabled", false);
        });
    }
});
