<?php
// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Convert hex color to rgba string with fixed alpha.
function lg_forge_hex_to_rgba( $hex, $alpha ) {
    $hex = ltrim( $hex, '#' );

    if ( 3 === strlen( $hex ) ) {
        $r = hexdec( str_repeat( substr( $hex, 0, 1 ), 2 ) );
        $g = hexdec( str_repeat( substr( $hex, 1, 1 ), 2 ) );
        $b = hexdec( str_repeat( substr( $hex, 2, 1 ), 2 ) );
    } elseif ( 6 === strlen( $hex ) ) {
        $r = hexdec( substr( $hex, 0, 2 ) );
        $g = hexdec( substr( $hex, 2, 2 ) );
        $b = hexdec( substr( $hex, 4, 2 ) );
    } else {
        return '';
    }

    $alpha = max( 0, min( 1, (float) $alpha ) );
    return 'rgba(' . $r . ', ' . $g . ', ' . $b . ', ' . $alpha . ')';
}

// Enqueue login styles
add_action( 'login_enqueue_scripts', 'lg_forge_enqueue_login_styles' );
function lg_forge_enqueue_login_styles() {
    $options = get_option( 'loginlook_options' );
    $logo_url = isset( $options['logo_url'] ) ? $options['logo_url'] : '';
    $logo_width = isset( $options['logo_width'] ) ? $options['logo_width'] : '84';
    $logo_height = isset( $options['logo_height'] ) ? $options['logo_height'] : '84';
    $active_theme = isset( $options['active_theme'] ) ? $options['active_theme'] : 'default';
    if ( 'preview-card' === $active_theme ) {
        $active_theme = 'skylogin-soft-ui';
    }
    $background_color = isset( $options['background_color'] ) ? sanitize_hex_color( $options['background_color'] ) : '';
    $background_image_url = isset( $options['background_image_url'] ) ? $options['background_image_url'] : '';
    $background_overlay_color = isset( $options['background_overlay_color'] ) ? sanitize_hex_color( $options['background_overlay_color'] ) : '';
    $background_overlay_opacity = isset( $options['background_overlay_opacity'] ) ? $options['background_overlay_opacity'] : '0.45';
    $text_color = isset( $options['text_color'] ) ? sanitize_hex_color( $options['text_color'] ) : '';

    // Enqueue theme CSS based on selected theme
    $theme_css_file = 'theme-' . sanitize_file_name( $active_theme ) . '.css';
    $theme_css_path = LG_FORGE_PLUGIN_DIR . 'assets/css/' . $theme_css_file;

    if ( file_exists( $theme_css_path ) ) {
        wp_enqueue_style( 'loginlook-theme-' . $active_theme, LG_FORGE_PLUGIN_URL . 'assets/css/' . $theme_css_file, array(), LG_FORGE_VERSION );
    }

    // Enqueue the base login style CSS
    wp_enqueue_style( 'loginlook-login-css', LG_FORGE_PLUGIN_URL . 'public/login-style.css', array(), LG_FORGE_VERSION );

    // Enqueue login placeholders script
    wp_enqueue_script( 'loginlook-login-js', LG_FORGE_PLUGIN_URL . 'public/login.js', array( 'jquery' ), LG_FORGE_VERSION, true );
    wp_localize_script( 'loginlook-login-js', 'loginlookPlaceholders', array(
        'username' => __( 'Username or Email Address', 'skyquake-loginforge' ),
        'password' => __( 'Password', 'skyquake-loginforge' ),
        'email'    => __( 'Email Address', 'skyquake-loginforge' ),
    ) );

    // Inline styles for dynamic values
    $inline_css = '';
    if ( $logo_url ) {
        $inline_css .= "
        .login h1 a {
            background-image: url('" . esc_url( $logo_url ) . "') !important;
            width: " . intval( $logo_width ) . "px !important;
            height: " . intval( $logo_height ) . "px !important;
            background-size: contain !important;
            background-repeat: no-repeat !important;
            background-position: center !important;
        }
        ";
    }

    if ( $background_color ) {
        $inline_css .= "
        body.login {
            background-color: " . esc_html( $background_color ) . " !important;
        }
        ";
    }

    if ( $background_image_url ) {
        $inline_css .= "
        body.login {
            background-image: url('" . esc_url( $background_image_url ) . "') !important;
            background-size: cover !important;
            background-position: center !important;
            background-repeat: no-repeat !important;
        }
        ";
    }

    if ( $text_color ) {
        $inline_css .= "
        body.login,
        body.login #nav,
        body.login #backtoblog,
        body.login #nav a,
        body.login #backtoblog a,
        body.login label,
        body.login .message,
        body.login .privacy-policy-page-link {
            color: " . esc_html( $text_color ) . " !important;
        }
        ";
    }

    if ( $background_overlay_color ) {
        $opacity = is_numeric( $background_overlay_opacity ) ? (float) $background_overlay_opacity : 0.45;
        $opacity = max( 0, min( 1, $opacity ) );
        $overlay_rgba = lg_forge_hex_to_rgba( $background_overlay_color, $opacity );
        if ( $overlay_rgba ) {
            $inline_css .= "
            .loginlook-overlay {
                position: fixed;
                inset: 0;
                background: " . esc_html( $overlay_rgba ) . " !important;
                pointer-events: none;
                z-index: 0;
            }
            body.login #login,
            body.login h1,
            body.login #nav,
            body.login #backtoblog,
            body.login .language-switcher {
                position: relative;
                z-index: 1;
            }
            ";
        }
    }

    if ( $inline_css ) {
        wp_add_inline_style( 'loginlook-login-css', $inline_css );
    }
}

// Render overlay layer on login page when enabled.
add_action( 'login_footer', 'lg_forge_render_overlay_layer' );
function lg_forge_render_overlay_layer() {
    $options = get_option( 'loginlook_options' );
    $background_overlay_color = isset( $options['background_overlay_color'] ) ? sanitize_hex_color( $options['background_overlay_color'] ) : '';
    $background_overlay_opacity = isset( $options['background_overlay_opacity'] ) ? $options['background_overlay_opacity'] : '0.45';

    if ( ! $background_overlay_color ) {
        return;
    }

    $opacity = is_numeric( $background_overlay_opacity ) ? (float) $background_overlay_opacity : 0.45;
    $opacity = max( 0, min( 1, $opacity ) );
    $overlay_rgba = lg_forge_hex_to_rgba( $background_overlay_color, $opacity );

    if ( ! $overlay_rgba ) {
        return;
    }
    ?>
    <div class="loginlook-overlay" aria-hidden="true"></div>
    <?php
}

// Add active theme class to login body
add_filter( 'login_body_class', 'lg_forge_login_body_class', 10, 2 );
function lg_forge_login_body_class( $classes, $action ) {
    $options = get_option( 'loginlook_options' );
    $active_theme = isset( $options['active_theme'] ) ? $options['active_theme'] : 'default';
    $classes[] = 'loginlook-theme-' . sanitize_html_class( $active_theme );
    return $classes;
}

// Set login logo URL
add_filter( 'login_headerurl', 'lg_forge_login_logo_url' );
function lg_forge_login_logo_url() {
    $options = get_option( 'loginlook_options' );
    $redirect_url = isset( $options['redirect_url'] ) ? $options['redirect_url'] : home_url();
    return esc_url( $redirect_url );
}

// Set login logo title
add_filter( 'login_headertitle', 'lg_forge_login_logo_title' );
function lg_forge_login_logo_title() {
    return get_bloginfo( 'name' );
}
?>
