<?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_Border;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Widget_Service_Icon extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Icon_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',
			'title'         => 'Service Icon',
			'icon'          => 'eicon-font',
			'category'      => array( 'sifency-service-components' ),
			'keywords'      => array( 'service', 'icon', 'font', 'meta', 'dynamic', 'sifency' ),
			'style_depends' => array( 'sifency-typo-style', 'sifency-theme-builder-service-style' ),
		);}
	protected function register_controls() {
		$this->start_controls_section( 'section_icon_content', array( 'label' => esc_html__( 'Icon', 'sifency-addons' ) ) );
		$this->add_control(
			'placeholder_icon_class',
			array(
				'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',
			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_icon_style_controls(
			array(
				'selector'        => '.sifency-icon-box',
				'show_dimensions' => true,
			)
		);
		$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();
		$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_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array() );

		$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 id="<?php echo esc_attr ( $wrapper_id ); ?>" <?php echo  $animation_attrs; // phpcs:ignore ?>>
			<<?php echo tag_escape( $tag ); ?> <?php echo wp_kses( $this->get_render_attribute_string( 'icon_wrapper' ), array() ); ?>>
				<i class="<?php echo esc_attr( $icon_class ); ?>" aria-hidden="true"></i>
			</<?php echo tag_escape( $tag ); ?>>
		</div>
		<?php
	}
}
