<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Image_Size;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Css_Filter;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Widget_Service_Icon_Image extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Help_Controls;

	protected function get_allowed_template_types() {
		return 'service';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-service-icon-image',
			'title'         => 'Service Image',
			'icon'          => 'eicon-image',
			'category'      => array( 'sifency-service-components' ),
			'keywords'      => array( 'service', 'icon', 'image', 'media', 'meta', 'sifency' ),
			'style_depends' => array( 'sifency-typo-style', 'sifency-theme-builder-service-style' ),
		);}
	protected function register_controls() {
		$this->start_controls_section( 'section_image_content', array( 'label' => esc_html__( 'Image', 'sifency-addons' ) ) );
		$this->add_control(
			'placeholder_image',
			array(
				'label'       => 'Placeholder Image',
				'type'        => Controls_Manager::MEDIA,
				'description' => 'This image will be shown in the editor if no icon image is set.',
			)
		);
		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'    => 'image_size',
				'default' => 'thumbnail',
			)
		);
		$this->add_control(
			'link',
			array(
				'label'       => 'Link',
				'type'        => Controls_Manager::URL,
				'dynamic'     => array( 'active' => true ),
				'placeholder' => 'https://your-link.com',
			)
		);
		$this->add_responsive_control(
			'align',
			array(
				'label'        => 'Alignment',
				'type'         => Controls_Manager::CHOOSE,
				'options'      => array(
					'left'   => array(
						'title' => 'Left',
						'icon'  => 'eicon-h-align-left',
					),
					'center' => array(
						'title' => 'Center',
						'icon'  => 'eicon-h-align-center',
					),
					'right'  => array(
						'title' => 'Right',
						'icon'  => 'eicon-h-align-right',
					),
				),
				'prefix_class' => 'elementor-align%s-',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_image_style_controls(
			array(
				'selector'  => '.sifency-icon-image-wrapper img',
				'selectors' => array(
					'image'     => '.sifency-icon-image-wrapper img',
					'container' => '.sifency-icon-image-wrapper img',
					'dimesions' => '.sifency-icon-image-wrapper img',
				),
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();
		$image_url  = '';

		$image_id = sifency_get_field( 'service_icon_image' );

		if ( ! empty( $image_id ) ) {
			$image_url = \Elementor\Group_Control_Image_Size::get_attachment_image_src( $image_id, 'image_size', $settings ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally.
		} elseif ( \Elementor\Plugin::$instance->editor->is_edit_mode() && ! empty( $settings['placeholder_image']['url'] ) ) {
			$image_url = $settings['placeholder_image']['url'];
		}

		if ( empty( $image_url ) ) {
			return;
		}

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array() );

		$tag = 'span';
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-icon-image-wrapper' );
		if ( ! empty( $settings['link']['url'] ) ) {
			$tag = 'a';
			$this->add_link_attributes( 'wrapper', $settings['link'] );
		}

		?>
		<div id="<?php echo esc_attr ( $wrapper_id ); ?>" <?php echo wp_kses_post ( $animation_attrs ); // phpcs:ignore ?>>
			<<?php echo tag_escape( $tag ); ?> <?php echo wp_kses( $this->get_render_attribute_string( 'wrapper' ), array() ); ?>>
				<img class="sifency-icon-image" src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( get_the_title() ); ?>" />
			</<?php echo tag_escape( $tag ); ?>>
		</div>
		<?php
	}
}
