<?php
/**
 * Widget Name: Age Gate
 * Description: An age-verification gate shown over the page until the visitor
 *              confirms they meet a minimum age. Three verification modes
 *              ( confirm buttons, date of birth, or a checkbox ), a remembered
 *              choice via localStorage, a configurable decline action ( show a
 *              message or redirect ) and five distinctive overlay styles. The gate
 *              is editor-safe: inside Elementor it renders inline for styling and
 *              never locks the canvas. Only the selected style CSS loads on the
 *              frontend.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.6.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Utils;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Background;

/**
 * Class Captwiki_Age_Gate
 *
 * @since 1.6.0
 */
class Captwiki_Age_Gate extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @since 1.6.0
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-age-gate';
	}

	/**
	 * Get widget title.
	 *
	 * @since 1.6.0
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Age Gate', 'captain-widgets-kit' );
	}

	/**
	 * Get widget icon.
	 *
	 * @since 1.6.0
	 * @return string
	 */
	public function get_icon(): string {
		return 'eicon-lock-user captwiki-el-logo';
	}

	/**
	 * Get widget categories.
	 *
	 * @since 1.6.0
	 * @return array
	 */
	public function get_categories(): array {
		return array( 'captwiki-essential' );
	}

	/**
	 * Get widget keywords.
	 *
	 * @since 1.6.0
	 * @return array
	 */
	public function get_keywords(): array {
		return array( 'age', 'gate', 'verification', 'restrict', 'adult', 'popup', 'modal', 'captain-widgets-kit' );
	}

	/**
	 * Common stylesheet dependency.
	 *
	 * @since 1.6.0
	 * @return array
	 */
	public function get_style_depends(): array {
		return array( 'captwiki_age_gate' );
	}

	/**
	 * Frontend script dependency.
	 *
	 * @since 1.6.0
	 * @return array
	 */
	public function get_script_depends(): array {
		return array( 'captwiki-age-gate' );
	}

	/**
	 * Disable Elementor's default inner wrapper.
	 *
	 * @since 1.6.0
	 * @return bool
	 */
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/**
	 * Selectable visual styles ( gate-specific layouts ).
	 *
	 * @since 1.6.0
	 * @return array
	 */
	private function captwiki_style_options(): array {
		return array(
			'style-1' => esc_html__( 'Style 1 – Centered Card', 'captain-widgets-kit' ),
			'style-2' => esc_html__( 'Style 2 – Full-Screen Cover', 'captain-widgets-kit' ),
			'style-3' => esc_html__( 'Style 3 – Frosted Glass', 'captain-widgets-kit' ),
			'style-4' => esc_html__( 'Style 4 – Split Image', 'captain-widgets-kit' ),
			'style-5' => esc_html__( 'Style 5 – Top Accent', 'captain-widgets-kit' ),
		);
	}

	/**
	 * Register widget controls.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	protected function register_controls(): void {
		$this->register_content_controls();
		$this->register_verify_text_controls();
		$this->register_verification_controls();
		$this->register_fail_controls();

		$this->register_overlay_style_controls();
		$this->register_panel_style_controls();
		$this->register_text_style_controls();
		$this->register_button_style_controls();
		$this->register_field_style_controls();
	}

	/**
	 * Content tab — primary content.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_content_controls(): void {

		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Content', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'widget_style',
			array(
				'label'   => esc_html__( 'Style', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'style-1',
				'options' => $this->captwiki_style_options(),
			)
		);

		$this->add_control(
			'show_logo',
			array(
				'label'        => esc_html__( 'Show Logo / Image', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'logo',
			array(
				'label'     => esc_html__( 'Logo / Image', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::MEDIA,
				'default'   => array( 'url' => Utils::get_placeholder_image_src() ),
				'condition' => array( 'show_logo' => 'yes' ),
				'dynamic'   => array( 'active' => true ),
			)
		);

		$this->add_control(
			'title',
			array(
				'label'       => esc_html__( 'Title', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Are you of legal age?', 'captain-widgets-kit' ),
				'label_block' => true,
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->add_control(
			'description',
			array(
				'label'   => esc_html__( 'Description', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::TEXTAREA,
				'rows'    => 3,
				'default' => esc_html__( 'You must confirm you are of legal age to enter this website.', 'captain-widgets-kit' ),
				'dynamic' => array( 'active' => true ),
			)
		);

		$this->add_control(
			'verify_method',
			array(
				'label'   => esc_html__( 'Verification Method', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'buttons',
				'options' => array(
					'buttons'  => esc_html__( 'Confirm Buttons ( Yes / No )', 'captain-widgets-kit' ),
					'dob'      => esc_html__( 'Date of Birth', 'captain-widgets-kit' ),
					'checkbox' => esc_html__( 'Checkbox Confirm', 'captain-widgets-kit' ),
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Content tab — labels for each verification method.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_verify_text_controls(): void {

		$this->start_controls_section(
			'section_verify_text',
			array(
				'label' => esc_html__( 'Verification Labels', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		// Buttons.
		$this->add_control(
			'confirm_text',
			array(
				'label'     => esc_html__( 'Confirm Button', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Yes, I am', 'captain-widgets-kit' ),
				'condition' => array( 'verify_method' => 'buttons' ),
			)
		);

		$this->add_control(
			'decline_text',
			array(
				'label'     => esc_html__( 'Decline Button', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'No, I am not', 'captain-widgets-kit' ),
				'condition' => array( 'verify_method' => 'buttons' ),
			)
		);

		// Date of birth.
		$this->add_control(
			'dob_button_text',
			array(
				'label'     => esc_html__( 'Submit Button', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Enter', 'captain-widgets-kit' ),
				'condition' => array( 'verify_method' => 'dob' ),
			)
		);

		// Checkbox.
		$this->add_control(
			'checkbox_label',
			array(
				'label'     => esc_html__( 'Checkbox Label', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'I confirm I am of legal age.', 'captain-widgets-kit' ),
				'condition' => array( 'verify_method' => 'checkbox' ),
			)
		);

		$this->add_control(
			'checkbox_button_text',
			array(
				'label'     => esc_html__( 'Enter Button', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Enter Site', 'captain-widgets-kit' ),
				'condition' => array( 'verify_method' => 'checkbox' ),
			)
		);

		$this->add_control(
			'error_text',
			array(
				'label'       => esc_html__( 'Error Message', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Please enter a valid date.', 'captain-widgets-kit' ),
				'label_block' => true,
				'description' => esc_html__( 'Shown for an invalid date or an unchecked box.', 'captain-widgets-kit' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Content tab — verification behaviour.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_verification_controls(): void {

		$this->start_controls_section(
			'section_verification',
			array(
				'label' => esc_html__( 'Settings', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'min_age',
			array(
				'label'   => esc_html__( 'Minimum Age', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 1,
				'max'     => 120,
				'step'    => 1,
				'default' => 18,
			)
		);

		$this->add_control(
			'remember_days',
			array(
				'label'       => esc_html__( 'Remember For ( Days )', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 0,
				'max'         => 365,
				'step'        => 1,
				'default'     => 30,
				'description' => esc_html__( 'How long to remember a passed check. 0 = ask every visit ( session only ).', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'cookie_key',
			array(
				'label'       => esc_html__( 'Storage Key', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => 'captwiki_age_gate',
				'description' => esc_html__( 'Use the same key across pages to remember the visitor site-wide.', 'captain-widgets-kit' ),
				'ai'          => array( 'active' => false ),
			)
		);

		$this->add_control(
			'decline_action',
			array(
				'label'   => esc_html__( 'On Decline', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'message',
				'options' => array(
					'message'  => esc_html__( 'Show Message', 'captain-widgets-kit' ),
					'redirect' => esc_html__( 'Redirect', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_control(
			'redirect_url',
			array(
				'label'       => esc_html__( 'Redirect URL', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::URL,
				'placeholder' => esc_html__( 'https://www.google.com', 'captain-widgets-kit' ),
				'default'     => array( 'url' => 'https://www.google.com' ),
				'condition'   => array( 'decline_action' => 'redirect' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Content tab — decline message screen.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_fail_controls(): void {

		$this->start_controls_section(
			'section_fail',
			array(
				'label'     => esc_html__( 'Decline Message', 'captain-widgets-kit' ),
				'tab'       => Controls_Manager::TAB_CONTENT,
				'condition' => array( 'decline_action' => 'message' ),
			)
		);

		$this->add_control(
			'fail_title',
			array(
				'label'       => esc_html__( 'Title', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Access Denied', 'captain-widgets-kit' ),
				'label_block' => true,
			)
		);

		$this->add_control(
			'fail_message',
			array(
				'label'   => esc_html__( 'Message', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::TEXTAREA,
				'rows'    => 3,
				'default' => esc_html__( 'Sorry, you are not old enough to view this content.', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'retry_text',
			array(
				'label'   => esc_html__( 'Try Again Button', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'Go Back', 'captain-widgets-kit' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Style tab — overlay backdrop.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_overlay_style_controls(): void {

		$this->start_controls_section(
			'section_overlay_style',
			array(
				'label' => esc_html__( 'Overlay', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'accent_color',
			array(
				'label'       => esc_html__( 'Accent Color', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::COLOR,
				'default'     => '',
				'selectors'   => array(
					'{{WRAPPER}} .captwiki-age-gate' => '--captwiki-age-accent: {{VALUE}};',
				),
				'description' => esc_html__( 'Drives the confirm button and accents.', 'captain-widgets-kit' ),
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'overlay_background',
				'label'    => esc_html__( 'Background', 'captain-widgets-kit' ),
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .captwiki-age-gate__overlay',
			)
		);

		$this->add_control(
			'overlay_blur',
			array(
				'label'      => esc_html__( 'Backdrop Blur', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 30 ) ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-age-gate__overlay' => 'backdrop-filter: blur( {{SIZE}}{{UNIT}} ); -webkit-backdrop-filter: blur( {{SIZE}}{{UNIT}} );',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Style tab — panel container.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_panel_style_controls(): void {

		$this->start_controls_section(
			'section_panel_style',
			array(
				'label' => esc_html__( 'Panel', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'panel_background',
				'label'    => esc_html__( 'Background', 'captain-widgets-kit' ),
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .captwiki-age-gate__panel',
			)
		);

		$this->add_responsive_control(
			'panel_width',
			array(
				'label'      => esc_html__( 'Max Width', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array( 'min' => 280, 'max' => 1000 ),
					'%'  => array( 'min' => 30, 'max' => 100 ),
				),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-age-gate__panel' => 'max-width: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'panel_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', 'rem' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-age-gate__body' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'panel_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-age-gate__panel' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'panel_border',
				'label'    => esc_html__( 'Border', 'captain-widgets-kit' ),
				'selector' => '{{WRAPPER}} .captwiki-age-gate__panel',
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'panel_shadow',
				'label'    => esc_html__( 'Box Shadow', 'captain-widgets-kit' ),
				'selector' => '{{WRAPPER}} .captwiki-age-gate__panel',
			)
		);

		$this->add_responsive_control(
			'logo_width',
			array(
				'label'      => esc_html__( 'Logo Width', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array( 'px' => array( 'min' => 30, 'max' => 400 ) ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-age-gate__logo' => 'width: {{SIZE}}{{UNIT}};',
				),
				'condition'  => array( 'show_logo' => 'yes' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Style tab — title & description text.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_text_style_controls(): void {

		$this->start_controls_section(
			'section_text_style',
			array(
				'label' => esc_html__( 'Title & Text', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'text_align',
			array(
				'label'     => esc_html__( 'Alignment', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::CHOOSE,
				'default'   => 'center',
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .captwiki-age-gate__body' => 'text-align: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'title_color',
			array(
				'label'     => esc_html__( 'Title Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-age-gate__title' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'title_typography',
				'selector' => '{{WRAPPER}} .captwiki-age-gate__title',
			)
		);

		$this->add_control(
			'desc_color',
			array(
				'label'     => esc_html__( 'Description Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-age-gate__desc' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'desc_typography',
				'selector' => '{{WRAPPER}} .captwiki-age-gate__desc',
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Style tab — buttons.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_button_style_controls(): void {

		$this->start_controls_section(
			'section_button_style',
			array(
				'label' => esc_html__( 'Buttons', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'button_typography',
				'selector' => '{{WRAPPER}} .captwiki-age-gate__btn',
			)
		);

		$this->add_responsive_control(
			'button_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-age-gate__btn' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'button_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-age-gate__btn' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'button_gap',
			array(
				'label'      => esc_html__( 'Spacing', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 40 ) ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-age-gate__buttons' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		// Confirm button states.
		$this->add_control(
			'heading_confirm',
			array(
				'label' => esc_html__( 'Confirm / Enter Button', 'captain-widgets-kit' ),
				'type'  => Controls_Manager::HEADING,
			)
		);

		$this->start_controls_tabs( 'confirm_state_tabs' );

		$this->start_controls_tab(
			'confirm_normal',
			array( 'label' => esc_html__( 'Normal', 'captain-widgets-kit' ) )
		);

		$this->add_control(
			'confirm_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-age-gate__btn--confirm' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'confirm_bg',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-age-gate__btn--confirm' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'confirm_hover',
			array( 'label' => esc_html__( 'Hover', 'captain-widgets-kit' ) )
		);

		$this->add_control(
			'confirm_color_hover',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-age-gate__btn--confirm:hover' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'confirm_bg_hover',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-age-gate__btn--confirm:hover' => 'background-color: {{VALUE}};',
				),
			)
		);

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

		// Decline button states.
		$this->add_control(
			'heading_decline',
			array(
				'label'     => esc_html__( 'Decline Button', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
				'condition' => array( 'verify_method' => 'buttons' ),
			)
		);

		$this->start_controls_tabs(
			'decline_state_tabs',
			array( 'condition' => array( 'verify_method' => 'buttons' ) )
		);

		$this->start_controls_tab(
			'decline_normal',
			array(
				'label'     => esc_html__( 'Normal', 'captain-widgets-kit' ),
				'condition' => array( 'verify_method' => 'buttons' ),
			)
		);

		$this->add_control(
			'decline_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-age-gate__btn--decline' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'decline_bg',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-age-gate__btn--decline' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'decline_hover',
			array(
				'label'     => esc_html__( 'Hover', 'captain-widgets-kit' ),
				'condition' => array( 'verify_method' => 'buttons' ),
			)
		);

		$this->add_control(
			'decline_color_hover',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-age-gate__btn--decline:hover' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'decline_bg_hover',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-age-gate__btn--decline:hover' => 'background-color: {{VALUE}};',
				),
			)
		);

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

		$this->end_controls_section();
	}

	/**
	 * Style tab — date / checkbox fields.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_field_style_controls(): void {

		$this->start_controls_section(
			'section_field_style',
			array(
				'label'      => esc_html__( 'Input Fields', 'captain-widgets-kit' ),
				'tab'        => Controls_Manager::TAB_STYLE,
				'conditions' => array(
					'relation' => 'or',
					'terms'    => array(
						array(
							'name'  => 'verify_method',
							'value' => 'dob',
						),
						array(
							'name'  => 'verify_method',
							'value' => 'checkbox',
						),
					),
				),
			)
		);

		$this->add_control(
			'field_text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-age-gate__dob-input' => 'color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-age-gate__check'     => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'field_bg_color',
			array(
				'label'     => esc_html__( 'Field Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-age-gate__dob-input' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'field_border',
				'label'    => esc_html__( 'Field Border', 'captain-widgets-kit' ),
				'selector' => '{{WRAPPER}} .captwiki-age-gate__dob-input',
			)
		);

		$this->add_responsive_control(
			'field_radius',
			array(
				'label'      => esc_html__( 'Field Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-age-gate__dob-input' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'error_color',
			array(
				'label'     => esc_html__( 'Error Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-age-gate__error' => 'color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Render the active verification controls.
	 *
	 * @since 1.6.0
	 * @param array  $settings Widget settings.
	 * @param string $base_id  Widget instance id base.
	 * @return void
	 */
	private function captwiki_render_verify( array $settings, string $base_id ): void {
		$method = in_array( $settings['verify_method'], array( 'buttons', 'dob', 'checkbox' ), true ) ? $settings['verify_method'] : 'buttons';

		echo '<div class="captwiki-age-gate__verify">';

		if ( 'buttons' === $method ) {
			$this->captwiki_render_buttons( $settings );
		} elseif ( 'dob' === $method ) {
			$this->captwiki_render_dob( $settings, $base_id );
		} else {
			$this->captwiki_render_checkbox( $settings, $base_id );
		}

		echo '</div>';

		$error = ! empty( $settings['error_text'] ) ? $settings['error_text'] : '';
		echo '<p class="captwiki-age-gate__error" role="alert" hidden>' . esc_html( $error ) . '</p>';
	}

	/**
	 * Render confirm / decline buttons.
	 *
	 * @since 1.6.0
	 * @param array $settings Widget settings.
	 * @return void
	 */
	private function captwiki_render_buttons( array $settings ): void {
		$confirm = ! empty( $settings['confirm_text'] ) ? $settings['confirm_text'] : esc_html__( 'Yes, I am', 'captain-widgets-kit' );
		$decline = ! empty( $settings['decline_text'] ) ? $settings['decline_text'] : esc_html__( 'No, I am not', 'captain-widgets-kit' );

		echo '<div class="captwiki-age-gate__buttons">';
		echo '<button type="button" class="captwiki-age-gate__btn captwiki-age-gate__btn--confirm" data-action="confirm">' . esc_html( $confirm ) . '</button>';
		echo '<button type="button" class="captwiki-age-gate__btn captwiki-age-gate__btn--decline" data-action="decline">' . esc_html( $decline ) . '</button>';
		echo '</div>';
	}

	/**
	 * Render the date-of-birth fields.
	 *
	 * @since 1.6.0
	 * @param array  $settings Widget settings.
	 * @param string $base_id  Widget instance id base.
	 * @return void
	 */
	private function captwiki_render_dob( array $settings, string $base_id ): void {
		$button = ! empty( $settings['dob_button_text'] ) ? $settings['dob_button_text'] : esc_html__( 'Enter', 'captain-widgets-kit' );

		echo '<div class="captwiki-age-gate__dob">';

		echo '<input type="number" inputmode="numeric" class="captwiki-age-gate__dob-input captwiki-age-gate__dob-day" min="1" max="31" placeholder="' . esc_attr__( 'DD', 'captain-widgets-kit' ) . '" aria-label="' . esc_attr__( 'Day', 'captain-widgets-kit' ) . '" id="' . esc_attr( $base_id . '-day' ) . '">';
		echo '<input type="number" inputmode="numeric" class="captwiki-age-gate__dob-input captwiki-age-gate__dob-month" min="1" max="12" placeholder="' . esc_attr__( 'MM', 'captain-widgets-kit' ) . '" aria-label="' . esc_attr__( 'Month', 'captain-widgets-kit' ) . '" id="' . esc_attr( $base_id . '-month' ) . '">';
		echo '<input type="number" inputmode="numeric" class="captwiki-age-gate__dob-input captwiki-age-gate__dob-year" min="1900" max="2100" placeholder="' . esc_attr__( 'YYYY', 'captain-widgets-kit' ) . '" aria-label="' . esc_attr__( 'Year', 'captain-widgets-kit' ) . '" id="' . esc_attr( $base_id . '-year' ) . '">';

		echo '</div>';

		echo '<div class="captwiki-age-gate__buttons">';
		echo '<button type="button" class="captwiki-age-gate__btn captwiki-age-gate__btn--confirm" data-action="verify-dob">' . esc_html( $button ) . '</button>';
		echo '</div>';
	}

	/**
	 * Render the checkbox confirmation.
	 *
	 * @since 1.6.0
	 * @param array  $settings Widget settings.
	 * @param string $base_id  Widget instance id base.
	 * @return void
	 */
	private function captwiki_render_checkbox( array $settings, string $base_id ): void {
		$label  = ! empty( $settings['checkbox_label'] ) ? $settings['checkbox_label'] : esc_html__( 'I confirm I am of legal age.', 'captain-widgets-kit' );
		$button = ! empty( $settings['checkbox_button_text'] ) ? $settings['checkbox_button_text'] : esc_html__( 'Enter Site', 'captain-widgets-kit' );
		$cb_id  = $base_id . '-checkbox';

		echo '<label class="captwiki-age-gate__check" for="' . esc_attr( $cb_id ) . '">';
		echo '<input type="checkbox" class="captwiki-age-gate__checkbox" id="' . esc_attr( $cb_id ) . '">';
		echo '<span class="captwiki-age-gate__check-text">' . esc_html( $label ) . '</span>';
		echo '</label>';

		echo '<div class="captwiki-age-gate__buttons">';
		echo '<button type="button" class="captwiki-age-gate__btn captwiki-age-gate__btn--confirm" data-action="confirm-check">' . esc_html( $button ) . '</button>';
		echo '</div>';
	}

	/**
	 * Render the decline / fail screen.
	 *
	 * @since 1.6.0
	 * @param array $settings Widget settings.
	 * @return void
	 */
	private function captwiki_render_fail( array $settings ): void {
		if ( 'message' !== $settings['decline_action'] ) {
			return;
		}

		$title   = ! empty( $settings['fail_title'] ) ? $settings['fail_title'] : '';
		$message = ! empty( $settings['fail_message'] ) ? $settings['fail_message'] : '';
		$retry   = ! empty( $settings['retry_text'] ) ? $settings['retry_text'] : '';

		echo '<div class="captwiki-age-gate__fail" hidden>';
		if ( '' !== $title ) {
			echo '<h2 class="captwiki-age-gate__fail-title">' . esc_html( $title ) . '</h2>';
		}
		if ( '' !== $message ) {
			echo '<p class="captwiki-age-gate__fail-msg">' . esc_html( $message ) . '</p>';
		}
		if ( '' !== $retry ) {
			echo '<button type="button" class="captwiki-age-gate__btn captwiki-age-gate__btn--retry" data-action="retry">' . esc_html( $retry ) . '</button>';
		}
		echo '</div>';
	}

	/**
	 * Render the optional logo / image.
	 *
	 * @since 1.6.0
	 * @param array $settings Widget settings.
	 * @return void
	 */
	private function captwiki_render_logo( array $settings ): void {
		if ( 'yes' !== $settings['show_logo'] || empty( $settings['logo']['url'] ) ) {
			return;
		}

		$alt = ! empty( $settings['title'] ) ? $settings['title'] : esc_html__( 'Age gate', 'captain-widgets-kit' );

		echo '<div class="captwiki-age-gate__media">';

		if ( ! empty( $settings['logo']['id'] ) ) {
			echo wp_get_attachment_image( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				(int) $settings['logo']['id'],
				'medium',
				false,
				array(
					'class' => 'captwiki-age-gate__logo',
					'alt'   => $alt,
				)
			);
		} else {
			echo '<img class="captwiki-age-gate__logo" src="' . esc_url( $settings['logo']['url'] ) . '" alt="' . esc_attr( $alt ) . '">';
		}

		echo '</div>';
	}

	/**
	 * Build the JS configuration data attributes.
	 *
	 * @since 1.6.0
	 * @param array $settings Widget settings.
	 * @return string Escaped attribute string.
	 */
	private function captwiki_build_config_attrs( array $settings ): string {
		$min_age       = isset( $settings['min_age'] ) ? max( 1, (int) $settings['min_age'] ) : 18;
		$remember_days = isset( $settings['remember_days'] ) ? max( 0, (int) $settings['remember_days'] ) : 30;
		$cookie_key    = ! empty( $settings['cookie_key'] ) ? sanitize_key( $settings['cookie_key'] ) : 'captwiki_age_gate';
		$decline       = 'redirect' === $settings['decline_action'] ? 'redirect' : 'message';
		$redirect_url  = ( 'redirect' === $decline && ! empty( $settings['redirect_url']['url'] ) ) ? $settings['redirect_url']['url'] : '';

		$attrs  = ' data-min-age="' . esc_attr( $min_age ) . '"';
		$attrs .= ' data-remember-days="' . esc_attr( $remember_days ) . '"';
		$attrs .= ' data-storage-key="' . esc_attr( $cookie_key ) . '"';
		$attrs .= ' data-decline-action="' . esc_attr( $decline ) . '"';
		$attrs .= ' data-verify-method="' . esc_attr( $settings['verify_method'] ) . '"';

		if ( '' !== $redirect_url ) {
			$attrs .= ' data-redirect-url="' . esc_url( $redirect_url ) . '"';
		}

		return $attrs;
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	protected function render(): void {
		$settings = $this->get_settings_for_display();

		$allowed_styles = array_keys( $this->captwiki_style_options() );
		$style          = in_array( $settings['widget_style'], $allowed_styles, true ) ? $settings['widget_style'] : 'style-1';

		// Load only the active style stylesheet, depending on the common one.
		wp_enqueue_style(
			'captwiki-age-gate-' . $style,
			CAPTWIKI_URL . 'assets/css/widgets/age-gate/captwiki-age-gate-' . $style . '.css',
			array( 'captwiki_age_gate' ),
			CAPTWIKI_VERSION
		);

		$base_id   = 'captwiki-age-gate-' . $this->get_id();
		$unique    = 'captwiki-widget-' . $this->get_id();
		$title     = ! empty( $settings['title'] ) ? $settings['title'] : '';
		$desc      = ! empty( $settings['description'] ) ? $settings['description'] : '';
		$title_id  = $base_id . '-title';

		$wrap_class = 'captwiki-age-gate captwiki-' . $style . ' ' . $unique;
		$config     = $this->captwiki_build_config_attrs( $settings );

		echo '<div class="' . esc_attr( $wrap_class ) . '"' . $config . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

			echo '<div class="captwiki-age-gate__overlay">';
				echo '<div class="captwiki-age-gate__panel" role="dialog" aria-modal="true" aria-labelledby="' . esc_attr( $title_id ) . '">';

					$this->captwiki_render_logo( $settings );

					echo '<div class="captwiki-age-gate__body">';

		if ( '' !== $title ) {
			echo '<h2 class="captwiki-age-gate__title" id="' . esc_attr( $title_id ) . '">' . esc_html( $title ) . '</h2>';
		}

		if ( '' !== $desc ) {
			echo '<p class="captwiki-age-gate__desc">' . esc_html( $desc ) . '</p>';
		}

					$this->captwiki_render_verify( $settings, $base_id );
					$this->captwiki_render_fail( $settings );

					echo '</div>'; // .captwiki-age-gate__body

				echo '</div>'; // .captwiki-age-gate__panel
			echo '</div>'; // .captwiki-age-gate__overlay

		echo '</div>';
	}
}
