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

use Elementor\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_Animation_Controls; 
use Sifency\Traits\Sifency_Animation_Manager;  

class Sifency_Widget_Service_Icon_Image extends Widget_Base {
	use Sifency_Animation_Controls;

	public function get_name() { return 'sifency-service-icon-image'; }
	public function get_title() { return esc_html__( 'Service Icon Image', 'sifency-addons' ); }
	public function get_icon() { return 'eicon-image'; }
	public function get_categories() { return [ 'sifency-service-components' ]; }
	public function get_keywords() { return [ 'service', 'icon', 'image', 'media', 'meta', 'sifency' ]; }
	public function get_style_depends() {
	    return [ 'sifency-typo-style', 'sifency-theme-builder-service-style' ];
    }
	protected function register_controls() {
		// --- Content Tab: Image Settings ---
		$this->start_controls_section( 'section_image_content', [ 'label' => esc_html__( 'Image', 'sifency-addons' ) ] );
		$this->add_control( 'placeholder_image', [ '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(), [ 'name' => 'image_size', 'default' => 'thumbnail' ] );
		$this->add_control( 'link', [ 'label' => 'Link', 'type' => Controls_Manager::URL, 'dynamic' => [ 'active' => true ], 'placeholder' => 'https://your-link.com' ] );
		$this->add_responsive_control( 'align', [ 'label' => 'Alignment', 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => ['title' => 'Left', 'icon' => 'eicon-h-align-left'], 'center' => ['title' => 'Center', 'icon' => 'eicon-h-align-center'], 'right' => ['title' => 'Right', 'icon' => 'eicon-h-align-right'] ], 'prefix_class' => 'elementor-align%s-' ] );
		$this->end_controls_section();

		// --- Style Tab: Image Styling ---
		$this->start_controls_section( 'section_image_style', [ 'label' => esc_html__( 'Image', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ] );
		$this->add_responsive_control( 'width', [ 'label' => 'Width', 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px', '%' ], 'range' => [ 'px' => ['min' => 20, 'max' => 500], '%' => ['min' => 10, 'max' => 100] ], 'selectors' => [ '{{WRAPPER}} .sifency-icon-image' => 'width: {{SIZE}}{{UNIT}};' ] ] );
		$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'image_border', 'selector' => '{{WRAPPER}} .sifency-icon-image' ] );
		$this->add_responsive_control( 'image_border_radius', [ 'label' => 'Border Radius', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .sifency-icon-image' => 'border-radius: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' ] ] );
		$this->start_controls_tabs( 'image_effects' );
		$this->start_controls_tab('image_normal', ['label' => 'Normal']);
		$this->add_control( 'opacity', [ 'label' => 'Opacity', 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 1, 'step' => 0.05 ] ], 'selectors' => [ '{{WRAPPER}} .sifency-icon-image' => 'opacity: {{SIZE}};' ] ] );
		$this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'css_filters', 'selector' => '{{WRAPPER}} .sifency-icon-image' ] );
		$this->end_controls_tab();
		$this->start_controls_tab('image_hover', ['label' => 'Hover']);
		$this->add_control( 'opacity_hover', [ 'label' => 'Opacity', 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 1, 'step' => 0.05 ] ], 'selectors' => [ '{{WRAPPER}} .sifency-icon-image-wrapper:hover .sifency-icon-image' => 'opacity: {{SIZE}};' ] ] );
		$this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'css_filters_hover', 'selector' => '{{WRAPPER}} .sifency-icon-image-wrapper:hover .sifency-icon-image' ] );
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();
		$this->add_sifency_animation_controls();
	}

	protected function render() {
		$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 );
		} 
		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_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( [] );

		$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 <?php echo wp_kses_post ( $animation_attrs ); // phpcs:ignore ?>>
			<<?php echo tag_escape( $tag ); ?> <?php echo wp_kses ( $this->get_render_attribute_string( 'wrapper' ), [] ); ?>>
				<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
	}
}