<?php
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Build login page inline CSS string
 *
 * Returns a string of CSS rules based on the plugin's stored settings.
 * Used by admilogu_enqueue_login_assets() to inject styles via wp_add_inline_style().
 *
 * @return string CSS rules string.
 */
function admilogu_build_login_css()
{
    $logo = get_option('admilogu_logo_url', admilogu_DEFAULT_LOGO);
    $logo_width = get_option('admilogu_logo_width', '100%');
    $logo_height = get_option('admilogu_logo_height', '80px');
    $logo_bg_color = get_option('admilogu_logo_bg_color', '');
    $bg_type = get_option('admilogu_bg_type', 'color');
    $bg_image = get_option('admilogu_bg_image', admilogu_DEFAULT_BG);
    $bg_color = get_option('admilogu_bg_color', '#f0f0f1');
    $lostpwd_color = get_option('admilogu_lostpwd_color', '#2271b1');
    $backtoblog_color = get_option('admilogu_backtoblog_color', '#2271b1');
    $lostpwd_hover = get_option('admilogu_lostpwd_hover_color', '#135e96');
    $backtoblog_hover = get_option('admilogu_backtoblog_hover_color', '#135e96');
    $hide_backtoblog = get_option('admilogu_hide_backtoblog', 0);
    $hide_logo = get_option('admilogu_hide_logo', 0);
    $center_form = get_option('admilogu_center_form', 0);
    $form_bg = get_option('admilogu_loginform_bg_color', '#ffffff');
    $form_border = get_option('admilogu_loginform_border_color', '#c3c4c7');
    $form_label = get_option('admilogu_loginform_label_color', '#3c434a');
    $btn_bg = get_option('admilogu_btn_bg_color', '#2271b1');
    $btn_text = get_option('admilogu_btn_text_color', '#ffffff');
    $btn_bg_hover = get_option('admilogu_btn_bg_hover_color', '#135e96');
    $btn_text_hover = get_option('admilogu_btn_text_hover_color', '#ffffff');

    $css = '';

    // Logo
    if ($hide_logo) {
        $css .= '#login h1 { display: none !important; }';
    } elseif ($logo) {
        $css .= '#login h1 a {';
        $css .= 'background-image: url(' . esc_url($logo) . ') !important;';
        $css .= 'background-size: contain !important;';
        $css .= 'width: ' . esc_attr($logo_width) . ' !important;';
        $css .= 'height: ' . esc_attr($logo_height) . ' !important;';
        if ($logo_bg_color) {
            $css .= 'background-color: ' . esc_attr($logo_bg_color) . ' !important;';
        }
        $css .= '}';
    }

    // Background
    if ($bg_type === 'color' && $bg_color) {
        $css .= 'body.login { background-color: ' . esc_attr($bg_color) . ' !important; background-image: none !important; }';
    } elseif ($bg_type === 'image' && $bg_image) {
        $css .= 'body.login {';
        $css .= 'background-image: url(' . esc_url($bg_image) . ') !important;';
        $css .= 'background-size: cover !important;';
        $css .= 'background-position: center !important;';
        $css .= 'background-repeat: no-repeat !important;';
        $css .= 'background-attachment: fixed !important;';
        $css .= '}';
    } elseif ($bg_type === 'video') {
        $css .= 'body.login { background: transparent !important; }';
    }

    // Centering form
    if ($center_form) {
        $css .= 'body.login { display: flex !important; flex-direction: column !important; justify-content: center !important; align-items: center !important; min-height: 100vh !important; }';
        $css .= 'body.login #login { padding: 0 !important; margin: auto !important; }';
    }

    // Video background element
    $css .= '.adminlgb-video-bg {';
    $css .= 'position: fixed; top: 50%; left: 50%;';
    $css .= 'min-width: 100%; min-height: 100%;';
    $css .= 'width: auto; height: auto;';
    $css .= 'z-index: -100;';
    $css .= 'transform: translate(-50%, -50%);';
    $css .= 'object-fit: cover;';
    $css .= '}';

    // Login form
    $css .= 'body.login form {';
    if ($form_bg) {
        $css .= 'background: ' . esc_attr($form_bg) . ' !important;';
    }
    if ($form_border) {
        $css .= 'border-color: ' . esc_attr($form_border) . ' !important;';
    }
    $css .= 'border-width: 2px !important; border-style: solid !important;';
    $css .= 'box-shadow: 0 1px 3px rgba(0,0,0,.13) !important;';
    $css .= '}';

    // Form labels
    if ($form_label) {
        $css .= 'body.login label { color: ' . esc_attr($form_label) . ' !important; }';
    }

    // Button
    if ($btn_bg) {
        $css .= 'body.login .button-primary {';
        $css .= 'background: ' . esc_attr($btn_bg) . ' !important;';
        $css .= 'border-color: ' . esc_attr($btn_bg) . ' !important;';
        $css .= 'box-shadow: 0 1px 0 ' . esc_attr($btn_bg) . ' !important;';
        $css .= 'text-shadow: none !important;';
        if ($btn_text) {
            $css .= 'color: ' . esc_attr($btn_text) . ' !important;';
        }
        $css .= '}';
    }

    if ($btn_bg_hover) {
        $css .= 'body.login .button-primary:hover, body.login .button-primary:focus {';
        $css .= 'background: ' . esc_attr($btn_bg_hover) . ' !important;';
        $css .= 'border-color: ' . esc_attr($btn_bg_hover) . ' !important;';
        $css .= 'box-shadow: 0 1px 0 ' . esc_attr($btn_bg_hover) . ' !important;';
        if ($btn_text_hover) {
            $css .= 'color: ' . esc_attr($btn_text_hover) . ' !important;';
        }
        $css .= 'opacity: 1 !important;';
        $css .= '}';
    }

    // Nav / back-to-blog links
    $css .= '#nav a, #backtoblog a { text-shadow: none !important; }';

    if ($lostpwd_color) {
        $css .= '#nav a { color: ' . esc_attr($lostpwd_color) . ' !important; }';
    }
    if ($lostpwd_hover) {
        $css .= '#nav a:hover { color: ' . esc_attr($lostpwd_hover) . ' !important; }';
    }

    if ($hide_backtoblog) {
        $css .= '#backtoblog { display: none !important; }';
    } else {
        if ($backtoblog_color) {
            $css .= '#backtoblog a { color: ' . esc_attr($backtoblog_color) . ' !important; }';
        }
        if ($backtoblog_hover) {
            $css .= '#backtoblog a:hover { color: ' . esc_attr($backtoblog_hover) . ' !important; }';
        }
    }



    // Custom CSS
    $custom_css = get_option('admilogu_custom_css', '');
    if (!empty($custom_css)) {
        $css .= ' ' . wp_strip_all_tags($custom_css);
    }

    return $css;
}

/**
 * Enqueue login page assets (CSS and JS)
 *
 * Registers placeholder handles and attaches inline CSS/JS via the proper
 * WordPress API (wp_add_inline_style / wp_add_inline_script).
 * Hooked to login_enqueue_scripts so assets are loaded through WordPress's
 * asset pipeline rather than being echoed directly into the page.
 */
function admilogu_enqueue_login_assets()
{
    // ── Inline CSS ──────────────────────────────────────────────────────────
    wp_register_style('admilogu-login', false, array(), '1.0.0'); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
    wp_enqueue_style('admilogu-login');
    wp_add_inline_style('admilogu-login', admilogu_build_login_css());

    // ── Custom Font ─────────────────────────────────────────────────────────
    $font = get_option('admilogu_custom_font', '');
    if (!empty($font)) {
        $font_url = 'https://fonts.googleapis.com/css2?family=' . urlencode($font) . ':wght@400;500;600&display=swap';
        wp_enqueue_style('admilogu-custom-font', $font_url, array(), null); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
        wp_add_inline_style('admilogu-login', 'body.login { font-family: "' . esc_attr($font) . '", sans-serif !important; }');
    }

    // ── Inline JS (open logo link in new tab + Custom JS) ────────────────────
    wp_register_script('admilogu-login-script', false, array(), '1.0.0', true); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
    wp_enqueue_script('admilogu-login-script');

    if (get_option('admilogu_logo_target_blank', 1)) {
        wp_add_inline_script(
            'admilogu-login-script',
            'document.addEventListener("DOMContentLoaded", function () {
                var link = document.querySelector("#login h1 a");
                if (link) {
                    link.setAttribute("target", "_blank");
                    link.setAttribute("rel", "noopener noreferrer");
                }
            });'
        );
    }

    $custom_js = get_option('admilogu_custom_js', '');
    if (!empty($custom_js)) {
        wp_add_inline_script('admilogu-login-script', wp_strip_all_tags($custom_js));
    }

    // ── Lockout Countdown Script ─────────────────────────────────────────────
    wp_enqueue_script(
        'admilogu-lockout-timer',
        admilogu_URL . 'assets/js/admilogu_lockout.js',
        array(),
        '1.0.0',
        true
    );
}
add_action('login_enqueue_scripts', 'admilogu_enqueue_login_assets');

/**
 * Render video background HTML
 *
 * If the background type is set to video, this function outputs the <video>
 * element in the login page footer.
 */
function admilogu_render_video_bg()
{
    $bg_type = get_option('admilogu_bg_type', 'color');
    $bg_video = get_option('admilogu_bg_video', admilogu_DEFAULT_VIDEO);
    $bg_video_mute = get_option('admilogu_bg_video_mute', 1);

    if ($bg_type === 'video' && !empty($bg_video)) {
        $muted = $bg_video_mute ? 'muted' : '';
        echo '<video autoplay loop ' . esc_attr($muted) . ' playsinline class="adminlgb-video-bg">';
        echo '<source src="' . esc_url($bg_video) . '" type="video/mp4">';
        echo '</video>';
    }
}
add_action('login_footer', 'admilogu_render_video_bg');

/**
 * Customize Login Logo URL
 *
 * Filters the URL of the login logo to point to the custom link
 * configured in the plugin settings.
 *
 * @param string $url The original logo URL.
 * @return string The modified logo link.
 */
function admilogu_login_logo_url($url)
{
    return get_option('admilogu_logo_link', 'https://wordpress.org/');
}
add_filter('login_headerurl', 'admilogu_login_logo_url');

/**
 * Customize Login Logo Title
 *
 * Filters the title attribute of the login logo to use the custom title
 * configured in the plugin settings.
 *
 * @param string $title The original logo title.
 * @return string The modified logo title.
 */
function admilogu_login_logo_title($title)
{
    return get_option('admilogu_logo_title', '');
}
add_filter('login_headertext', 'admilogu_login_logo_title');

/**
 * Render Custom Footer
 * 
 * Outputs the custom footer HTML configured in the settings.
 */
function admilogu_render_custom_footer()
{
    $footer = get_option('admilogu_custom_footer', '');
    if (!empty($footer)) {
        echo '<div class="admilogu-custom-footer" style="text-align:center; padding: 20px; position: relative; z-index: 10;">' . wp_kses_post($footer) . '</div>';
    }
}
add_action('login_footer', 'admilogu_render_custom_footer');



