<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
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_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Image_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Image extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Image_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-addons-image',
			'title'          => 'Sifency Image',
			'icon'           => 'eicon-image',
			'category'       => 'sifency-addons-widgets',
			'keywords'       => array( 'image', 'photo', 'picture', 'visual' ),
			'style_depends'  => array( 'sifency-typo-style', 'sifency-frontend-style' ),
			'script_depends' => array( 'sifency-frontend-script' ),
		);}
	protected function register_controls() {
		$this->add_sifency_image_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_image_style_controls(
			array(
				'selector'  => '.sifency-image-container',
				'selectors' => array(
					'image'      => '.sifency-image-container img',
					'container'  => '.sifency-image-container',
					'dimensions' => '.sifency-image-container img',
				),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'caption_style',
				'label'      => 'Caption Style',
				'selector'   => '.sifency-caption-style',
				'condition'  => array( 'caption!' => '' ),
				'color_only' => true,
				'exclude'    => array( 'tabs', 'transition', 'radius', 'bg', 'shadow' ),
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'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_Addons_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 = array( '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 = array( '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' ); ?>>
					<?php echo wp_kses_post( $image_html ); ?>
				<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
	}
}
