<?php
/**
 * Call To Action widget.
 *
 * @package IMPACT_WIDGETS_ELEMENTOR
 */

namespace ImpactAddons\ImpactWidgetsForElementor\Widgets;

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

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

class Call_To_Action extends Widget_Base {
	public function get_name(): string {
		return 'impact_widgets_elementor_call-to-action';
	}

	public function get_title(): string {
		return esc_html__( 'Call To Action', 'impact-widgets-for-elementor' );
	}

	public function get_icon(): string {
		return 'eicon-call-to-action';
	}

	public function get_categories(): array {
		return array( 'impact-widgets-elementor' );
	}

	public function get_style_depends(): array {
		return array( 'impact_widgets_elementor_call-to-action' );
	}

	protected function register_controls(): void {
		$this->register_content_controls();
		$this->register_style_controls();
	}

	private function register_content_controls(): void {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Content', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			'background_preset',
			array(
				'label'   => esc_html__( 'Background Preset', 'impact-widgets-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'clean-light',
				'options' => array(
					'clean-light'     => esc_html__( 'Clean Light', 'impact-widgets-for-elementor' ),
					'gradient-banner' => esc_html__( 'Gradient Banner', 'impact-widgets-for-elementor' ),
					'action-banner'   => esc_html__( 'Action Banner', 'impact-widgets-for-elementor' ),
					'image-overlay'   => esc_html__( 'Image Overlay', 'impact-widgets-for-elementor' ),
				),
			)
		);

		$this->add_control(
			'gradient_start_color',
			array(
				'label'     => esc_html__( 'Gradient Start', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#1fb2dd',
				'condition' => array(
					'background_preset' => 'gradient-banner',
				),
			)
		);

		$this->add_control(
			'gradient_end_color',
			array(
				'label'     => esc_html__( 'Gradient End', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#39d2c2',
				'condition' => array(
					'background_preset' => 'gradient-banner',
				),
			)
		);

		$this->add_control(
			'background_image',
			array(
				'label'     => esc_html__( 'Background Image', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::MEDIA,
				'dynamic'   => array(
					'active' => true,
				),
				'condition' => array(
					'background_preset' => 'image-overlay',
				),
			)
		);

		$this->add_control(
			'overlay_start_color',
			array(
				'label'     => esc_html__( 'Overlay Start', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => 'rgba(145, 47, 255, 0.82)',
				'condition' => array(
					'background_preset' => 'image-overlay',
				),
			)
		);

		$this->add_control(
			'overlay_end_color',
			array(
				'label'     => esc_html__( 'Overlay End', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => 'rgba(59, 76, 255, 0.78)',
				'condition' => array(
					'background_preset' => 'image-overlay',
				),
			)
		);

		$this->add_control(
			'eyebrow',
			array(
				'label'       => esc_html__( 'Eyebrow', 'impact-widgets-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Limited Availability', 'impact-widgets-for-elementor' ),
				'placeholder' => esc_html__( 'Enter eyebrow text', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			'heading',
			array(
				'label'       => esc_html__( 'Heading', 'impact-widgets-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Create a stronger first impression with a focused CTA.', 'impact-widgets-for-elementor' ),
				'placeholder' => esc_html__( 'Enter heading', 'impact-widgets-for-elementor' ),
				'label_block' => true,
			)
		);

		$this->add_control(
			'description',
			array(
				'label'       => esc_html__( 'Description', 'impact-widgets-for-elementor' ),
				'type'        => Controls_Manager::TEXTAREA,
				'default'     => esc_html__( 'Highlight your offer, explain the value in one sentence, and direct visitors toward the next action.', 'impact-widgets-for-elementor' ),
				'rows'        => 4,
			)
		);

		$this->add_control(
			'show_image',
			array(
				'label'        => esc_html__( 'Show Image', 'impact-widgets-for-elementor' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'impact-widgets-for-elementor' ),
				'label_off'    => esc_html__( 'No', 'impact-widgets-for-elementor' ),
				'return_value' => 'yes',
				'default'      => '',
				'separator'    => 'before',
				'condition'    => array(
					'background_preset!' => 'action-banner',
				),
			)
		);

		$this->add_control(
			'cta_image',
			array(
				'label'     => esc_html__( 'Image', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::MEDIA,
				'dynamic'   => array(
					'active' => true,
				),
				'condition' => array(
					'show_image'         => 'yes',
					'background_preset!' => 'action-banner',
				),
			)
		);

		$this->add_control(
			'cta_image_position',
			array(
				'label'     => esc_html__( 'Image Position', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left' => array(
						'title' => esc_html__( 'Left', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-h-align-left',
					),
					'right' => array(
						'title' => esc_html__( 'Right', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-h-align-right',
					),
				),
				'default'   => 'right',
				'toggle'    => false,
				'condition' => array(
					'show_image'         => 'yes',
					'background_preset!' => 'action-banner',
				),
			)
		);

		$this->add_control(
			'primary_button_heading',
			array(
				'label'     => esc_html__( 'Primary Button', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'primary_button_text',
			array(
				'label'       => esc_html__( 'Text', 'impact-widgets-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Get Started', 'impact-widgets-for-elementor' ),
				'placeholder' => esc_html__( 'Enter button text', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			'primary_button_icon',
			array(
				'label'            => esc_html__( 'Icon', 'impact-widgets-for-elementor' ),
				'type'             => Controls_Manager::ICONS,
				'fa4compatibility' => 'primary_button_icon_fa4',
			)
		);

		$this->add_control(
			'primary_button_icon_position',
			array(
				'label'   => esc_html__( 'Icon Position', 'impact-widgets-for-elementor' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => array(
					'left' => array(
						'title' => esc_html__( 'Left', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-h-align-left',
					),
					'right' => array(
						'title' => esc_html__( 'Right', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-h-align-right',
					),
				),
				'default' => 'left',
				'toggle'  => false,
			)
		);

		$this->add_control(
			'primary_button_link',
			array(
				'label'         => esc_html__( 'Link', 'impact-widgets-for-elementor' ),
				'type'          => Controls_Manager::URL,
				'placeholder'   => esc_html__( 'https://your-link.com', 'impact-widgets-for-elementor' ),
				'show_external' => true,
				'default'       => array(
					'url' => '#',
				),
			)
		);

		$this->add_control(
			'secondary_button_heading',
			array(
				'label'     => esc_html__( 'Secondary Button', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'secondary_button_text',
			array(
				'label'       => esc_html__( 'Text', 'impact-widgets-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Learn More', 'impact-widgets-for-elementor' ),
				'placeholder' => esc_html__( 'Enter button text', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			'secondary_button_icon',
			array(
				'label'            => esc_html__( 'Icon', 'impact-widgets-for-elementor' ),
				'type'             => Controls_Manager::ICONS,
				'fa4compatibility' => 'secondary_button_icon_fa4',
			)
		);

		$this->add_control(
			'secondary_button_icon_position',
			array(
				'label'   => esc_html__( 'Icon Position', 'impact-widgets-for-elementor' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => array(
					'left' => array(
						'title' => esc_html__( 'Left', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-h-align-left',
					),
					'right' => array(
						'title' => esc_html__( 'Right', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-h-align-right',
					),
				),
				'default' => 'left',
				'toggle'  => false,
			)
		);

		$this->add_control(
			'secondary_button_link',
			array(
				'label'         => esc_html__( 'Link', 'impact-widgets-for-elementor' ),
				'type'          => Controls_Manager::URL,
				'placeholder'   => esc_html__( 'https://your-link.com', 'impact-widgets-for-elementor' ),
				'show_external' => true,
				'default'       => array(
					'url' => '',
				),
			)
		);

		$this->end_controls_section();
	}

	private function register_style_controls(): void {
		$this->start_controls_section(
			'section_style_wrapper',
			array(
				'label' => esc_html__( 'Card', 'impact-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'content_align',
			array(
				'label'     => esc_html__( 'Alignment', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left' => array(
						'title' => esc_html__( 'Left', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right' => array(
						'title' => esc_html__( 'Right', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'   => 'left',
				'prefix_class' => 'iaew-cta-align-',
			)
		);

		$this->add_responsive_control(
			'card_padding',
			array(
				'label'      => esc_html__( 'Padding', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-cta__inner' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'card_gap',
			array(
				'label'      => esc_html__( 'Gap', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 120,
					),
				),
				'default'    => array(
					'size' => 36,
					'unit' => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-cta' => '--iaew-cta-content-gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'image_overlay_height',
			array(
				'label'      => esc_html__( 'Image Overlay Height', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'vh' ),
				'range'      => array(
					'px' => array(
						'min' => 160,
						'max' => 1200,
					),
					'vh' => array(
						'min' => 20,
						'max' => 100,
					),
				),
				'default'    => array(
					'size' => 620,
					'unit' => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-cta--preset-image-overlay .iaew-cta__inner' => '--iaew-cta-image-overlay-height: {{SIZE}}{{UNIT}};',
				),
				'condition'  => array(
					'background_preset' => 'image-overlay',
				),
			)
		);

		$this->add_responsive_control(
			'card_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-cta__inner' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'card_background',
				'selector' => '{{WRAPPER}} .iaew-cta__inner',
			)
		);

		$this->add_control(
			'card_background_overlay_heading',
			array(
				'label'     => esc_html__( 'Background Overlay', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'card_background_overlay_color',
			array(
				'label'     => esc_html__( 'Overlay Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-cta__background-overlay' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'card_background_overlay_opacity',
			array(
				'label'      => esc_html__( 'Overlay Opacity', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( '%' ),
				'range'      => array(
					'%' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => '%',
					'size' => 0,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-cta__background-overlay' => 'opacity: calc({{SIZE}} / 100);',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'card_border',
				'selector' => '{{WRAPPER}} .iaew-cta__inner',
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'card_shadow',
				'selector' => '{{WRAPPER}} .iaew-cta__inner',
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_text',
			array(
				'label' => esc_html__( 'Text', 'impact-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'eyebrow_color',
			array(
				'label'     => esc_html__( 'Eyebrow Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-cta__eyebrow' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'eyebrow_typography',
				'selector' => '{{WRAPPER}} .iaew-cta__eyebrow',
			)
		);

		$this->add_control(
			'heading_color',
			array(
				'label'     => esc_html__( 'Heading Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-cta__heading' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'heading_typography',
				'selector' => '{{WRAPPER}} .iaew-cta__heading',
			)
		);

		$this->add_control(
			'description_color',
			array(
				'label'     => esc_html__( 'Description Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-cta__description' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'description_typography',
				'selector' => '{{WRAPPER}} .iaew-cta__description',
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_media',
			array(
				'label'     => esc_html__( 'Image', 'impact-widgets-for-elementor' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array(
					'background_preset!' => 'action-banner',
				),
			)
		);

		$this->add_responsive_control(
			'cta_image_size',
			array(
				'label'      => esc_html__( 'Max Width', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 80,
						'max' => 800,
					),
					'%'  => array(
						'min' => 10,
						'max' => 100,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-cta__media' => 'max-width: {{SIZE}}{{UNIT}};',
				),
				'condition'  => array(
					'show_image' => 'yes',
				),
			)
		);

		$this->add_responsive_control(
			'cta_image_width',
			array(
				'label'      => esc_html__( 'Width', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 40,
						'max' => 800,
					),
					'%'  => array(
						'min' => 10,
						'max' => 100,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-cta__media' => 'width: {{SIZE}}{{UNIT}};',
				),
				'condition'  => array(
					'show_image' => 'yes',
				),
			)
		);

		$this->add_responsive_control(
			'cta_image_height',
			array(
				'label'      => esc_html__( 'Height', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'vh' ),
				'range'      => array(
					'px' => array(
						'min' => 40,
						'max' => 900,
					),
					'vh' => array(
						'min' => 10,
						'max' => 100,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-cta__image' => 'height: {{SIZE}}{{UNIT}};',
				),
				'condition'  => array(
					'show_image' => 'yes',
				),
			)
		);

		$this->add_control(
			'cta_image_object_fit',
			array(
				'label'     => esc_html__( 'Object Fit', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'cover',
				'options'   => array(
					'cover'   => esc_html__( 'Cover', 'impact-widgets-for-elementor' ),
					'contain' => esc_html__( 'Contain', 'impact-widgets-for-elementor' ),
					'fill'    => esc_html__( 'Fill', 'impact-widgets-for-elementor' ),
				),
				'selectors' => array(
					'{{WRAPPER}} .iaew-cta__image' => 'object-fit: {{VALUE}};',
				),
				'condition' => array(
					'show_image' => 'yes',
				),
			)
		);

		$this->add_responsive_control(
			'cta_image_padding',
			array(
				'label'      => esc_html__( 'Padding', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-cta__image' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
				'condition'  => array(
					'show_image' => 'yes',
				),
			)
		);

		$this->add_responsive_control(
			'cta_image_margin',
			array(
				'label'      => esc_html__( 'Margin', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-cta__media' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
				'condition'  => array(
					'show_image' => 'yes',
				),
			)
		);

		$this->add_responsive_control(
			'cta_image_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-cta__image' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
				'condition'  => array(
					'show_image' => 'yes',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'      => 'cta_image_border',
				'selector'  => '{{WRAPPER}} .iaew-cta__image',
				'condition' => array(
					'show_image' => 'yes',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'      => 'cta_image_shadow',
				'selector'  => '{{WRAPPER}} .iaew-cta__image',
				'condition' => array(
					'show_image' => 'yes',
				),
			)
		);

		$this->add_responsive_control(
			'cta_image_opacity',
			array(
				'label'      => esc_html__( 'Opacity', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( '%' ),
				'range'      => array(
					'%' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => '%',
					'size' => 100,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-cta__image' => 'opacity: calc({{SIZE}} / 100);',
				),
				'condition'  => array(
					'show_image' => 'yes',
				),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_primary_button',
			array(
				'label' => esc_html__( 'Primary Button', 'impact-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->register_button_style_controls( 'primary', '{{WRAPPER}} .iaew-cta__button--primary' );

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_secondary_button',
			array(
				'label' => esc_html__( 'Secondary Button', 'impact-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->register_button_style_controls( 'secondary', '{{WRAPPER}} .iaew-cta__button--secondary' );

		$this->end_controls_section();
	}

	private function register_button_style_controls( string $prefix, string $selector ): void {
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => $prefix . '_button_typography',
				'selector' => $selector,
			)
		);

		$this->add_responsive_control(
			$prefix . '_button_padding',
			array(
				'label'      => esc_html__( 'Padding', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					$selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			$prefix . '_button_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					$selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			$prefix . '_button_icon_size',
			array(
				'label'      => esc_html__( 'Icon Size', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 8,
						'max' => 72,
					),
				),
				'selectors'  => array(
					$selector . ' .iaew-cta__button-icon' => 'font-size: {{SIZE}}{{UNIT}};',
					$selector . ' .iaew-cta__button-icon svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			$prefix . '_button_icon_gap',
			array(
				'label'      => esc_html__( 'Icon Gap', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 40,
					),
				),
				'selectors'  => array(
					$selector => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->start_controls_tabs( $prefix . '_button_tabs' );

		$this->start_controls_tab(
			$prefix . '_button_normal',
			array(
				'label' => esc_html__( 'Normal', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			$prefix . '_button_text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$selector => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			$prefix . '_button_icon_color',
			array(
				'label'     => esc_html__( 'Icon Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$selector . ' .iaew-cta__button-icon' => 'color: {{VALUE}};',
					$selector . ' .iaew-cta__button-icon svg' => 'fill: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			$prefix . '_button_background_color',
			array(
				'label'     => esc_html__( 'Background Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$selector => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => $prefix . '_button_border',
				'selector' => $selector,
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			$prefix . '_button_hover',
			array(
				'label' => esc_html__( 'Hover', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			$prefix . '_button_hover_text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$selector . ':hover, ' . $selector . ':focus' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			$prefix . '_button_hover_icon_color',
			array(
				'label'     => esc_html__( 'Icon Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$selector . ':hover .iaew-cta__button-icon, ' . $selector . ':focus .iaew-cta__button-icon' => 'color: {{VALUE}};',
					$selector . ':hover .iaew-cta__button-icon svg, ' . $selector . ':focus .iaew-cta__button-icon svg' => 'fill: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			$prefix . '_button_hover_background_color',
			array(
				'label'     => esc_html__( 'Background Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$selector . ':hover, ' . $selector . ':focus' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			$prefix . '_button_hover_border_color',
			array(
				'label'     => esc_html__( 'Border Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$selector . ':hover, ' . $selector . ':focus' => 'border-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();
	}

	private function render_button_content( string $text, array $icon_settings, string $icon_position ): void {
		$has_icon = ! empty( $icon_settings['value'] );
		$icon_position = in_array( $icon_position, array( 'left', 'right' ), true ) ? $icon_position : 'left';

		if ( $has_icon && 'left' === $icon_position ) {
			?>
			<span class="iaew-cta__button-icon" aria-hidden="true">
				<?php Icons_Manager::render_icon( $icon_settings, array( 'aria-hidden' => 'true' ) ); ?>
			</span>
			<?php
		}
		?>
		<span class="iaew-cta__button-text"><?php echo esc_html( $text ); ?></span>
		<?php
		if ( $has_icon && 'right' === $icon_position ) {
			?>
			<span class="iaew-cta__button-icon" aria-hidden="true">
				<?php Icons_Manager::render_icon( $icon_settings, array( 'aria-hidden' => 'true' ) ); ?>
			</span>
			<?php
		}
	}

	protected function render(): void {
		$settings = $this->get_settings_for_display();

		$background_preset = isset( $settings['background_preset'] ) ? sanitize_key( $settings['background_preset'] ) : 'clean-light';
		if ( ! in_array( $background_preset, array( 'clean-light', 'gradient-banner', 'action-banner', 'image-overlay' ), true ) ) {
			$background_preset = 'clean-light';
		}

		$eyebrow = isset( $settings['eyebrow'] ) ? sanitize_text_field( $settings['eyebrow'] ) : '';
		$heading = isset( $settings['heading'] ) ? sanitize_text_field( $settings['heading'] ) : '';
		$description = isset( $settings['description'] ) ? sanitize_textarea_field( $settings['description'] ) : '';
		$background_image_url = ! empty( $settings['background_image']['url'] ) ? esc_url( $settings['background_image']['url'] ) : '';
		$cta_image_id = ! empty( $settings['cta_image']['id'] ) ? absint( $settings['cta_image']['id'] ) : 0;
		$cta_image_url = ! empty( $settings['cta_image']['url'] ) ? esc_url( $settings['cta_image']['url'] ) : '';
		$show_image = isset( $settings['show_image'] ) && 'yes' === $settings['show_image'] && ( $cta_image_id || '' !== $cta_image_url );
		$cta_image_position = isset( $settings['cta_image_position'] ) && in_array( $settings['cta_image_position'], array( 'left', 'right' ), true ) ? $settings['cta_image_position'] : 'right';
		$primary_text = isset( $settings['primary_button_text'] ) ? sanitize_text_field( $settings['primary_button_text'] ) : '';
		$secondary_text = isset( $settings['secondary_button_text'] ) ? sanitize_text_field( $settings['secondary_button_text'] ) : '';
		$primary_button_icon = isset( $settings['primary_button_icon'] ) && is_array( $settings['primary_button_icon'] ) ? $settings['primary_button_icon'] : array();
		$secondary_button_icon = isset( $settings['secondary_button_icon'] ) && is_array( $settings['secondary_button_icon'] ) ? $settings['secondary_button_icon'] : array();
		$primary_button_icon_position = isset( $settings['primary_button_icon_position'] ) ? sanitize_key( $settings['primary_button_icon_position'] ) : 'left';
		$secondary_button_icon_position = isset( $settings['secondary_button_icon_position'] ) ? sanitize_key( $settings['secondary_button_icon_position'] ) : 'left';
		$overlay_start = ! empty( $settings['overlay_start_color'] ) ? sanitize_text_field( $settings['overlay_start_color'] ) : 'rgba(145, 47, 255, 0.82)';
		$overlay_end = ! empty( $settings['overlay_end_color'] ) ? sanitize_text_field( $settings['overlay_end_color'] ) : 'rgba(59, 76, 255, 0.78)';
		$gradient_start = ! empty( $settings['gradient_start_color'] ) ? sanitize_text_field( $settings['gradient_start_color'] ) : '#1fb2dd';
		$gradient_end = ! empty( $settings['gradient_end_color'] ) ? sanitize_text_field( $settings['gradient_end_color'] ) : '#39d2c2';
		$is_action_banner = 'action-banner' === $background_preset;
		if ( $is_action_banner ) {
			$show_image = false;
		}

		$has_primary_button = '' !== $primary_text;
		$has_secondary_button = '' !== $secondary_text;

		$this->add_render_attribute(
			'wrapper',
			'class',
			array(
				'iaew-cta',
				'iaew-cta--preset-' . sanitize_html_class( $background_preset ),
				$show_image ? 'iaew-cta--has-media' : 'iaew-cta--no-media',
				'iaew-cta--media-' . sanitize_html_class( $cta_image_position ),
			)
		);

		if ( 'image-overlay' === $background_preset || 'gradient-banner' === $background_preset ) {
			$style_rules = array();

			if ( 'image-overlay' === $background_preset ) {
				$style_rules[] = '--iaew-cta-overlay-start: ' . esc_attr( $overlay_start );
				$style_rules[] = '--iaew-cta-overlay-end: ' . esc_attr( $overlay_end );

				if ( '' !== $background_image_url ) {
					$style_rules[] = '--iaew-cta-background-image: url(' . esc_url( $background_image_url ) . ')';
				}
			}

			if ( 'gradient-banner' === $background_preset ) {
				$style_rules[] = '--iaew-cta-gradient-start: ' . esc_attr( $gradient_start );
				$style_rules[] = '--iaew-cta-gradient-end: ' . esc_attr( $gradient_end );
			}

			$inner_style = implode( '; ', $style_rules ) . ';';
			$this->add_render_attribute( 'inner', 'style', $inner_style );
		}

		if ( $has_primary_button ) {
			$this->add_render_attribute(
				'primary_button',
				'class',
				array(
					'iaew-cta__button',
					'iaew-cta__button--primary',
				)
			);
			$this->add_link_attributes( 'primary_button', $settings['primary_button_link'] );
		}

		if ( $has_secondary_button ) {
			$this->add_render_attribute(
				'secondary_button',
				'class',
				array(
					'iaew-cta__button',
					'iaew-cta__button--secondary',
				)
			);
			$this->add_link_attributes( 'secondary_button', $settings['secondary_button_link'] );
		}

		?>
		<div <?php $this->print_render_attribute_string( 'wrapper' ); ?>>
			<div class="iaew-cta__inner" <?php $this->print_render_attribute_string( 'inner' ); ?>>
				<span class="iaew-cta__background-overlay" aria-hidden="true"></span>
				<?php if ( $show_image ) : ?>
					<div class="iaew-cta__media">
						<?php
						if ( $cta_image_id ) {
							echo wp_kses_post( wp_get_attachment_image( $cta_image_id, 'full', false, array( 'class' => 'iaew-cta__image' ) ) );
						} elseif ( '' !== $cta_image_url ) {
							?>
							<img class="iaew-cta__image" src="<?php echo esc_url( $cta_image_url ); ?>" alt="" />
							<?php
						}
						?>
					</div>
				<?php endif; ?>
				<div class="iaew-cta__content">
					<?php if ( '' !== $eyebrow ) : ?>
						<div class="iaew-cta__eyebrow"><?php echo esc_html( $eyebrow ); ?></div>
					<?php endif; ?>

					<?php if ( '' !== $heading ) : ?>
						<h2 class="iaew-cta__heading"><?php echo esc_html( $heading ); ?></h2>
					<?php endif; ?>

					<?php if ( '' !== $description ) : ?>
						<p class="iaew-cta__description"><?php echo esc_html( $description ); ?></p>
					<?php endif; ?>

					<?php if ( ! $is_action_banner && ( $has_primary_button || $has_secondary_button ) ) : ?>
						<div class="iaew-cta__actions">
							<?php if ( $has_primary_button ) : ?>
								<a <?php $this->print_render_attribute_string( 'primary_button' ); ?>><?php $this->render_button_content( $primary_text, $primary_button_icon, $primary_button_icon_position ); ?></a>
							<?php endif; ?>

							<?php if ( $has_secondary_button ) : ?>
								<a <?php $this->print_render_attribute_string( 'secondary_button' ); ?>><?php $this->render_button_content( $secondary_text, $secondary_button_icon, $secondary_button_icon_position ); ?></a>
							<?php endif; ?>
						</div>
					<?php endif; ?>
				</div>
				<?php if ( $is_action_banner && ( $has_primary_button || $has_secondary_button ) ) : ?>
					<div class="iaew-cta__actions iaew-cta__actions--aside">
						<?php if ( $has_primary_button ) : ?>
							<a <?php $this->print_render_attribute_string( 'primary_button' ); ?>><?php $this->render_button_content( $primary_text, $primary_button_icon, $primary_button_icon_position ); ?></a>
						<?php endif; ?>

						<?php if ( $has_secondary_button ) : ?>
							<a <?php $this->print_render_attribute_string( 'secondary_button' ); ?>><?php $this->render_button_content( $secondary_text, $secondary_button_icon, $secondary_button_icon_position ); ?></a>
						<?php endif; ?>
					</div>
				<?php endif; ?>

			</div>
		</div>
		<?php
	}
}
