<?php

class Logindes_login_style {

    public static function init() {
        
        // Custom full login screen
        add_action( 'login_header', array( __CLASS__, 'render_full_custom_login' ) );
    }
    
    

    public static function render_full_custom_login() {

        //Size Attributes 
        $login_bg_size_w_attr = sanitize_text_field(get_option('logindes_login_bg_size_w_attr', 'px'));
        $login_bg_size_h_attr = sanitize_text_field(get_option('logindes_login_bg_size_h_attr', 'px'));
        $login_padding_attr = sanitize_text_field(get_option('logindes_login_padding_attr', 'px'));

        $form_bg_w_attr = sanitize_text_field(get_option('logindes_form_bg_w_attr', 'px'));
        $form_bg_h_attr = sanitize_text_field(get_option('logindes_form_bg_h_attr', 'px'));
        $form_bg_size_width_attr = sanitize_text_field(get_option('logindes_form_bg_size_width_attr', 'px'));
        $form_bg_size_h_attr = sanitize_text_field(get_option('logindes_form_bg_size_h_attr', 'px'));
        $form_bg_padding_attr = sanitize_text_field(get_option('logindes_form_bg_padding_attr', 'px'));

        $input_font_size_attr = sanitize_text_field(get_option('logindes_input_font_size_attr', 'px'));
        $input_padding_attr = sanitize_text_field(get_option('logindes_input_padding_attr', 'px'));
        $input_border_radius_attr = sanitize_text_field(get_option('logindes_input_border_radius_attr', 'px'));
        $input_border_width_attr = sanitize_text_field(get_option('logindes_input_border_width_attr', 'px'));

        $button_font_size_attr = sanitize_text_field(get_option('logindes_button_font_size_attr', 'px'));
        $button_padding_attr = sanitize_text_field(get_option('logindes_button_padding_attr', 'px'));
        $button_border_radius_attr = sanitize_text_field(get_option('logindes_button_border_radius_attr', 'px'));
        $button_border_width_attr = sanitize_text_field(get_option('logindes_button_border_width_attr', 'px'));

        $label_font_size_attr = sanitize_text_field(get_option('logindes_label_font_size_attr', 'px'));
        $forgot_font_size_attr = sanitize_text_field(get_option('logindes_forgot_font_size_attr', 'px'));

        $check = true;

        if ( $check ) {
            // Get all custom options
            $bg_image        = get_option('logindes_login_bg_image', '' );
            $bg_color        = get_option('logindes_login_bg_color', '#f0f0f1' );
            $bg_opacity = get_option('logindes_login_bg_opacity', '100'); // default 100%
            $bg_repeat  = sanitize_text_field(get_option('logindes_login_bg_repeat', 'no-repeat'));
            $bg_op = intval($bg_opacity);
            $bg_op = max(0, min(100, $bg_op)) / 100; // convert 0–100 → 0–1
            $bg_w       = sanitize_text_field(get_option('logindes_login_bg_size_w', ''));
            $bg_h       = sanitize_text_field(get_option('logindes_login_bg_size_h', ''));
            $bg_size = '';
            if ($bg_w !== '' || $bg_h !== '') {
                $w       = $bg_w !== '' ? $bg_w : 'auto';
                $h       = $bg_h !== '' ? $bg_h : 'auto';
                $bg_size = $w . $login_bg_size_w_attr . ' ' . $h . $login_bg_size_h_attr;
            }
            $bg_top    = sanitize_text_field(get_option('logindes_login_bg_pos_top', ''));
            $bg_bottom     = sanitize_text_field(get_option('logindes_login_bg_pos_bottom', ''));
            $bg_left     = sanitize_text_field(get_option('logindes_login_bg_pos_left', ''));
            $bg_right     = sanitize_text_field(get_option('logindes_login_bg_pos_right', ''));
            $logo_url        = get_option('logindes_logo_url', '' );
            $logo_text       = sanitize_text_field( get_option('logindes_logo_text', '' ) );
            $form_bg_color   = sanitize_hex_color( get_option('logindes_form_bg_color', '#fff' ) );
            $form_bg_image   = esc_url( get_option('logindes_form_bg_image', '' ) );
            $form_bg_transparent = get_option('logindes_form_bg_transparent', 0 );
            $form_bg_opacity = get_option('logindes_form_bg_opacity', 100 );
            $form_bg_opacity = intval(value: $form_bg_opacity);
            $form_bg_width   = get_option('logindes_form_bg_width', '400' );
            $form_bg_height  = get_option('logindes_form_bg_height', '246' );
            $form_bg_repeat  = get_option('logindes_form_bg_repeat', 'no-repeat' );
            $bg_image_position = sanitize_text_field(get_option('logindes_backgroundImagePosition', 'center'));
            $form_bg_image_position = sanitize_text_field(get_option('logindes_formBackgroundImagePosition', 'center'));
            $form_bg_size_width = get_option('logindes_form_bg_size_width', '100');
            $form_bg_size_height = get_option('logindes_form_bg_size_height', 'auto');

            $form_bg_size = '';
            if ($form_bg_size_width !== '' || $form_bg_size_height !== '') {
                $w = $form_bg_size_width !== '' ? $form_bg_size_width : 'auto';
                $h = $form_bg_size_height !== '' ? $form_bg_size_height : 'auto';
                $form_bg_size = $w . $form_bg_size_width_attr . ' ' . $h . $form_bg_size_h_attr;
            }

            // Separate options for position
            $form_bg_position_top    = get_option('logindes_form_bg_position_top', '26');
            $form_bg_position_bottom = get_option('logindes_form_bg_position_bottom', '26');
            $form_bg_position_left   = get_option('logindes_form_bg_position_left', '24');
            $form_bg_position_right  = get_option('logindes_form_bg_position_right', '24');

            $input_bg_color       = get_option('logindes_input_bg_color', '#fff');
            $input_text_color     = get_option('logindes_input_text_color', '#2c3338');
            $input_font_size      = get_option('logindes_input_font_size', '14');
            $input_padding_top    = get_option('logindes_input_padding_top', '10');
            $input_padding_bottom = get_option('logindes_input_padding_bottom', '10');
            $input_padding_left   = get_option('logindes_input_padding_left', '10');
            $input_padding_right  = get_option('logindes_input_padding_right', '10');
            $input_border_radius  = get_option('logindes_input_border_radius', '4');
            $input_border_width   = get_option('logindes_input_border_width', '1');
            $input_border_style   = get_option('logindes_input_border_style', 'solid');
            $input_border_color   = get_option('logindes_input_border_color', '#8c8f94');

            $button_bg_color      = get_option('logindes_button_bg_color', '#135e96');
            $button_text_color    = get_option('logindes_button_text_color', '#fff');
            $button_font_size     = get_option('logindes_button_font_size', '13');
            $button_padding_top   = get_option('logindes_button_padding_top', '10');
            $button_padding_bottom= get_option('logindes_button_padding_bottom', '10');
            $button_padding_left  = get_option('logindes_button_padding_left', '12');
            $button_padding_right = get_option('logindes_button_padding_right', '12');
            $button_border_radius = get_option('logindes_button_border_radius', '3');
            $button_border_width  = get_option('logindes_button_border_width', '1');
            $button_border_style  = get_option('logindes_button_border_style', 'solid');
            $button_border_color  = get_option('logindes_button_border_color', '#135e96');


            $label_text_color    = get_option('logindes_label_text_color', '');
            $label_font_size      = get_option('logindes_label_font_size', '');

            $forgot_text_color    = get_option('logindes_forgot_text_color', '');
            $forgot_font_size      = get_option('logindes_forgot_font_size', '');

            $width           = intval( get_option('logindes_logo_width', 150) );
            $height          = intval( get_option('logindes_logo_height', 80) );
            $top             = intval( get_option('logindes_logo_top', 20) );
            $bottom          = intval( get_option('logindes_logo_bottom', 20) );
            $align           = esc_attr( get_option('logindes_logo_align', 'center') );
            $font            = intval( get_option('logindes_logo_font_size', 18) );
            $weight          = esc_attr( get_option('logindes_logo_font_weight', 'normal') );

            $logo_text_color = sanitize_text_field(get_option('logindes_logo_text_color', '#ffffff'));
            $logo_text_align = esc_attr(get_option('logindes_logo_text_align', 'center'));
        }

    ob_start(); // Stop default login HTML
    ?>
      <!DOCTYPE html>
        <html <?php language_attributes(); ?>>
        <head>
            <meta charset="<?php bloginfo( 'charset' ); ?>">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <?php wp_head(); ?>
        </head>
        <body class="login custom-login login-screen-designer">
            <div class="opacity-div"></div>
            <div class="custom-login-wrapper">
                <div class="logo">
                    <div class="form-opacity-div"></div>
                    <?php
					if ( ! empty( $logo_url ) ) {
                        $logo_id = attachment_url_to_postid( $logo_url );
                    
                        if ( $logo_id ) {
                            echo wp_get_attachment_image( 
                                $logo_id, 
                                'full', 
                                false, 
                                array(
                                    'class' => 'site-logo',
                                    'alt'   => get_post_meta( $logo_id, '_wp_attachment_image_alt', true ) ?: 'Logo',
                                ) 
                            );
                        } else {
                            // Agar URL galat ho ya ID na mile
                            echo '<img src="' . esc_url( plugin_dir_url( __FILE__ ) . '../assets/img/w-logo-blue.png' ) . '" class="site-logo" alt="Logo">';
                        }
                    } else {
                        // Agar $logo_url hi empty ho
                        echo '<img src="' . esc_url( plugin_dir_url( __FILE__ ) . '../assets/img/w-logo-blue.png' ) . '" class="site-logo" alt="Logo">';
                    }
					?>
                   
                </div>

                <?php if($logo_text): ?>

                <h2 class="logo-text login-title"><?php echo esc_html($logo_text); ?></h2>

                <?php endif; ?>

                <form name="loginform" id="loginform" action="<?php echo esc_url( site_url('wp-login.php', 'login_post') ); ?>" method="post">

                    <div class="single-field-input">
                        <label for="user_login">Your Email</label> 
                        <input  type="text"  name="log"  id="user_login"  class="input"  value=""  size="20" >
                    </div>
                    <div class="single-field-input">

                    <div class="forget-password">
                        <label for="user_pass"> Password </label>
						<a href="<?php echo esc_url( wp_lostpassword_url() ); ?>">Forgot?</a>
                    </div>
                        <input type="password"  name="pwd"  id="user_pass"  class="input password-input"  value="" size="20" >
                        <span id="togglePassword">
                            <svg class="eye-open" width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M12 5.25C4.5 5.25 1.5 12 1.5 12C1.5 12 4.5 18.75 12 18.75C19.5 18.75 22.5 12 22.5 12C22.5 12 19.5 5.25 12 5.25Z" stroke="#98A2B3" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
                                <path d="M12 15.75C14.0711 15.75 15.75 14.0711 15.75 12C15.75 9.92893 14.0711 8.25 12 8.25C9.92893 8.25 8.25 9.92893 8.25 12C8.25 14.0711 9.92893 15.75 12 15.75Z" stroke="#98A2B3" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
                            </svg>
                            <svg class="eye-closed" width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" >
                                <path d="M1.5 1.5L22.5 22.5" stroke="#98A2B3" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                                <path d="M12 5.25C4.5 5.25 1.5 12 1.5 12C1.5 12 4.5 18.75 12 18.75C19.5 18.75 22.5 12 22.5 12C22.5 12 19.5 5.25 12 5.25Z" stroke="#98A2B3" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                                <path d="M12 15.75C14.0711 15.75 15.75 14.0711 15.75 12C15.75 9.92893 14.0711 8.25 12 8.25C9.92893 8.25 8.25 9.92893 8.25 12C8.25 14.0711 9.92893 15.75 12 15.75Z" stroke="#98A2B3" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                            </svg>
                        </span>
                    </div>

                    <div class="keep-me">
                        <label><input name="rememberme" type="checkbox" id="rememberme" value="forever"> Keep me logged in</label>
                    </div>

                        <input type="hidden" name="redirect_to" value="<?php echo esc_url( admin_url() ); ?>">
                        <input type="hidden" name="testcookie" value="1">

                    <div class="submit-btn">
                        <input type="submit"  name="wp-submit"  id="wp-submit"  class="button button-primary button-large"  value="Login now">
                    </div>

                </form>
            </div>

            <?php wp_footer(); ?>

        </body>
        </html>
        <?php
        exit;
    }
}