<?php
/**
 * Plugin Name: Uplink7 Skeleton Loader
 * Description: Professional shimmer skeleton screens. High-end branding, smart dark mode, and layout options.
 * Version: 1.1
 * Update URI: https://api.freemius.com
 * Author URI: https://uplink7.com
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Author: Uplink7
 * Text Domain: uplink7-skeleton-loader
 */

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

// ==========================================
// 1. FREEMIUS SDK INTEGRATION
// ==========================================
if ( !function_exists( 'ul7_sl_fs' ) ) {
    function ul7_sl_fs() {
        global $ul7_sl_fs;
        if ( !isset( $ul7_sl_fs ) ) {
            $sdk_path = dirname( __FILE__ ) . '/lib/freemius/start.php';
            if ( !file_exists( $sdk_path ) ) {
                return false;
            }
            require_once $sdk_path;
            $ul7_sl_fs = fs_dynamic_init( [
                'id'               => '27901',
                'slug'             => 'uplink7-skleton-loader',
                'type'             => 'plugin',
                'public_key'       => 'pk_9638b63b5a946be863cac419055b3',
                'is_premium'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'menu'             => [
                    'slug'   => 'uplink7-skleton-loader',
                    'parent' => [
                        'slug' => 'options-general.php',
                    ],
                ],
                'is_live'          => true,
            ] );
        }
        return $ul7_sl_fs;
    }
    ul7_sl_fs();
}

// ==========================================
// 2. ADMIN UI ASSETS
// ==========================================
add_action('admin_head', function() {
    echo '<style>
        .ul7-help-badge { display:inline-block; width:18px; height:18px; background:#0073aa; color:#fff; border-radius:50%; text-align:center; font-size:12px; line-height:18px; cursor:help; margin-left:8px; font-weight:bold; }
        .ul7-help-badge:hover::after { content: attr(data-help); position:absolute; background:#2c3338; color:#fff; padding:12px; border-radius:6px; font-size:13px; width:260px; z-index:999; line-height:1.4; font-weight:normal; box-shadow:0 5px 15px rgba(0,0,0,0.3); margin-left:10px; }
        .ul7-settings-card { background:#fff; border:1px solid #ccd0d4; padding:25px; border-radius:8px; margin-top:20px; max-width:900px; }
    </style>';
});

// ==========================================
// 3. SETTINGS PAGE
// ==========================================
add_action('admin_menu', function() {
    add_options_page('Uplink7 Loader', 'Uplink7 Loader', 'manage_options', 'uplink7-skleton-loader', 'ul7_sl_settings_page');
});

add_action('admin_init', function() {
    // Nur noch Appearance-relevante Felder registrieren
    $fields = ['ul7_style', 'ul7_bg_col', 'ul7_shm_col', 'ul7_logo_url', 'ul7_dark_mode'];
    foreach($fields as $f) register_setting('ul7-sl-group', $f);
});

function ul7_sl_settings_page() {
    ?>
    <div class="wrap">
        <h1>Uplink7 Skeleton Loader <span style="color:#0073aa;">Dashboard</span></h1>
        <?php
		$ul7_sl_fs = ul7_sl_fs();
		if ( $ul7_sl_fs && ! $ul7_sl_fs->is_premium() ) : ?>
            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 30px; margin-top:0; width: 100%;">
                <div style="background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%); color: white; padding: 25px; border-radius: 12px; box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1);">
                    <h3 style="margin-top:0; color:white;">Start Free Trial</h3>
                    <p>Test all Premium features for 3 days for free. No obligation.</p>
                    <a href="<?php echo esc_url( $ul7_sl_fs->get_trial_url() ); ?>" class="button button-large" style="background: white; color: #6e8efb; border: none; font-weight: bold;">Try for free</a>
                </div>
                <div style="background: #fff; border: 2px solid #fbbf24; padding: 25px; border-radius: 12px; box-shadow: 0 10px 15px -3px rgba(0,0,0,0.05);">
                    <h3 style="margin-top:0;">Upgrade to Premium</h3>
                    <p>Unlock Statistics, more features and Priority Support.</p>
                    <a href="<?php echo esc_url( $ul7_sl_fs->get_upgrade_url() ); ?>" class="button button-large button-primary" style="background: #fbbf24; color: #000; border: none; font-weight: bold;">Upgrade Now</a>
                </div>
            </div>
		<?php endif; ?>
        <form method="post" action="options.php">
            <?php settings_fields('ul7-sl-group'); ?>

            <div class="ul7-settings-card">
                <h2>Appearance</h2>
                <table class="form-table">
                    <tr>
                        <th scope="row">Layout Style <span class="ul7-help-badge" data-help="Classic: Standard blog lines. Ecommerce: 3-column product grid. Minimal: Subtle text lines. Hero: Large top image block.">?</span></th>
                        <td>
                            <select name="ul7_style">
                                <option value="classic" <?php selected(get_option('ul7_style'), 'classic'); ?>>Classic (Blog)</option>
                                <option value="ecommerce" <?php selected(get_option('ul7_style'), 'ecommerce'); ?>>Ecommerce (Grid)</option>
                                <option value="minimal" <?php selected(get_option('ul7_style'), 'minimal'); ?>>Minimalist (Text)</option>
                                <option value="hero" <?php selected(get_option('ul7_style'), 'hero'); ?>>Hero (Big Image)</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">Logo URL <span class="ul7-help-badge" data-help="Full link to your logo (PNG/SVG/WebP). This logo will pulse in the center during loading.">?</span></th>
                        <td><input type="text" name="ul7_logo_url" value="<?php echo esc_attr(get_option('ul7_logo_url')); ?>" class="regular-text" placeholder="https://site.com/logo.png" /></td>
                    </tr>
                    <tr>
                        <th scope="row">Background Color</th>
                        <td><input type="color" name="ul7_bg_col" value="<?php echo esc_attr(get_option('ul7_bg_col', '#ffffff')); ?>" /></td>
                    </tr>
                    <tr>
                        <th scope="row">Shimmer Color</th>
                        <td><input type="color" name="ul7_shm_col" value="<?php echo esc_attr(get_option('ul7_shm_col', '#f0f0f0')); ?>" /></td>
                    </tr>
                    <tr>
                        <th scope="row">Dark Mode <span class="ul7-help-badge" data-help="Automatically switch to dark colors if the user has Dark Mode enabled in their OS.">?</span></th>
                        <td><input type="checkbox" name="ul7_dark_mode" value="1" <?php checked(get_option('ul7_dark_mode'), 1); ?> /> Auto-Detect Dark Mode</td>
                    </tr>
                </table>
            </div>

            <?php submit_button('Save Settings'); ?>
        </form>

        <div style="background: #fff; border: 2px solid #d63638; padding: 25px; margin-top: 20px; border-radius: 12px; display: flex; justify-content: space-between; align-items: center;">
            <div><h3>Support</h3><p>Documentation can be found here.</p></div>
            <a href="https://www.uplink7.com/dokumentation/uplink7-skeleton-loader-wordpress-plugin/" target="_blank" class="button button-primary" style="background:#f9c300; border-color:#000; color:#000;">Docs</a>
        </div>
    </div>
    <?php
}

// ==========================================
// 4. FRONTEND LOGIC
// ==========================================
function ul7_sl_can_render() {
    // Da Ausschlussoptionen entfernt wurden, wird der Loader nun generell gerendert
    return true;
}

add_action('wp_head', function() {
    if (!ul7_sl_can_render()) return;
    $bg = get_option('ul7_bg_col', '#ffffff');
    $sh = get_option('ul7_shm_col', '#f0f0f0');
    $dark = get_option('ul7_dark_mode');

    echo "<style id='ul7-sl-styles'>
        #ul7-sl-overlay { position:fixed; top:0; left:0; width:100%; height:100%; background:$bg; z-index:99999999; display:flex; flex-direction:column; padding:10vh 12%; gap:20px; transition: opacity 0.5s ease, transform 0.6s cubic-bezier(0.8, 0, 0.2, 1); visibility:visible; }
        .ul7-shimmer-box { background:$sh; background:linear-gradient(90deg, $sh 25%, #ffffff 50%, $sh 75%); background-size:200% 100%; animation:ul7-sh-anim 1.5s infinite linear; border-radius:10px; }
        @keyframes ul7-sh-anim { 0%{background-position:200% 0} 100%{background-position:-200% 0} }
        .ul7-logo-pulse { position:absolute; top:50%; left:50%; transform:translate(-50%,-50%); max-width:140px; animation:ul7-lp 2s infinite; }
        @keyframes ul7-lp { 0%,100%{opacity:0.6} 50%{opacity:1} }
        .type-ecommerce { flex-direction:row !important; flex-wrap:wrap; }
        .type-ecommerce .item { width:calc(33.3% - 14px); height:180px; }
        body.ul7-sl-ready #ul7-sl-overlay { opacity:0; visibility:hidden; pointer-events:none; }";
    
    if($dark) {
        echo "@media (prefers-color-scheme: dark) { 
            #ul7-sl-overlay { background:#121212 !important; } 
            .ul7-shimmer-box { background:#222; background:linear-gradient(90deg, #222 25%, #333 50%, #222 75%); } 
        }";
    }
    echo "</style>";
});

add_action('wp_body_open', 'ul7_sl_render_html');
add_action('wp_footer', 'ul7_sl_render_html');

function ul7_sl_render_html() {
    static $rendered = false; if ($rendered || !ul7_sl_can_render()) return; $rendered = true;
    $style = get_option('ul7_style', 'classic');
    $logo = get_option('ul7_logo_url');
    echo '<div id="ul7-sl-overlay" class="type-'.$style.'">';
    if($logo) echo '<img src="'.esc_url($logo).'" class="ul7-logo-pulse">';
    if($style == 'ecommerce') { for($i=0;$i<6;$i++) echo '<div class="ul7-shimmer-box item"></div>'; }
    elseif($style == 'minimal') { echo '<div class="ul7-shimmer-box" style="height:20px; width:40%;"></div><div class="ul7-shimmer-box" style="height:20px; width:100%;"></div>'; }
    elseif($style == 'hero') { echo '<div class="ul7-shimmer-box" style="height:45vh;"></div><div class="ul7-shimmer-box" style="height:30px;"></div>'; }
    else { echo '<div class="ul7-shimmer-box" style="height:50px; width:30%;"></div><div class="ul7-shimmer-box" style="height:20px;"></div><div class="ul7-shimmer-box" style="height:300px;"></div>'; }
    echo '</div>';
}

add_action('wp_footer', function() {
    if (!ul7_sl_can_render()) return;
    ?>
    <script>
        (function() {
            const h = () => { 
                document.body.classList.add('ul7-sl-ready'); 
                setTimeout(() => {
                    const overlay = document.getElementById('ul7-sl-overlay');
                    if(overlay) overlay.remove();
                }, 1000); 
            };
            window.addEventListener('load', h);
            window.addEventListener('wheel', h, {once:true});
            window.addEventListener('touchstart', h, {once:true});
            setTimeout(h, 4500); 
        })();
    </script>
    <?php
}, 999);