<?php
/**
 * Widget Name: Cookie Consent
 * Description: A GDPR-style cookie consent notice that appears until the visitor
 *              accepts or rejects cookies. Supports an icon, a privacy-policy link,
 *              optional Reject and Preferences buttons, a configurable position, a
 *              show delay, a remembered choice (localStorage + optional browser
 *              cookie), a custom "captwiki:cookie-consent" JS event and five
 *              distinctive layouts. Editor-safe: it renders inline inside Elementor.
 *              Only the selected style CSS loads on the frontend.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.7.0
 */

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

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

/**
 * Class Captwiki_Cookie_Consent
 *
 * @since 1.7.0
 */
class Captwiki_Cookie_Consent extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @since 1.7.0
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-cookie-consent';
	}

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

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

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

	/**
	 * Get widget keywords.
	 *
	 * @since 1.7.0
	 * @return array
	 */
	public function get_keywords(): array {
		return array( 'cookie', 'consent', 'gdpr', 'privacy', 'notice', 'banner', 'popup', 'captain-widgets-kit' );
	}

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

	/**
	 * Frontend script dependency.
	 *
	 * @since 1.7.0
	 * @return array
	 */
	public function get_script_depends(): array {
		return array( 'captwiki-cookie-consent' );
	}

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

	/**
	 * Selectable visual styles ( consent-specific layouts ).
	 *
	 * @since 1.7.0
	 * @return array
	 */
	private function captwiki_style_options(): array {
		return array(
			'style-1' => esc_html__( 'Style 1 – Floating Card', 'captain-widgets-kit' ),
			'style-2' => esc_html__( 'Style 2 – Full-Width Bar', 'captain-widgets-kit' ),
			'style-3' => esc_html__( 'Style 3 – Centered Modal', 'captain-widgets-kit' ),
			'style-4' => esc_html__( 'Style 4 – Minimal Pill', 'captain-widgets-kit' ),
			'style-5' => esc_html__( 'Style 5 – Bold Icon Toast', 'captain-widgets-kit' ),
		);
	}

	/**
	 * Position options for the floating layouts.
	 *
	 * @since 1.7.0
	 * @return array
	 */
	private function captwiki_position_options(): array {
		return array(
			'bottom-left'   => esc_html__( 'Bottom Left', 'captain-widgets-kit' ),
			'bottom-center' => esc_html__( 'Bottom Center', 'captain-widgets-kit' ),
			'bottom-right'  => esc_html__( 'Bottom Right', 'captain-widgets-kit' ),
			'top-left'      => esc_html__( 'Top Left', 'captain-widgets-kit' ),
			'top-center'    => esc_html__( 'Top Center', 'captain-widgets-kit' ),
			'top-right'     => esc_html__( 'Top Right', 'captain-widgets-kit' ),
		);
	}

	/**
	 * Register widget controls.
	 *
	 * @since 1.7.0
	 * @return void
	 */
	protected function register_controls(): void {
		$this->register_content_controls();
		$this->register_button_controls();
		$this->register_settings_controls();

		$this->register_box_style_controls();
		$this->register_icon_style_controls();
		$this->register_text_style_controls();
		$this->register_button_style_controls();
	}

	/**
	 * Content tab — primary content.
	 *
	 * @since 1.7.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_icon',
			array(
				'label'        => esc_html__( 'Show Icon', '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(
			'icon',
			array(
				'label'     => esc_html__( 'Icon', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'fas fa-cookie-bite',
					'library' => 'fa-solid',
				),
				'condition' => array( 'show_icon' => 'yes' ),
			)
		);

		$this->add_control(
			'title',
			array(
				'label'       => esc_html__( 'Title', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'We value your privacy', 'captain-widgets-kit' ),
				'label_block' => true,
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->add_control(
			'message',
			array(
				'label'   => esc_html__( 'Message', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::TEXTAREA,
				'rows'    => 3,
				'default' => esc_html__( 'We use cookies to enhance your browsing experience and analyse our traffic. By clicking "Accept", you consent to our use of cookies.', 'captain-widgets-kit' ),
				'dynamic' => array( 'active' => true ),
			)
		);

		$this->add_control(
			'learn_more_text',
			array(
				'label'       => esc_html__( 'Privacy Link Text', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Learn more', 'captain-widgets-kit' ),
				'label_block' => true,
			)
		);

		$this->add_control(
			'learn_more_link',
			array(
				'label'         => esc_html__( 'Privacy Policy URL', 'captain-widgets-kit' ),
				'type'          => Controls_Manager::URL,
				'placeholder'   => esc_html__( 'https://your-site.com/privacy-policy', 'captain-widgets-kit' ),
				'show_external' => true,
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Content tab — buttons.
	 *
	 * @since 1.7.0
	 * @return void
	 */
	private function register_button_controls(): void {

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

		$this->add_control(
			'accept_text',
			array(
				'label'   => esc_html__( 'Accept Button', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'Accept', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'show_reject',
			array(
				'label'        => esc_html__( 'Show Reject Button', '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(
			'reject_text',
			array(
				'label'     => esc_html__( 'Reject Button', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Reject', 'captain-widgets-kit' ),
				'condition' => array( 'show_reject' => 'yes' ),
			)
		);

		$this->add_control(
			'show_settings',
			array(
				'label'        => esc_html__( 'Show Preferences Button', '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'      => '',
			)
		);

		$this->add_control(
			'settings_text',
			array(
				'label'     => esc_html__( 'Preferences Button', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Preferences', 'captain-widgets-kit' ),
				'condition' => array( 'show_settings' => 'yes' ),
			)
		);

		$this->add_control(
			'settings_link',
			array(
				'label'         => esc_html__( 'Preferences URL', 'captain-widgets-kit' ),
				'type'          => Controls_Manager::URL,
				'placeholder'   => esc_html__( 'https://your-site.com/cookie-settings', 'captain-widgets-kit' ),
				'show_external' => true,
				'condition'     => array( 'show_settings' => 'yes' ),
				'description'   => esc_html__( 'Leave empty to fire a "captwiki:cookie-preferences" event instead ( for your own preferences panel ).', 'captain-widgets-kit' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Content tab — behaviour settings.
	 *
	 * @since 1.7.0
	 * @return void
	 */
	private function register_settings_controls(): void {

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

		$this->add_control(
			'position',
			array(
				'label'       => esc_html__( 'Position', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::SELECT,
				'default'     => 'bottom-left',
				'options'     => $this->captwiki_position_options(),
				'description' => esc_html__( 'Applies to the floating, pill and toast styles. The bar uses top/bottom only; the modal is always centered.', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'show_delay',
			array(
				'label'       => esc_html__( 'Show Delay ( Seconds )', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 0,
				'max'         => 30,
				'step'        => 1,
				'default'     => 1,
				'description' => esc_html__( 'Wait this long after page load before showing the notice.', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'remember_days',
			array(
				'label'       => esc_html__( 'Remember For ( Days )', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 1,
				'max'         => 365,
				'step'        => 1,
				'default'     => 180,
				'description' => esc_html__( 'How long to remember the visitor\'s choice before asking again.', 'captain-widgets-kit' ),
			)
		);

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

		$this->add_control(
			'set_cookie',
			array(
				'label'        => esc_html__( 'Also Set Browser Cookie', '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',
				'description'  => esc_html__( 'Writes the choice to a cookie too, so the server can read consent.', 'captain-widgets-kit' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Style tab — box container.
	 *
	 * @since 1.7.0
	 * @return void
	 */
	private function register_box_style_controls(): void {

		$this->start_controls_section(
			'section_box_style',
			array(
				'label' => esc_html__( 'Box', '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-cookie' => '--captwiki-cookie-accent: {{VALUE}};',
				),
				'description' => esc_html__( 'Drives the Accept button and links.', 'captain-widgets-kit' ),
			)
		);

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

		$this->add_responsive_control(
			'box_width',
			array(
				'label'      => esc_html__( 'Max Width', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array( 'min' => 260, 'max' => 1200 ),
					'%'  => array( 'min' => 20, 'max' => 100 ),
				),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-cookie__box' => 'max-width: {{SIZE}}{{UNIT}};',
				),
				'description' => esc_html__( 'Ignored by the Full-Width Bar style.', 'captain-widgets-kit' ),
			)
		);

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

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

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

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

		$this->add_control(
			'backdrop_color',
			array(
				'label'       => esc_html__( 'Modal Backdrop', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::COLOR,
				'default'     => '',
				'selectors'   => array(
					'{{WRAPPER}} .captwiki-cookie__backdrop' => 'background-color: {{VALUE}};',
				),
				'description' => esc_html__( 'Used by the Centered Modal style only.', 'captain-widgets-kit' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Style tab — icon.
	 *
	 * @since 1.7.0
	 * @return void
	 */
	private function register_icon_style_controls(): void {

		$this->start_controls_section(
			'section_icon_style',
			array(
				'label'     => esc_html__( 'Icon', 'captain-widgets-kit' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_icon' => 'yes' ),
			)
		);

		$this->add_control(
			'icon_color',
			array(
				'label'     => esc_html__( 'Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-cookie__icon' => 'color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-cookie__icon svg' => 'fill: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'icon_size',
			array(
				'label'      => esc_html__( 'Size', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 14, 'max' => 80 ) ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-cookie__icon' => 'font-size: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Style tab — title & message.
	 *
	 * @since 1.7.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_control(
			'title_color',
			array(
				'label'     => esc_html__( 'Title Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-cookie__title' => 'color: {{VALUE}};',
				),
			)
		);

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

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

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

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

		$this->end_controls_section();
	}

	/**
	 * Style tab — buttons.
	 *
	 * @since 1.7.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-cookie__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-cookie__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-cookie__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' => 30 ) ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-cookie__actions' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

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

		$this->start_controls_tabs( 'accept_state_tabs' );

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

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

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

		$this->end_controls_tab();

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

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

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

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

		// Secondary buttons.
		$this->add_control(
			'heading_secondary',
			array(
				'label'     => esc_html__( 'Reject / Preferences', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->start_controls_tabs( 'secondary_state_tabs' );

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

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

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

		$this->end_controls_tab();

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

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

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

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

		$this->end_controls_section();
	}

	/**
	 * Render the optional icon.
	 *
	 * @since 1.7.0
	 * @param array $settings Widget settings.
	 * @return void
	 */
	private function captwiki_render_icon( array $settings ): void {
		if ( 'yes' !== $settings['show_icon'] || empty( $settings['icon']['value'] ) ) {
			return;
		}

		echo '<span class="captwiki-cookie__icon" aria-hidden="true">';
		Icons_Manager::render_icon( $settings['icon'], array( 'aria-hidden' => 'true' ) );
		echo '</span>';
	}

	/**
	 * Render the message with an optional privacy-policy link.
	 *
	 * @since 1.7.0
	 * @param array $settings Widget settings.
	 * @return void
	 */
	private function captwiki_render_message( array $settings ): void {
		$message = ! empty( $settings['message'] ) ? $settings['message'] : '';

		if ( '' === $message ) {
			return;
		}

		echo '<p class="captwiki-cookie__message">';
		echo esc_html( $message );

		if ( ! empty( $settings['learn_more_text'] ) && ! empty( $settings['learn_more_link']['url'] ) ) {
			$target = ! empty( $settings['learn_more_link']['is_external'] ) ? ' target="_blank"' : '';
			$rel    = ! empty( $settings['learn_more_link']['nofollow'] ) ? ' rel="nofollow"' : '';

			echo ' <a class="captwiki-cookie__link" href="' . esc_url( $settings['learn_more_link']['url'] ) . '"' . $target . $rel . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			echo esc_html( $settings['learn_more_text'] );
			echo '</a>';
		}

		echo '</p>';
	}

	/**
	 * Render the action buttons.
	 *
	 * @since 1.7.0
	 * @param array $settings Widget settings.
	 * @return void
	 */
	private function captwiki_render_actions( array $settings ): void {
		$accept = ! empty( $settings['accept_text'] ) ? $settings['accept_text'] : esc_html__( 'Accept', 'captain-widgets-kit' );

		echo '<div class="captwiki-cookie__actions">';

		echo '<button type="button" class="captwiki-cookie__btn captwiki-cookie__btn--accept" data-action="accept">' . esc_html( $accept ) . '</button>';

		if ( 'yes' === $settings['show_reject'] ) {
			$reject = ! empty( $settings['reject_text'] ) ? $settings['reject_text'] : esc_html__( 'Reject', 'captain-widgets-kit' );
			echo '<button type="button" class="captwiki-cookie__btn captwiki-cookie__btn--reject" data-action="reject">' . esc_html( $reject ) . '</button>';
		}

		if ( 'yes' === $settings['show_settings'] ) {
			$settings_text = ! empty( $settings['settings_text'] ) ? $settings['settings_text'] : esc_html__( 'Preferences', 'captain-widgets-kit' );

			if ( ! empty( $settings['settings_link']['url'] ) ) {
				$target = ! empty( $settings['settings_link']['is_external'] ) ? ' target="_blank"' : '';
				$rel    = ! empty( $settings['settings_link']['nofollow'] ) ? ' rel="nofollow"' : '';
				echo '<a class="captwiki-cookie__btn captwiki-cookie__btn--settings" href="' . esc_url( $settings['settings_link']['url'] ) . '"' . $target . $rel . '>' . esc_html( $settings_text ) . '</a>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			} else {
				echo '<button type="button" class="captwiki-cookie__btn captwiki-cookie__btn--settings" data-action="settings">' . esc_html( $settings_text ) . '</button>';
			}
		}

		echo '</div>';
	}

	/**
	 * Build the JS configuration data attributes.
	 *
	 * @since 1.7.0
	 * @param array $settings Widget settings.
	 * @return string Escaped attribute string.
	 */
	private function captwiki_build_config_attrs( array $settings ): string {
		$remember_days = isset( $settings['remember_days'] ) ? max( 1, (int) $settings['remember_days'] ) : 180;
		$delay         = isset( $settings['show_delay'] ) ? max( 0, (int) $settings['show_delay'] ) : 1;
		$cookie_key    = ! empty( $settings['cookie_key'] ) ? sanitize_key( $settings['cookie_key'] ) : 'captwiki_cookie_consent';
		$set_cookie    = 'yes' === $settings['set_cookie'] ? '1' : '0';

		$attrs  = ' data-remember-days="' . esc_attr( $remember_days ) . '"';
		$attrs .= ' data-delay="' . esc_attr( $delay ) . '"';
		$attrs .= ' data-storage-key="' . esc_attr( $cookie_key ) . '"';
		$attrs .= ' data-set-cookie="' . esc_attr( $set_cookie ) . '"';

		return $attrs;
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * @since 1.7.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-cookie-consent-' . $style,
			CAPTWIKI_URL . 'assets/css/widgets/cookie-consent/captwiki-cookie-consent-' . $style . '.css',
			array( 'captwiki_cookie_consent' ),
			CAPTWIKI_VERSION
		);

		$allowed_pos = array_keys( $this->captwiki_position_options() );
		$position    = in_array( $settings['position'], $allowed_pos, true ) ? $settings['position'] : 'bottom-left';

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

		$wrap_class  = 'captwiki-cookie captwiki-' . $style . ' captwiki-cookie--pos-' . $position . ' ' . $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-cookie__backdrop"></div>';

			echo '<div class="captwiki-cookie__box" role="dialog" aria-live="polite" aria-label="' . esc_attr__( 'Cookie consent', 'captain-widgets-kit' ) . '"' . ( '' !== $title ? ' aria-labelledby="' . esc_attr( $title_id ) . '"' : '' ) . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

				$this->captwiki_render_icon( $settings );

				echo '<div class="captwiki-cookie__content">';

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

				$this->captwiki_render_message( $settings );

				echo '</div>'; // .captwiki-cookie__content

				$this->captwiki_render_actions( $settings );

			echo '</div>'; // .captwiki-cookie__box

		echo '</div>';
	}
}
