<?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_Reset_Password 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-reset-password',
			'title'          => 'Sifency Reset Password',
			'icon'           => 'eicon-lock-user',
			'category'       => array( 'sifency-advance-widgets' ),
			'script_depends' => array( 'sifency-reset-password-script' ),
			'style_depends'  => array( 'sifency-login-style', 'sifency-typo-style' ),
			'keywords'       => array( 'reset password', 'password recovery', 'lost password', 'forgot password', 'user login', 'account recovery' ),
		);}
	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', 'sifency-addons' ),
					'floating' => esc_html__( '3. Floating Labels', 'sifency-addons' ),
					'glass'    => esc_html__( '4. Glassmorphism', 'sifency-addons' ),
					'split'    => esc_html__( '5. Split Screen', 'sifency-addons' ),
					'neon'     => esc_html__( '6. Dark / Neon', 'sifency-addons' ),
					'outline'  => esc_html__( '7. 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(
			'redirect_reset',
			array(
				'label'       => 'Redirect After Submit',
				'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/Email Label',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Username or Email',
			)
		);
		$this->add_control(
			'user_placeholder',
			array(
				'label'   => 'Placeholder',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Enter your email',
			)
		);
		$this->add_control(
			'btn_text',
			array(
				'label'   => 'Button Text',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Reset Password',
			)
		);
		$this->add_control(
			'login_text',
			array(
				'label'   => 'Back to Login Text',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Back to Login',
			)
		);
		$this->add_control(
			'login_url',
			array(
				'label' => 'Login Page URL',
				'type'  => Controls_Manager::URL,
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'style_fields',
			array(
				'label' => esc_html__( 'Input Fields', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			array(
				'name'     => 'field_typography',
				'label'    => esc_html__( 'Typography', 'sifency-addons' ),
				'selector' => '{{WRAPPER}} .sifency-reset-input',
			)
		);
		$this->start_controls_tabs( 'tabs_field_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-reset-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-reset-input' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			array(
				'name'     => 'field_border',
				'selector' => '{{WRAPPER}} .sifency-reset-input',
			)
		);
		$this->add_group_control(
			\Elementor\Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'field_box_shadow',
				'selector' => '{{WRAPPER}} .sifency-reset-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-reset-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-reset-input:focus' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'field_border_focus_color',
			array(
				'label'     => esc_html__( 'Border Color (Focus)', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-reset-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-reset-input:focus',
			)
		);

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

		// --- Dimensions ---
		$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-reset-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-reset-input' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);

		// --- Placeholder ---
		$this->add_control(
			'placeholder_color',
			array(
				'label'     => esc_html__( 'Placeholder Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'separator' => 'before',
				'selectors' => array(
					'{{WRAPPER}} .sifency-reset-input::-webkit-input-placeholder' => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-reset-input::-moz-placeholder'          => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-reset-input:-ms-input-placeholder'      => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-reset-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-reset-form-wrap, .sifency-reset-logged-in',
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'    => 'image_item_',
				'label'     => esc_html__( 'Image Wrapper', 'sifency-addons' ),
				'selector'  => '.sifency-reset-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-reset-label',
				'exclude'  => array( '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-reset-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() ) {
			echo '<div class="sifency-reset-logged-in">';
			echo '<p>' . esc_html__( 'You are already logged in.', 'sifency-addons' ) . '</p>';
			echo '<a href="' . esc_url( wp_logout_url( home_url() ) ) . '" class="sifency-reset-submit">' . esc_html__( 'Logout', 'sifency-addons' ) . '</a>';
			echo '</div>';
			return;
		}

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-reset-wrapper' );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-reset-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-reset-container">
				
				<?php if ( 'split' === $settings['layout_type'] ) : ?>
					<div class="sifency-reset-image" style="background-image: url(<?php echo esc_url( $settings['split_image']['url'] ); ?>);"></div>
				<?php endif; ?>

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

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

						<?php if ( ! empty( $settings['login_url']['url'] ) ) : ?>
							<div class="sifency-login-link">
								<a href="<?php echo esc_url( $settings['login_url']['url'] ); ?>"><?php echo esc_html( $settings['login_text'] ); ?></a>
							</div>
						<?php endif; ?>

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

		</div>
		<?php
	}
}
