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

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Sifency\Traits\Sifency_Animation_Controls; 
use Sifency\Traits\Sifency_Animation_Manager;  

class Sifency_Widget_Service_Icon extends Widget_Base {
	use Sifency_Animation_Controls;

	public function get_name() { return 'sifency-service-icon'; }
	public function get_title() { return esc_html__( 'Service Icon', 'sifency-addons' ); }
	public function get_icon() { return 'eicon-font'; }
	public function get_categories() { return [ 'sifency-service-components' ]; }
	public function get_keywords() { return [ 'service', 'icon', 'font', 'meta', 'dynamic', 'sifency' ]; }
	public function get_style_depends() {
	    return [ 'sifency-typo-style', 'sifency-theme-builder-service-style' ];
    }
	protected function register_controls() {
		// --- Content Tab: Icon Settings ---
		$this->start_controls_section( 'section_icon_content', [ 'label' => esc_html__( 'Icon', 'sifency-addons' ) ] );
		$this->add_control( 'placeholder_icon_class', [ 'label' => 'Placeholder Icon Class', 'type' => Controls_Manager::TEXT, 'default' => 'fas fa-star', 'description' => 'This icon class will be shown in the editor if the dynamic field is empty.' ] );
		$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: Icon Styling ---
		$this->start_controls_section( 'section_icon_style', [ 'label' => esc_html__( 'Icon', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ] );
		$this->add_responsive_control( 'icon_size', [ 'label' => 'Size', 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-icon-box i' => 'font-size: {{SIZE}}px;' ] ] );
		$this->add_responsive_control( 'icon_padding', [ 'label' => 'Padding', 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-icon-box' => 'padding: {{SIZE}}px;' ] ] );
		$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'icon_border', 'selector' => '{{WRAPPER}} .sifency-icon-box' ] );
		$this->add_responsive_control( 'icon_border_radius', [ 'label' => 'Border Radius', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .sifency-icon-box' => 'border-radius: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' ] ] );
		$this->start_controls_tabs( 'icon_colors' );
		$this->start_controls_tab('icon_color_normal', ['label' => 'Normal']);
		$this->add_control( 'icon_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-icon-box i' => 'color: {{VALUE}};' ] ] );
		$this->add_control( 'icon_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-icon-box' => 'background-color: {{VALUE}};' ] ] );
		$this->end_controls_tab();
		$this->start_controls_tab('icon_color_hover', ['label' => 'Hover']);
		$this->add_control( 'icon_hover_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-icon-box:hover i' => 'color: {{VALUE}};' ] ] );
		$this->add_control( 'icon_hover_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-icon-box:hover' => 'background-color: {{VALUE}};' ] ] );
		$this->add_control( 'icon_hover_border_color', [ 'label' => 'Border Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-icon-box:hover' => 'border-color: {{VALUE}};' ] ] );
		$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();
		$icon_class = sifency_get_field( 'service_icon' );

		if ( empty( $icon_class ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				$icon_class = $settings['placeholder_icon_class'];
			} else { return; }
		}
		if ( empty( $icon_class ) ) return;

		$animation_manager = new Sifency_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( [] );

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

		?>
		<div <?php echo wp_kes_post ( $animation_attrs ); // phpcs:ignore ?>>
			<<?php echo tag_escape( $tag ); ?> <?php echo wp_kses ( $this->get_render_attribute_string( 'icon_wrapper' ), [] ); ?>>
				<i class="<?php echo esc_attr( $icon_class ); ?>" aria-hidden="true"></i>
			</<?php echo tag_escape( $tag ); ?>>
		</div>
		<?php
	}
}