<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use Elementor\Widget_Base;
use Elementor\Group_Control_Typography;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Image_Size;
use Elementor\Group_Control_Css_Filter;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Border;
use Elementor\Utils;
use Sifency\Traits\Sifency_Image_Style_Controls;
use Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Image_Controls;
use Sifency\Traits\Sifency_Core_Text_Style;

class Sifency_Widget_Image extends Widget_Base {
	use Sifency_Animation_Controls;
	use Sifency_Image_Style_Controls;
	use Sifency_Image_Controls;
	use Sifency_Core_Text_Style;

	public function get_name() {
		return 'sifency-addons-image';
	}
	public function get_title() {
		return esc_html__( 'Sifency Image', 'sifency-addons' );
	}

	public function get_icon() {
		return 'eicon-image';
	}

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

	public function get_keywords() {
		return [ 'image', 'photo', 'media', 'animation', 'parallax', 'hover', 'effect' ];
	}

    public function get_style_depends() {
	    return [ 'sifency-typo-style' , 'sifency-frontend-style' ];
    }

    public function get_script_depends() {
        return [ 'sifency-frontend-script' ];
    }

	protected function register_controls() {
		$this->add_sifency_image_controls();
		$this->add_sifency_image_style_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'caption_style',
            'label'    => 'Caption Style',
            'selector' => '.sifency-caption-style',
			'target_children' => false, 
        ]);
		$this->add_sifency_animation_controls();
		$this->update_control(
            'sifency_animation_target',
            [
                'type' => Controls_Manager::HIDDEN,
                'default' => 'wrapper',
            ]
        );
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		if ( empty( $settings['image']['url'] ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				printf(
					'<div class="sifency-image-wrapper"><img src="%s" alt="" /></div>',
					esc_url( \Elementor\Utils::get_placeholder_image_src() )
				);
			}
			return;
		}
		$animation_manager = new Sifency_Animation_Manager($settings);
		$caption_text = '';
		if ( 'attachment' === $settings['caption_source'] ) {
			$caption_text = wp_get_attachment_caption( $settings['image']['id'] );
		} elseif ( 'custom' === $settings['caption_source'] ) {
			$caption_text = $settings['caption'];
		}
		$has_caption = ! empty( $caption_text );

		$image_html = \Elementor\Group_Control_Image_Size::get_attachment_image_html( $settings, 'image', 'image' );

		$container_classes = [ 'sifency-image-container' ];
		if ( ! empty( $settings['image_hover_animation'] ) ) {
			$container_classes[] = 'elementor-animation-' . $settings['image_hover_animation'];
		}
		$this->add_render_attribute( 'link_tag', 'class', $container_classes );

		$has_link = false;
		if ( 'custom' === $settings['link_to'] && ! empty( $settings['link']['url'] ) ) {
			$this->add_link_attributes( 'link_tag', $settings['link'] );
			$has_link = true;
		} elseif ( 'file' === $settings['link_to'] && ! empty( $settings['image']['url'] ) ) {
			$this->add_render_attribute( 'link_tag', 'href', esc_url( $settings['image']['url'] ) );
			$this->add_render_attribute( 'link_tag', 'data-elementor-open-lightbox', 'yes' );
			$has_link = true;
		}
		$link_tag = $has_link ? 'a' : 'div';
		$html_tag = $has_caption ? 'figure' : 'div';
		$wrapper_classes = ['sifency-image-wrapper'];
		$wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
		?>
		<<?php echo tag_escape( $html_tag ); ?> <?php
			
			echo wp_kses_post( $wrapper_attrs ); ?>>

			<<?php echo tag_escape( $link_tag ); ?> <?php $this->print_render_attribute_string( 'link_tag' ); ?>>
				<div class="sifency-image">
					<?php echo wp_kses_post( $image_html ); ?>
				</div>
				<div class="sifency-image-overlay"></div>
			</<?php echo tag_escape( $link_tag ); ?>>

			<?php if ( $has_caption ) : ?>
				<figcaption class="sifency-image-caption sifency-caption-style">
					<?php echo wp_kses_post( $caption_text ); ?>
				</figcaption>
			<?php endif; ?>

		</<?php echo tag_escape( $html_tag ); ?>>
		<?php
	}
}