<?php

/**
 * All the user and front-end related features handle from this file
 * Key function name user_setup
 */

if (!defined('ABSPATH')) {
    exit;
}

// Include hooks file here
$sapmfly_user_all_hooks = SAPMFLY_TEMPLATES . 'user/all-hooks.php';
if (file_exists($sapmfly_user_all_hooks)) {
    require_once $sapmfly_user_all_hooks;
}

// User setup starts here
function sapmfly_user_setup()
{
    // Save option on form submission with nonce verification
    if (isset($_POST['sapmfly_save_settings'])) {
        if (
            isset($_POST['sapmfly_settings_nonce']) &&
            check_admin_referer('sapmfly_save_settings_action', 'sapmfly_settings_nonce')
        ) {
            // Save each option individually
            $enable_progress_bar = isset($_POST['sapmfly_enable_progress_bar']) ? 'yes' : 'no';
            update_option('sapmfly_enable_progress_bar', $enable_progress_bar);

            $enable_back_top_button = isset($_POST['sapmfly_enable_back_top_button']) ? 'yes' : 'no';
            update_option('sapmfly_enable_back_top_button', $enable_back_top_button);

            $disable_mouse_right_click_btn = isset($_POST['sapmfly_disable_mouse_right_click_button']) ? 'yes' : 'no';
            update_option('sapmfly_disable_mouse_right_click_button', $disable_mouse_right_click_btn);

            $disable_all_comments_btn = isset($_POST['sapmfly_disable_all_comments_button']) ? 'yes' : 'no';
            update_option('sapmfly_disable_all_comments_button', $disable_all_comments_btn);

            $dark_mood_for_users = isset($_POST['sapmfly_dark_mood_for_users_button']) ? 'yes' : 'no';
            update_option('sapmfly_dark_mood_for_users_button', $dark_mood_for_users);

            $hide_toolbar_user = isset($_POST['sapmfly_hide_toolbar_user']) ? 'yes' : 'no';
            update_option('sapmfly_hide_toolbar_user', $hide_toolbar_user);

            $maintenance_mood = isset($_POST['sapmfly_maintenance_mood']) ? 'yes' : 'no';
            update_option('sapmfly_maintenance_mood', $maintenance_mood);

            $ai_summary = isset($_POST['sapmfly_ai_summary']) ? 'yes' : 'no';
            update_option('sapmfly_ai_summary', $ai_summary);

            $click_to_talk = isset($_POST['sapmfly_click_to_talk']) ? 'yes' : 'no';
            update_option('sapmfly_click_to_talk', $click_to_talk);

            $click_to_call = isset($_POST['sapmfly_click_to_call']) ? 'yes' : 'no';
            update_option('sapmfly_click_to_call', $click_to_call);

            $christmas_snowfall = isset($_POST['sapmfly_christmas_snowfall']) ? 'yes' : 'no';
            update_option('sapmfly_christmas_snowfall', $christmas_snowfall);

            $add_sitewide_floating_button = isset($_POST['sapmfly_sitewide_floating_button']) ? 'yes' : 'no';
            update_option('sapmfly_sitewide_floating_button', $add_sitewide_floating_button);

            $global_toggle_value = isset($_POST['sapmfly_widgets_toggle']) ? 'yes' : 'no';
            update_option('sapmfly_widgets_toggle', $global_toggle_value);
        } else {
            wp_die(esc_html__('Security check failed. Please try again.', 'minifly'));
        }
    }

    // Get saved option values
    $progress_enabled = get_option('sapmfly_enable_progress_bar', 'no');
    $back_top_enabled = get_option('sapmfly_enable_back_top_button', 'no');
    $disable_mouse_right_click = get_option('sapmfly_disable_mouse_right_click_button', 'no');
    $disable_all_comments = get_option('sapmfly_disable_all_comments_button', 'no');
    $dark_mood_for_users = get_option('sapmfly_dark_mood_for_users_button', 'no');
    $hide_toolbar_user = get_option('sapmfly_hide_toolbar_user', 'no');
    $maintenance_mood = get_option('sapmfly_maintenance_mood', 'no');
    $ai_summary = get_option('sapmfly_ai_summary', 'no');
    $click_to_talk = get_option('sapmfly_click_to_talk', 'no');
    $click_to_call = get_option('sapmfly_click_to_call', 'no');
    $christmas_snowfall = get_option('sapmfly_christmas_snowfall', 'no');
    $add_sitewide_floating_button = get_option('sapmfly_sitewide_floating_button', 'no');

    // Save global save option
    $global_toggle_value = get_option('sapmfly_widgets_toggle', 'no');

?>

    <!-- User page starts here -->
    <div class="sapmfly-user-setup-wrap">
        <div class="sapmfly-user-setup-page sapmfly-fonts">
            <div class="sapmfly-plugin-global-headings">
                <?php
                $sapmfly_promotional_header_template = defined('SAPMFLY_TEMPLATES') ? SAPMFLY_TEMPLATES . 'global-header-design.php' : '';
                if ($sapmfly_promotional_header_template && file_exists($sapmfly_promotional_header_template)) {
                    require_once $sapmfly_promotional_header_template;
                }
                ?>
            </div>

            <div class="sapmfly-user-setup-page-body">
                <div class="sapmfly-page-heading">
                    <h1><?php echo esc_html__('Frontend Features and Website Enhancements', 'minifly'); ?></h1>
                    <p><?php echo esc_html__('Access and control all your frontend tools and essential website features from this page. Toggle on for necessary options.', 'minifly'); ?></p>
                </div>

                <div class="sapmfly-user-setup-settings-area">
                    <form method="post" id="sapmfly-settings-form">
                        <div class="sapmfly-user-setup-form">
                            <div class="sapmfly-user-setup-header">
                                <div class="sapmfly-user-setup-header-title">
                                    <h2><?php echo esc_html__('Setup user end options here', 'minifly'); ?></h2>
                                </div>
                                <div class="sapmfly-user-setup-header-controls">
                                    <span class="sapmfly-toggle-label sapmfly-disable-all"><?php echo esc_html__('Disable All', 'minifly'); ?></span>

                                    <!-- Switch Toggle -->
                                    <label class="sapmfly-switch">
                                        <input type="checkbox" id="sapmfly-global-toggle" name="sapmfly_widgets_toggle" <?php checked($global_toggle_value, 'yes'); ?> />
                                        <span class="sapmfly-slider round"></span>
                                    </label>

                                    <span class="sapmfly-toggle-label sapmfly-enable-all"><?php echo esc_html__('Enable All', 'minifly'); ?></span>

                                    <!-- Save Button -->
                                    <button type="submit" name="sapmfly_save_settings" class="sapmfly-save-btn"><?php echo esc_html__('Save Changes', 'minifly'); ?></button>
                                </div>
                            </div>

                            <hr />
                            <div class="sapmfly-user-setup-settings-toggle">

                                <?php wp_nonce_field('sapmfly_save_settings_action', 'sapmfly_settings_nonce'); ?>

                                <div class="sapmfly-toggle-wrapper">

                                    <!-- Progress Bar Option -->
                                    <?php
                                    $sapmfly_progress_bar = SAPMFLY_TEMPLATES . 'user/features/progress-bar.php';
                                    if (file_exists($sapmfly_progress_bar)) {
                                        require_once $sapmfly_progress_bar;
                                    }
                                    ?>

                                    <!-- Back to Top Option -->
                                    <?php
                                    $sapmfly_back_to_top = SAPMFLY_TEMPLATES . 'user/features/back-to-top.php';
                                    if (file_exists($sapmfly_back_to_top)) {
                                        require_once $sapmfly_back_to_top;
                                    }
                                    ?>

                                    <!-- Disable mouse right click -->
                                    <?php
                                    $sapmfly_right_click = SAPMFLY_TEMPLATES . 'user/features/disable-right-click.php';
                                    if (file_exists($sapmfly_right_click)) {
                                        require_once $sapmfly_right_click;
                                    }
                                    ?>

                                    <!-- Disable all comments -->
                                    <?php
                                    $sapmfly_disable_comments = SAPMFLY_TEMPLATES . 'user/features/disable-comments.php';
                                    if (file_exists($sapmfly_disable_comments)) {
                                        require_once $sapmfly_disable_comments;
                                    }
                                    ?>

                                    <!-- Enable dark mood for front end -->
                                    <?php
                                    $sapmfly_dark_mood = SAPMFLY_TEMPLATES . 'user/features/dark-mood-user.php';
                                    if (file_exists($sapmfly_dark_mood)) {
                                        require_once $sapmfly_dark_mood;
                                    }
                                    ?>

                                    <!-- Enable dark mood for back end -->
                                    <?php
                                    $sapmfly_hide_user_toolbar = SAPMFLY_TEMPLATES . 'user/features/hide-toolbar-user.php';
                                    if (file_exists($sapmfly_hide_user_toolbar)) {
                                        require_once $sapmfly_hide_user_toolbar;
                                    }
                                    ?>


                                    <!-- Enable enable maintenance mood -->
                                    <?php
                                    $sapmfly_enable_maintenance = SAPMFLY_TEMPLATES . 'user/features/enable-maintenance-mood.php';
                                    if (file_exists($sapmfly_enable_maintenance)) {
                                        require_once $sapmfly_enable_maintenance;
                                    }
                                    ?>

                                    <!-- Enable dark mood for back end -->
                                    <?php
                                    $sapmfly_ai_summary_btn = SAPMFLY_TEMPLATES . 'user/features/post-ai-summary.php';
                                    if (file_exists($sapmfly_ai_summary_btn)) {
                                        require_once $sapmfly_ai_summary_btn;
                                    }
                                    ?>

                                    <!-- Click to contact via WhatsApp -->
                                    <?php
                                    $sapmfly_click_contact = SAPMFLY_TEMPLATES . 'user/features/click-to-contact.php';
                                    if (file_exists($sapmfly_click_contact)) {
                                        require_once $sapmfly_click_contact;
                                    }
                                    ?>

                                    <!-- Click to call button -->
                                    <?php
                                    $sapmfly_click_call = SAPMFLY_TEMPLATES . 'user/features/click-to-call.php';
                                    if (file_exists($sapmfly_click_call)) {
                                        require_once $sapmfly_click_call;
                                    }
                                    ?>

                                    <!-- Enable dark mood for back end -->
                                    <?php
                                    $sapmfly__christmas_snowfall = SAPMFLY_TEMPLATES . 'user/features/enable-christmas-snowfall.php';
                                    if (file_exists($sapmfly__christmas_snowfall)) {
                                        require_once $sapmfly__christmas_snowfall;
                                    }
                                    ?>

                                    <!-- Enable sitewide floating Button -->
                                    <?php
                                    $sapmfly_sitewide_floating_btn = SAPMFLY_TEMPLATES . 'user/features/sitewide-floating-button.php';
                                    if (file_exists($sapmfly_sitewide_floating_btn)) {
                                        require_once $sapmfly_sitewide_floating_btn;
                                    }
                                    ?>

                                </div>
                                <br><br>

                                <button type="submit" name="sapmfly_save_settings" class="sapmfly-save-btn"><?php echo esc_html__('Save Changes', 'minifly'); ?></button>

                                <br />
                                <br />

                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <!-- User page ends here -->
<?php
}
