<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_Widget_Login_Form extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-login-form',
			'title'          => 'Sifency Login Form',
			'icon'           => 'eicon-lock-user',
			'category'       => array( 'sifency-advance-widgets' ),
			'script_depends' => array( 'sifency-login-script' ),
			'style_depends'  => array( 'sifency-login-style', 'sifency-typo-style', 'sifency-icons' ),
			'keywords'       => array( 'login form', 'user login', 'login widget', 'login button', 'user authentication' ),
		);}
	protected function register_controls() {

		// --- CONFIG SECTION ---
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Form Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'card',
				'options' => array(
					'card'     => esc_html__( '1. Classic Card', 'sifency-addons' ),
					'minimal'  => esc_html__( '2. Minimal (No Border)', 'sifency-addons' ),
					'floating' => esc_html__( '3. Floating Labels', 'sifency-addons' ),
					'glass'    => esc_html__( '4. Glassmorphism', 'sifency-addons' ),
					'split'    => esc_html__( '5. Split Screen (Image)', 'sifency-addons' ),
					'outline'  => esc_html__( '6. Modern Outline', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'split_image',
			array(
				'label'     => esc_html__( 'Side Image', 'sifency-addons' ),
				'type'      => Controls_Manager::MEDIA,
				'default'   => array( 'url' => \Elementor\Utils::get_placeholder_image_src() ),
				'condition' => array( 'layout_type' => 'split' ),
			)
		);
		$this->add_control(
			'show_labels',
			array(
				'label'     => 'Show Labels',
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'condition' => array( 'layout_type!' => 'floating' ),
			)
		);
		$this->add_control(
			'show_remember',
			array(
				'label'   => 'Show Remember Me',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'redirect_url',
			array(
				'label'       => 'Redirect After Login',
				'type'        => Controls_Manager::TEXT,
				'placeholder' => home_url(),
			)
		);

		$this->end_controls_section();

		// --- TEXTS SECTION ---
		$this->start_controls_section( 'section_texts', array( 'label' => 'Labels & Text' ) );

		$this->add_control(
			'user_label',
			array(
				'label'   => 'Username Label',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Username or Email',
			)
		);
		$this->add_control(
			'user_placeholder',
			array(
				'label'   => 'Username Placeholder',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Enter your username',
			)
		);

		$this->add_control(
			'pass_label',
			array(
				'label'   => 'Password Label',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Password',
			)
		);
		$this->add_control(
			'pass_placeholder',
			array(
				'label'   => 'Password Placeholder',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Enter your password',
			)
		);

		$this->add_control(
			'btn_text',
			array(
				'label'   => 'Button Text',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Log In',
			)
		);

		$this->add_control(
			'show_links',
			array(
				'label'   => 'Show Lost/Register Links',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_lost_password',
			array(
				'label'     => 'Lost Password?',
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'condition' => array( 'show_links' => 'yes' ),
			)
		);
		$this->add_control(
			'show_register',
			array(
				'label'     => 'Register?',
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'condition' => array( 'show_links' => 'yes' ),
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// --- STYLES: FIELDS ---
		$this->start_controls_section(
			'style_fields',
			array(
				'label' => esc_html__( 'Input Fields', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		// Input Typography
		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			array(
				'name'     => 'field_typography',
				'label'    => esc_html__( 'Typography', 'sifency-addons' ),
				'selector' => '{{WRAPPER}} .sifency-login-input',
			)
		);

		// Input Tabs (Normal vs Focus)
		$this->start_controls_tabs( 'tabs_field_state' );

		// --- Normal State ---
		$this->start_controls_tab(
			'tab_field_normal',
			array( 'label' => esc_html__( 'Normal', 'sifency-addons' ) )
		);
		$this->add_control(
			'field_bg_color',
			array(
				'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-login-input' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'field_text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-login-input' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			array(
				'name'     => 'field_border',
				'selector' => '{{WRAPPER}} .sifency-login-input',
			)
		);
		$this->add_group_control(
			\Elementor\Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'field_box_shadow',
				'selector' => '{{WRAPPER}} .sifency-login-input',
			)
		);

		$this->end_controls_tab();

		// --- Focus State ---
		$this->start_controls_tab(
			'tab_field_focus',
			array( 'label' => esc_html__( 'Focus', 'sifency-addons' ) )
		);
		$this->add_control(
			'field_bg_focus',
			array(
				'label'     => esc_html__( 'Background (Focus)', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-login-input:focus' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'field_text_focus',
			array(
				'label'     => esc_html__( 'Text Color (Focus)', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-login-input:focus' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'field_border_focus',
			array(
				'label'     => esc_html__( 'Border Color (Focus)', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-login-input:focus' => 'border-color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			\Elementor\Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'field_box_shadow_focus',
				'selector' => '{{WRAPPER}} .sifency-login-input:focus',
			)
		);

		$this->end_controls_tab();
		$this->end_controls_tabs();

		// --- Spacing & Radius ---
		$this->add_responsive_control(
			'field_padding',
			array(
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'separator'  => 'before',
				'selectors'  => array( '{{WRAPPER}} .sifency-login-input' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			'field_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-login-input' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);

		// --- Placeholder Color ---
		$this->add_control(
			'placeholder_color',
			array(
				'label'     => esc_html__( 'Placeholder Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'separator' => 'before',
				'selectors' => array(
					'{{WRAPPER}} .sifency-login-input::-webkit-input-placeholder' => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-login-input::-moz-placeholder'          => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-login-input:-ms-input-placeholder'      => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-login-input::placeholder'               => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();

		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'form_item_',
				'label'    => esc_html__( 'Login Form Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-login-form-wrap, .sifency-login-logged-in',
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'    => 'image_item_',
				'label'     => esc_html__( 'Image Wrapper', 'sifency-addons' ),
				'selector'  => '.sifency-login-image',
				'condition' => array( 'layout_type' => 'split' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'form_link_',
				'label'    => esc_html__( 'Link Text', 'sifency-addons' ),
				'selector' => '.sifency-login-form-wrap a',
				'exclude'  => array( 'alignment', 'transition', 'shadow' ),
				'exclude'  => array( 'gradient' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'form_label_',
				'label'    => esc_html__( 'Label', 'sifency-addons' ),
				'selector' => '.sifency-login-form-wrap label',
				'exclude'  => array( 'alignment', 'transition', 'shadow' ),
				'exclude'  => array( 'gradient' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'text_label_',
				'label'    => esc_html__( 'Additional Text', 'sifency-addons' ),
				'selector' => '.sifency-remember-me, .sifency-register-link',
				'exclude'  => array( 'box_model', 'alignment', 'transition', 'shadow' ),
				'exclude'  => array( 'gradient' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'          => 'button_',
				'label'           => esc_html__( 'Button Style', 'sifency-addons' ),
				'selector'        => '.sifency-login-submit',
				'show_typography' => true,
				'show_dimensions' => true,
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id        = esc_attr( $context['dom_id'] );
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		// Logged In State
		if ( is_user_logged_in() && ! \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$current_user = wp_get_current_user();
			echo '<div class="sifency-login-logged-in">';
			echo '<div class="sifency-user-avatar">' . get_avatar( $current_user->ID, 64 ) . '</div>';
			echo '<p>' . sprintf( /* translators: Hello %s */ esc_html__( 'Hello, %s', 'sifency-addons' ), '<strong>' . esc_html( $current_user->display_name ) . '</strong>' ) . '</p>';
			echo '<a href="' . esc_url( wp_logout_url( home_url() ) ) . '" class="sifency-login-submit">' . esc_html__( 'Logout', 'sifency-addons' ) . '</a>';
			echo '</div>';
			return;
		}

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-login-wrapper' );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-login-layout-' . $settings['layout_type'] );

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?>>
			
			<div class="sifency-login-container">
				
				<?php if ( 'split' === $settings['layout_type'] ) : ?>
					<div class="sifency-login-image" style="background-image: url(<?php echo esc_url( $settings['split_image']['url'] ); ?>);"></div>
				<?php endif; ?>

				<div class="sifency-login-form-wrap">
					<form class="sifency-login-form" action="<?php echo esc_url( site_url( 'wp-login.php', 'login_post' ) ); ?>" method="post">
						
						<!-- Username -->
						<div class="sifency-field-group">
							<?php if ( 'yes' === $settings['show_labels'] && 'floating' !== $settings['layout_type'] ) : ?>
								<label class="sifency-login-label" for="user_login"><?php echo esc_html( $settings['user_label'] ); ?></label>
							<?php endif; ?>
							
							<input type="text" name="log" id="user_login" class="sifency-login-input" placeholder="<?php echo 'floating' === $settings['layout_type'] ? ' ' : esc_attr( $settings['user_placeholder'] ); ?>">
							
							<?php if ( 'floating' === $settings['layout_type'] ) : ?>
								<label class="sifency-login-label floating" for="user_login"><?php echo esc_html( $settings['user_label'] ); ?></label>
							<?php endif; ?>
						</div>

						<!-- Password -->
						<div class="sifency-field-group">
							<?php if ( 'yes' === $settings['show_labels'] && 'floating' !== $settings['layout_type'] ) : ?>
								<label class="sifency-login-label" for="user_pass"><?php echo esc_html( $settings['pass_label'] ); ?></label>
							<?php endif; ?>
							
							<div class="sifency-password-wrap">
								<input type="password" name="pwd" id="user_pass" class="sifency-login-input" placeholder="<?php echo 'floating' === $settings['layout_type'] ? ' ' : esc_attr( $settings['pass_placeholder'] ); ?>">
								<span class="sifency-pass-toggle"><i class="eicon-preview-medium"></i></span>
							</div>

							<?php if ( 'floating' === $settings['layout_type'] ) : ?>
								<label class="sifency-login-label floating" for="user_pass"><?php echo esc_html( $settings['pass_label'] ); ?></label>
							<?php endif; ?>
						</div>

						<!-- Extras -->
						<div class="sifency-login-meta">
							<?php if ( 'yes' === $settings['show_remember'] ) : ?>
								<label class="sifency-remember-me">
									<input name="rememberme" type="checkbox" value="forever"> <?php esc_html_e( 'Remember Me', 'sifency-addons' ); ?>
								</label>
							<?php endif; ?>
							
							<?php if ( 'yes' === $settings['show_links'] && 'yes' === $settings['show_lost_password'] ) : ?>
								<a class="sifency-lost-pass" href="<?php echo esc_url( wp_lostpassword_url() ); ?>"><?php esc_html_e( 'Lost Password?', 'sifency-addons' ); ?></a>
							<?php endif; ?>
						</div>

						<!-- Actions -->
						<div class="sifency-login-actions">
							<button type="submit" class="sifency-login-submit"><?php echo esc_html( $settings['btn_text'] ); ?></button>
							<?php if ( ! empty( $settings['redirect_url'] ) ) : ?>
								<input type="hidden" name="redirect_to" value="<?php echo esc_url( $settings['redirect_url'] ); ?>">
							<?php else : ?>
								<input type="hidden" name="redirect_to" value="<?php echo esc_url( home_url() ); ?>">
							<?php endif; ?>
						</div>

						<?php if ( 'yes' === $settings['show_links'] && 'yes' === $settings['show_register'] ) : ?>
							<div class="sifency-register-link">
								<?php esc_html_e( 'Don\'t have an account?', 'sifency-addons' ); ?> <a href="<?php echo esc_url( wp_registration_url() ); ?>"><?php esc_html_e( 'Register', 'sifency-addons' ); ?></a>
							</div>
						<?php endif; ?>

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

		</div>
		<?php
	}
}
