<?php 
    global $syrus;
    global $syrusSettings;
    global $syrusAssetsHandler;

    $infinite_scroll_chk = $syrus->get_option("generali","infinite_scroll");
    $infinite_scroll_home_chk = $syrus->get_option("generali","infinite_scroll_home");
    $span_replace_chk = $syrus->get_option("generali","span_replace");
    $p_cleaner_chk = $syrus->get_option("generali","p_cleaner");
    $http_cleaner_chk = $syrus->get_option("generali","http_cleaner");
    $controlli_seo_chk = explode(',',$syrus->get_option("generali","controlli_seo"));

    $js_scheme = $syrusAssetsHandler->get_js_scheme();
    $css_scheme = $syrusAssetsHandler->get_css_scheme();

    $controlli_seo_checkbox = [
        'tag-h1-presente' => "H1 tag presence in content",
        'tag-h2-non-presente' => "At least one H2 tag required",
        'tag-h3-non-presente' => "At least one H3 tag required",
        'tag-h2-uguale-al-titolo' => "H2 tags identical to article title",
        'tag-h2-e-h3-duplicato' => "H2 tags identical to H3 tags",
        'min-500-parole' => "Article must contain at least 500 words",
        'immagine-in-evidenza-non-presente' => "Every post must have a featured image",
    ];

    $chatgpt_token = $syrus->get_option('generali','chatgpt_token');
?>

<div class="syrus-settings-layout">
    <div class="syrus-settings-sidebar">
        <h3>Settings Navigation</h3>
        <ul class="syrus-sidebar-nav">
            <li><a href="#basic-settings"><i class="dashicons dashicons-admin-generic"></i>Basic Settings</a></li>
            <li><a href="#content-filters"><i class="dashicons dashicons-admin-tools"></i>Content Filters</a></li>
            <li><a href="#seo-controls"><i class="dashicons dashicons-search"></i>SEO Controls</a></li>
            <li><a href="#api-integration"><i class="dashicons dashicons-admin-plugins"></i>API Integration</a></li>
            <li><a href="#advanced-settings"><i class="dashicons dashicons-admin-settings"></i>Advanced Settings</a></li>
        </ul>
    </div>

    <div class="syrus-settings-content">

        <form action="<?php echo admin_url('admin-post.php'); ?>" method="POST">
            <input type="hidden" name="action" value="salva_impostazioni_generali">

            <div id="basic-settings" class="syrus-settings-section">
                <h2 class="syrus-section-title">
                    <i class="dashicons dashicons-admin-generic"></i>
                    Basic Settings
                </h2>
                
                <div class="syrus-form-group">
                    <div class="syrus-form-row">
                        <label class="syrus-form-label" for="infinite-scroll-chk">
                            Infinite scroll (single post)
                        </label>
                        <div class="syrus-toggle-switch">
                            <input type="checkbox" name="infinite_scroll_chk" value="1" id="infinite-scroll-chk" <?php echo $infinite_scroll_chk && $infinite_scroll_chk == 1 ? 'checked' : ''; ?>>
                            <span class="syrus-toggle-slider"></span>
                        </div>
                    </div>
                    <div class="syrus-hint"><?php esc_html_e("Enable infinite scroll for single post pages",'syrus') ?></div>
                </div>

                <div class="syrus-form-group">
                    <div class="syrus-form-row">
                        <label class="syrus-form-label" for="infinite-scroll-home-chk">
                            Infinite scroll (home page)
                        </label>
                        <div class="syrus-toggle-switch">
                            <input type="checkbox" name="infinite_scroll_home_chk" value="1" id="infinite-scroll-home-chk" <?php echo $infinite_scroll_home_chk && $infinite_scroll_home_chk == 1 ? 'checked' : ''; ?>>
                            <span class="syrus-toggle-slider"></span>
                        </div>
                    </div>
                    <div class="syrus-hint"><?php esc_html_e("Enable infinite scroll for home page without pagination",'syrus') ?></div>
                </div>
            </div>

            <div id="content-filters" class="syrus-settings-section">
                <h2 class="syrus-section-title">
                    <i class="dashicons dashicons-admin-tools"></i>
                    Content Filters
                </h2>
                
                <div class="syrus-form-group">
                    <div class="syrus-form-row">
                        <label class="syrus-form-label" for="span-replace-chk">
                            HTML Tag &lt;span&gt; remover
                            <span class="syrus-seo-badge">SEO</span>
                        </label>
                        <div class="syrus-toggle-switch">
                            <input type="checkbox" name="span_replace_chk" value="1" id="span-replace-chk" <?php echo $span_replace_chk && $span_replace_chk == 1 ? 'checked' : ''; ?>>
                            <span class="syrus-toggle-slider"></span>
                        </div>
                    </div>
                    <div class="syrus-hint"><?php esc_html_e("Remove all span tags from post content",'syrus') ?></div>
                </div>

                <div class="syrus-form-group">
                    <div class="syrus-form-row">
                        <label class="syrus-form-label" for="p-cleaner-chk">
                            HTML Tag &lt;p&gt; cleaner
                            <span class="syrus-seo-badge">SEO</span>
                        </label>
                        <div class="syrus-toggle-switch">
                            <input type="checkbox" name="p_cleaner_chk" value="1" id="p-cleaner-chk" <?php echo $p_cleaner_chk && $p_cleaner_chk == 1 ? 'checked' : ''; ?>>
                            <span class="syrus-toggle-slider"></span>
                        </div>
                    </div>
                    <div class="syrus-hint"><?php esc_html_e("Clean all paragraph tag attributes and inline styles",'syrus') ?></div>
                </div>

                <div class="syrus-form-group">
                    <div class="syrus-form-row">
                        <label class="syrus-form-label" for="http-cleaner-chk">
                            Replace HTTP to HTTPS
                            <span class="syrus-seo-badge">SEO</span>
                        </label>
                        <div class="syrus-toggle-switch">
                            <input type="checkbox" name="http_cleaner_chk" value="1" id="http-cleaner-chk" <?php echo $http_cleaner_chk && $http_cleaner_chk == 1 ? 'checked' : ''; ?>>
                            <span class="syrus-toggle-slider"></span>
                        </div>
                    </div>
                    <div class="syrus-hint"><?php esc_html_e("Automatically convert HTTP links to HTTPS",'syrus') ?></div>
                </div>
            </div>

            <div id="seo-controls" class="syrus-settings-section">
                <h2 class="syrus-section-title">
                    <i class="dashicons dashicons-search"></i>
                    SEO Controls
                </h2>
                
                <?php foreach($controlli_seo_checkbox as $key => $val) { ?>
                    <div class="syrus-form-group">
                        <div class="syrus-form-row">
                            <label class="syrus-form-label" for="<?php echo $key.'-chk'; ?>">
                                <?php echo $val; ?>
                                <span class="syrus-seo-badge">SEO</span>
                            </label>
                            <div class="syrus-toggle-switch">
                                <input type="checkbox" name="controlli_seo[]" value="<?php echo $key ?>" id="<?php echo $key.'-chk'; ?>" <?php echo $controlli_seo_chk && in_array($key,$controlli_seo_chk) ? 'checked' : ''; ?>>
                                <span class="syrus-toggle-slider"></span>
                            </div>
                        </div>
                    </div>
                <?php } ?>
            </div>

            <div id="api-integration" class="syrus-settings-section">
                <h2 class="syrus-section-title">
                    <i class="dashicons dashicons-admin-plugins"></i>
                    API Integration
                </h2>
                
                <div class="syrus-form-group">
                    <div class="syrus-form-row">
                        <label class="syrus-form-label" for="chatgpt_token">
                            ChatGPT API Token
                        </label>
                        <input type="text" class="syrus-form-input" name="chatgpt_token" id="chatgpt_token" placeholder="Enter your ChatGPT API token" value="<?php echo $chatgpt_token ?: '' ?>">
                    </div>
                    <div class="syrus-hint">Enter your OpenAI API key to enable AI content generation</div>
                </div>

                <div class="syrus-form-group">
                    <div class="syrus-form-row">
                        <label class="syrus-form-label" for="deepl_api_key">
                            DeepL API Key
                        </label>
                        <input type="text" class="syrus-form-input" name="deepl_api_key" id="deepl_api_key" placeholder="Enter your DeepL API key" value="<?php echo $syrus->get_option('generali','deepl_api_key') ?: '' ?>">
                    </div>
                    <div class="syrus-hint">Enter your DeepL API key to enable content translation in post editor</div>
                </div>
            </div>

            <div id="advanced-settings" class="syrus-settings-section">
                <h2 class="syrus-section-title">
                    <i class="dashicons dashicons-admin-settings"></i>
                    Advanced Settings
                </h2>
                
                <h3 style="color: #0f6eb4; margin-bottom: 20px;">Theme Styles</h3>
                
                <?php foreach($css_scheme as $handle => $info) : $enqueue = $syrus->get_option("generali","enqueue_css_$handle"); ?>
                    <?php if(!str_contains($handle, 'admin')) : ?>
                        <div class="syrus-form-group">
                            <div class="syrus-form-row">
                                <div style="flex: 1;">
                                    <label class="syrus-form-label" for="enqueue-<?php echo $handle ?>">
                                        <?php echo $handle ?>
                                    </label>
                                    <div class="syrus-hint" style="margin: 0; padding: 0;"><?php echo $info['partial_url'] ?></div>
                                </div>
                                <div class="syrus-toggle-switch">
                                    <input type="checkbox" name="enqueue_css_<?php echo $handle ?>" value="1" id="enqueue-<?php echo $handle ?>" <?php echo $enqueue == '' || $enqueue == 'y' ? 'checked' : ''; ?>>
                                    <span class="syrus-toggle-slider"></span>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>
                <?php endforeach; ?>

                <h3 style="color: #0f6eb4; margin: 40px 0 20px 0;">Theme Scripts</h3>
                
                <?php foreach($js_scheme as $handle => $info) : $enqueue = $syrus->get_option("generali","enqueue_js_$handle"); ?>
                    <?php if(!str_contains($handle, 'admin') && $handle != 'syrus-customizer-js') : ?>
                        <div class="syrus-form-group">
                            <div class="syrus-form-row">
                                <div style="flex: 1;">
                                    <label class="syrus-form-label" for="enqueue-<?php echo $handle ?>">
                                        <?php echo $handle ?>
                                    </label>
                                    <div class="syrus-hint" style="margin: 0; padding: 0;"><?php echo $info['partial_url'] ?></div>
                                </div>
                                <div class="syrus-toggle-switch">
                                    <input type="checkbox" name="enqueue_js_<?php echo $handle ?>" value="1" id="enqueue-<?php echo $handle ?>" <?php echo $enqueue == '' || $enqueue == 'y' ? 'checked' : ''; ?>>
                                    <span class="syrus-toggle-slider"></span>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>
                <?php endforeach; ?>
            </div>

            <div style="text-align: center; padding: 30px 0;">
                <button type="submit" class="syrus-save-button">
                    <i class="dashicons dashicons-yes" style="margin-right: 8px;"></i>
                    Save Settings
                </button>
            </div>

        </form>
    </div>
</div>