<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

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\Group_Control_Image_Size;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Core_Text_Style;
use Sifency\Traits\Sifency_Button_Style_Controls;

class Sifency_Call_To_Action extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Core_Text_Style;
    use Sifency_Button_Style_Controls;

	public function get_name() {
		return 'sifency_cta';
	}

	public function get_title() {
		return esc_html__( 'Sifency Call to Action', 'sifency-addons' );
	}

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

	public function get_categories() {
		return [ 'sifency-addons-widgets' ];
	}

	public function get_style_depends() {
		return [ 'sifency-cta-style' ];
	}

	protected function register_controls() {

		// ==========================
		// LAYOUT & SKIN
		// ==========================
		$this->start_controls_section(
			'section_layout',
			[ 'label' => esc_html__( 'Layout & Skin', 'sifency-addons' ) ]
		);

		$this->add_control(
			'skin',
			[
				'label'   => esc_html__( 'Skin', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'classic',
				'options' => [
					'classic' => 'Classic (Side/Top)',
					'cover'   => 'Cover (Background)',
					'overlap' => 'Overlap (Floating)',
				],
				'prefix_class' => 'sifency-cta-skin-',
			]
		);

		$this->add_control(
			'image_position',
			[
				'label'   => esc_html__( 'Image Position', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'default' => 'right',
				'options' => [
					'left'  => [ 'title' => 'Left', 'icon' => 'eicon-h-align-left' ],
					'top'   => [ 'title' => 'Top', 'icon' => 'eicon-v-align-top' ],
					'right' => [ 'title' => 'Right', 'icon' => 'eicon-h-align-right' ],
				],
				'condition' => [ 'skin!' => 'cover' ],
				'prefix_class' => 'sifency-cta-pos-',
			]
		);

		$this->add_responsive_control(
			'content_height',
			[
				'label' => esc_html__( 'Min Height', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'range' => [ 'px' => [ 'min' => 100, 'max' => 800 ] ],
				'selectors' => [ '{{WRAPPER}} .sifency-cta-wrapper' => 'min-height: {{SIZE}}px;' ],
				'condition' => [ 'skin' => 'cover' ],
			]
		);

		$this->end_controls_section();

		// ==========================
		// CONTENT
		// ==========================
		$this->start_controls_section(
			'section_content',
			[ 'label' => esc_html__( 'Content', 'sifency-addons' ) ]
		);

		$this->add_control(
			'cta_image',
			[
				'label' => esc_html__( 'Image', 'sifency-addons' ),
				'type' => Controls_Manager::MEDIA,
				'default' => [ 'url' => \Elementor\Utils::get_placeholder_image_src() ],
			]
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			[
				'name' => 'cta_image_size',
				'default' => 'large',
				'condition' => [ 'skin!' => 'cover' ],
			]
		);

		$this->add_control( 'subtitle', [
			'label' => 'Subtitle',
			'type' => Controls_Manager::TEXT,
			'default' => 'Limited Time Offer',
		]);

		$this->add_control( 'title', [
			'label' => 'Title',
			'type' => Controls_Manager::TEXT,
			'default' => 'Get Started Today',
			'label_block' => true,
		]);

		$this->add_control( 'description', [
			'label' => 'Description',
			'type' => Controls_Manager::TEXTAREA,
			'default' => 'Grow your business with our modern solutions. Click below to learn more.',
		]);

		$this->add_control( 'button_text', [
			'label' => 'Button Text',
			'type' => Controls_Manager::TEXT,
			'default' => 'Click Here',
		]);

		$this->add_control( 'button_link', [
			'label' => 'Link',
			'type' => Controls_Manager::URL,
			'default' => [ 'url' => '#' ],
		]);

		// Ribbon
		$this->add_control( 'show_ribbon', [
			'label' => 'Show Ribbon',
			'type' => Controls_Manager::SWITCHER,
			'separator' => 'before',
		]);
		$this->add_control( 'ribbon_text', [
			'label' => 'Ribbon Text',
			'type' => Controls_Manager::TEXT,
			'default' => 'SALE',
			'condition' => [ 'show_ribbon' => 'yes' ],
		]);

		$this->end_controls_section();

		// ==========================
		// STYLE: BOX
		// ==========================
		$this->start_controls_section( 'style_box', [ 'label' => 'Box Style', 'tab' => Controls_Manager::TAB_STYLE ] );
		
		$this->add_group_control( Group_Control_Background::get_type(), [ 
			'name' => 'box_bg', 
			'selector' => '{{WRAPPER}} .sifency-cta-wrapper',
			'condition' => [ 'skin' => ['classic', 'overlap'] ]
		]);

		// Overlay for Cover Skin
		$this->add_control( 'overlay_color', [
			'label' => 'Overlay Color',
			'type' => Controls_Manager::COLOR,
			'default' => 'rgba(0,0,0,0.5)',
			'selectors' => [ '{{WRAPPER}} .sifency-cta-overlay' => 'background-color: {{VALUE}};' ],
			'condition' => [ 'skin' => 'cover' ]
		]);

		$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'box_border', 'selector' => '{{WRAPPER}} .sifency-cta-wrapper' ] );
		$this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'box_shadow', 'selector' => '{{WRAPPER}} .sifency-cta-wrapper' ] );
		$this->add_responsive_control( 'box_radius', [ 'label' => 'Border Radius', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .sifency-cta-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
		$this->add_responsive_control( 'box_padding', [ 'label' => 'Padding', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .sifency-cta-content-box' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
		
		// Hover Animation
		$this->add_control( 'hover_anim', [ 'label' => 'Hover Animation', 'type' => Controls_Manager::HOVER_ANIMATION, 'prefix_class' => 'elementor-animation-' ] );

		$this->end_controls_section();

		$this->add_sifency_advanced_text_style([
            'prefix'   => 'label_style',
            'label'    => 'Caption',
            'selector' => '.sifency-label-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'Heading',
            'selector' => '.sifency-heading-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Subtext',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);

		// ==========================
		// STYLE: BUTTON
		// ==========================
		$this->start_controls_section( 'style_button', [ 'label' => 'Button', 'tab' => Controls_Manager::TAB_STYLE ] );
		
		$this->start_controls_tabs( 'tabs_btn' );
		$this->start_controls_tab( 'tab_btn_normal', [ 'label' => 'Normal' ] );
		$this->add_control( 'btn_bg', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-cta-btn' => 'background-color: {{VALUE}};' ] ] );
		$this->add_control( 'btn_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-cta-btn' => 'color: {{VALUE}};' ] ] );
		$this->end_controls_tab();
		$this->start_controls_tab( 'tab_btn_hover', [ 'label' => 'Hover' ] );
		$this->add_control( 'btn_hover_bg', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-cta-btn:hover' => 'background-color: {{VALUE}};' ] ] );
		$this->add_control( 'btn_hover_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-cta-btn:hover' => 'color: {{VALUE}};' ] ] );
		$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->add_responsive_control( 'btn_radius', [ 'label' => 'Radius', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .sifency-cta-btn' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
		$this->add_responsive_control( 'btn_padding', [ 'label' => 'Padding', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .sifency-cta-btn' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );

		$this->end_controls_section();

		// ==========================
		// STYLE: RIBBON
		// ==========================
		$this->start_controls_section(
			'section_style_ribbon',
			[
				'label'     => esc_html__( 'Ribbon', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ 'show_ribbon' => 'yes' ],
			]
		);

		$this->add_control(
			'ribbon_bg_color',
			[
				'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#ff6b6b',
				'selectors' => [
					'{{WRAPPER}} .sifency-cta-ribbon' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'ribbon_text_color',
			[
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#ffffff',
				'selectors' => [
					'{{WRAPPER}} .sifency-cta-ribbon' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'ribbon_typography',
				'selector' => '{{WRAPPER}} .sifency-cta-ribbon',
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name'     => 'ribbon_box_shadow',
				'selector' => '{{WRAPPER}} .sifency-cta-ribbon',
			]
		);

		$this->add_control(
			'ribbon_position_heading',
			[
				'label'     => esc_html__( 'Positioning', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);

		$this->add_responsive_control(
			'ribbon_distance_top',
			[
				'label'      => esc_html__( 'Distance from Top', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'em' ],
				'range'      => [
					'px' => [ 'min' => 0, 'max' => 100 ],
				],
				'selectors'  => [
					'{{WRAPPER}} .sifency-cta-ribbon' => 'top: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_responsive_control(
			'ribbon_distance_right',
			[
				'label'      => esc_html__( 'Distance from Right', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'em' ],
				'range'      => [
					'px' => [ 'min' => -50, 'max' => 50 ],
				],
				'selectors'  => [
					'{{WRAPPER}} .sifency-cta-ribbon' => 'right: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_responsive_control(
			'ribbon_padding',
			[
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em' ],
				'default'    => [
					'top'    => 5,
					'bottom' => 5,
					'left'   => 30,
					'right'  => 30,
					'unit'   => 'px',
					'isLinked' => false,
				],
				'selectors'  => [
					'{{WRAPPER}} .sifency-cta-ribbon' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$skin = $settings['skin'];

		// Render Logic
		?>
		<div class="sifency-cta-wrapper">
			
			<!-- Image Section -->
			<div class="sifency-cta-image-box">
				<?php 
				if ( 'cover' === $skin ) {
					// Cover: Background Image logic
					echo '<div class="sifency-cta-bg-cover" style="background-image: url(' . esc_url( $settings['cta_image']['url'] ) . ')"></div>';
					echo '<div class="sifency-cta-overlay"></div>';
				} else {
					echo wp_kses_post(
						\Elementor\Group_Control_Image_Size::get_attachment_image_html(
							$settings,
							'cta_image_size',
							'cta_image'
						)
					);
				}
				?>
			</div>

			<!-- Ribbon -->
			<?php if ( 'yes' === $settings['show_ribbon'] ) : ?>
				<div class="sifency-cta-ribbon"><span><?php echo esc_html( $settings['ribbon_text'] ); ?></span></div>
			<?php endif; ?>

			<!-- Content Section -->
			<div class="sifency-cta-content-box">
				<?php if ( $settings['subtitle'] ) : ?>
					<span class="sifency-cta-subtitle sifency-label-style"><?php echo esc_html( $settings['subtitle'] ); ?></span>
				<?php endif; ?>

				<?php if ( $settings['title'] ) : ?>
					<h3 class="sifency-cta-title sifency-heading-style"><?php echo esc_html( $settings['title'] ); ?></h3>
				<?php endif; ?>

				<?php if ( $settings['description'] ) : ?>
					<p class="sifency-cta-desc sifency-content-style"><?php echo esc_html( $settings['description'] ); ?></p>
				<?php endif; ?>

				<?php if ( $settings['button_text'] ) : ?>
					<div class="sifency-cta-btn-wrap">
						<a href="<?php echo esc_url( $settings['button_link']['url'] ); ?>" class="sifency-cta-btn sifency-button-style">
							<?php echo esc_html( $settings['button_text'] ); ?>
						</a>
					</div>
				<?php endif; ?>
			</div>

		</div>
		<?php
	}
}