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

use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Plugin;
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 Elementor\Group_Control_Image_Size;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_Widget_Restricted_Gate extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-content-restriction',
			'title'          => 'Sifency Content Restriction',
			'icon'           => 'eicon-lock-user',
			'category'       => array( 'sifency-business-widgets' ),
			'script_depends' => array( 'sifency-age-gate-script' ),
			'style_depends'  => array( 'sifency-age-gate-style' ),
		);
	}

	protected function register_controls() {
		// --- CONFIG SECTION ---
		$this->start_controls_section(
			'section_config',
			array( 'label' => esc_html__( 'Verification Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'editor_preview',
			array(
				'label'       => esc_html__( 'Show in Editor', 'sifency-addons' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => 'yes',
				'description' => esc_html__( 'Disable this to hide the popup while editing other elements on the page.', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'standard',
				'options' => array(
					'standard' => esc_html__( '1. Standard Modal (Yes/No)', 'sifency-addons' ),
					'birthday' => esc_html__( '2. Birthday Input', 'sifency-addons' ),
					'blur'     => esc_html__( '3. Blur / Upgrade to View', 'sifency-addons' ),
					'split'    => esc_html__( '4. Split Screen', 'sifency-addons' ),
					'neon'     => esc_html__( '5. Neon / Dark Mode', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'method',
			array(
				'label'     => esc_html__( 'Verification Method', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'buttons',
				'options'   => array(
					'buttons' => esc_html__( 'Yes / No Buttons', 'sifency-addons' ),
					'input'   => esc_html__( 'Date of Birth Input', 'sifency-addons' ),
				),
				'condition' => array( 'layout_type!' => 'birthday' ),
			)
		);
		$this->add_control(
			'min_age',
			array(
				'label'   => 'Minimum Age',
				'type'    => Controls_Manager::NUMBER,
				'default' => 18,
			)
		);
		$this->add_control(
			'cookie_life',
			array(
				'label'   => 'Remember for (Days)',
				'type'    => Controls_Manager::NUMBER,
				'default' => 30,
			)
		);

		$this->add_control(
			'redirect_on_fail',
			array(
				'label'   => 'Redirect on Fail?',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);
		$this->add_control(
			'redirect_url',
			array(
				'label'       => 'Redirect URL',
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'https://google.com',
				'condition'   => array( 'redirect_on_fail' => 'yes' ),
			)
		);
		$this->add_control(
			'fail_message',
			array(
				'label'     => 'Fail Message',
				'type'      => Controls_Manager::TEXTAREA,
				'default'   => 'Access Denied. You do not meet the age requirement.',
				'condition' => array( 'redirect_on_fail!' => 'yes' ),
			)
		);

		$this->end_controls_section();

		// --- CONTENT SECTION ---
		$this->start_controls_section( 'section_content', array( 'label' => 'Content' ) );

		$this->add_control(
			'logo',
			array(
				'label' => 'Logo/Image',
				'type'  => Controls_Manager::MEDIA,
			)
		);
		$this->add_control(
			'title',
			array(
				'label'   => 'Title',
				'type'    => Controls_Manager::TEXTAREA,
				'default' => 'Age Verification Required',
			)
		);
		$this->add_control(
			'desc',
			array(
				'label'   => 'Description',
				'type'    => Controls_Manager::TEXTAREA,
				'default' => 'Please confirm you are of legal age to view this content.',
			)
		);

		$this->add_control(
			'btn_yes',
			array(
				'label'   => 'Confirm Button Text',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Enter',
			)
		);
		$this->add_control(
			'btn_no',
			array(
				'label'     => 'Reject Button Text',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Exit',
				'condition' => array( 'method' => 'buttons' ),
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();

		// --- STYLE: MODAL ---
		$this->start_controls_section(
			'style_modal',
			array(
				'label' => 'Modal Box',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'backdrop_color',
			array(
				'label'     => 'Backdrop Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-age-gate-overlay' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->end_controls_section();

		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'wrapper_style',
				'label'    => 'Content Wrapper',
				'selector' => '{{WRAPPER}} .sifency-ag-box',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'heading_style_',
				'label'     => 'Title Style',
				'selector'  => '{{WRAPPER}} .sifency-heading-style',
				'condition' => array( 'title!' => '' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'content_style_',
				'label'          => 'Content Style',
				'selector'       => '{{WRAPPER}} .sifency-content-style',
				'condition'      => array( 'desc!' => '' ),
				'show_max_width' => true,
			)
		);
		$this->add_sifency_addons_button_style_controls(
			array(
				'prefix'    => 'primary_btn',
				'label'     => esc_html__( 'Button Primary', 'sifency-addons' ),
				'selector'  => '{{WRAPPER}} .sifency-ag-btn.sifency-ag-yes, {{WRAPPER}} .sifency-ag-btn.sifency-ag-submit',
				'condition' => array( 'btn_yes!' => '' ),
			)
		);
		$this->add_sifency_addons_button_style_controls(
			array(
				'prefix'    => 'secondary_btn',
				'label'     => esc_html__( 'Button Secondary', 'sifency-addons' ),
				'selector'  => '{{WRAPPER}} .sifency-ag-btn.sifency-ag-no',
				'condition' => array( 'btn_no!' => '' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'label_style_',
				'label'          => 'Message Style',
				'selector'       => '{{WRAPPER}} .sifency-label-style',
				'condition'      => array( 'fail_message!' => '' ),
				'show_max_width' => true,
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'selector'  => '{{WRAPPER}} .sifency-ag-logo img',
				'selectors' => array(
					'image'      => '{{WRAPPER}} .sifency-ag-logo img',
					'container'  => '{{WRAPPER}} .sifency-ag-logo',
					'dimensions' => '{{WRAPPER}} .sifency-ag-logo img',
				),
				'condition' => array( 'layout_type' => 'split' ),
			)
		);

		$this->add_sifency_addons_animation_controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();

		// Handle Elementor Edit Mode Overlay toggle
		if ( Plugin::$instance->editor->is_edit_mode() && 'yes' !== $settings['editor_preview'] ) {
			echo '<div style="padding: 20px; background: #fff; border: 2px dashed #ccc; text-align: center; border-radius: 8px;">';
			echo esc_html__( 'Sifency Age Gate hidden in Editor. Enable "Show in Editor" to style it.', 'sifency-addons' );
			echo '</div>';
			return;
		}

		$animation_manager       = new Sifency_Addons_Animation_Manager( $settings );
		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes();

		$this->add_render_attribute(
			'wrapper',
			'class',
			array(
				'sifency-age-gate-overlay',
				'sifency-ag-layout-' . esc_attr( $settings['layout_type'] ),
			)
		);

		$js_config = array(
			'method'       => ( $settings['layout_type'] === 'birthday' ) ? 'input' : $settings['method'],
			'age'          => absint( $settings['min_age'] ),
			'days'         => absint( $settings['cookie_life'] ),
			'redirect'     => $settings['redirect_on_fail'],
			'redirect_url' => $settings['redirect_url'],
		);

		// Encode securely without passing through KSES which strips quotes in data attributes
		$this->add_render_attribute( 'wrapper', 'data-ag-settings', wp_json_encode( $js_config ) );

		$is_input_mode = ( $settings['method'] === 'input' || $settings['layout_type'] === 'birthday' );
		?>

		<div id="<?php echo esc_attr( $wrapper_id ); ?>" 
		<?php
        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor widget rendering attributes
		echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) );
		?>
		<?php echo wp_kses_post( $animation_wrapper_attrs ); ?>>
			
			<div class="sifency-ag-box">

				<?php if ( 'split' === $settings['layout_type'] && ! empty( $settings['logo']['url'] ) ) : ?>
					<div class="sifency-ag-split-img" style="background-image: url(<?php echo esc_url( $settings['logo']['url'] ); ?>);"></div>
				<?php endif; ?>

				<div class="sifency-ag-content">
					
					<?php if ( 'split' !== $settings['layout_type'] && ! empty( $settings['logo']['url'] ) ) : ?>
						<div class="sifency-ag-logo">
							<img src="<?php echo esc_url( $settings['logo']['url'] ); ?>" alt="<?php echo esc_attr__( 'Age Verification Logo', 'sifency-addons' ); ?>">
						</div>
					<?php endif; ?>

					<h2 class="sifency-ag-title sifency-heading-style"><?php echo esc_html( $settings['title'] ); ?></h2>
					<div class="sifency-ag-desc sifency-content-style"><?php echo wp_kses_post( $settings['desc'] ); ?></div>

					<div class="sifency-ag-actions">
						
						<?php if ( $is_input_mode ) : ?>
							<div class="sifency-ag-inputs-wrap">
								<input type="number" placeholder="DD" class="sifency-ag-input ag-day" min="1" max="31">
								<input type="number" placeholder="MM" class="sifency-ag-input ag-month" min="1" max="12">
								<input type="number" placeholder="YYYY" class="sifency-ag-input ag-year" min="1900" max="<?php echo esc_attr( wp_date( 'Y' ) ); ?>">
							</div>
							<button class="sifency-ag-btn sifency-ag-submit"><?php echo esc_html( $settings['btn_yes'] ); ?></button>
						
						<?php else : ?>
							<button class="sifency-ag-btn sifency-ag-yes"><?php echo esc_html( $settings['btn_yes'] ); ?></button>
							<button class="sifency-ag-btn sifency-ag-no"><?php echo esc_html( $settings['btn_no'] ); ?></button>
						<?php endif; ?>

					</div>

					<div class="sifency-ag-error-msg sifency-label-style" style="display: none; margin-top: 15px;">
						<?php echo esc_html( $settings['fail_message'] ); ?>
					</div>

				</div>
			</div>

		</div>
		<?php
	}
}
