<?php
namespace SabaiApps\Directories\Component\WordPressContent\Elementor;

use Elementor\Controls_Manager;
use Elementor\Core\DynamicTags\Data_Tag;
use ElementorPro\Modules\DynamicTags\Module;

class ImageDynamicTag extends Data_Tag {

	use DynamicTagTrait;

	protected $_dynamicTagFieldTypes = array( 'wp_image' );

	public function get_name() {
		return 'drts-image';
	}

	public function get_title() {
		return 'Directories' . ' - ' . __( 'Image Field', 'directories-pro' );
	}

	public function get_categories() {
		return array(
			Module::IMAGE_CATEGORY,
		);
	}

	public function get_value( array $options = array() ) {
		if ( ( ! $field_key = $this->get_settings( 'field' ) )
			|| ( ! $entity = $this->_getEntity() )
			|| ( ! $field = $this->_getDynamicTagField( $entity, $field_key ) )
			|| ( ! $value = $entity->getSingleFieldValue( $field->getFieldName() ) )
			|| ( ! $url = wp_get_attachment_image_src( $value['attachment_id'], 'large' ) )
		) {
			return $this->get_settings( 'fallback' );
		}

		return array(
			'id'  => $value['attachment_id'],
			'url' => $url,
		);
	}

	protected function register_controls() {
		$this->add_control(
			'field',
			array(
				'label'  => __( 'Field name', 'directories-pro' ),
				'type'   => Controls_Manager::SELECT,
				'groups' => $this->_getDynamicTagFields( $this->_dynamicTagFieldTypes ),
			)
		);
		$this->add_control(
			'fallback',
			array(
				'label' => __( 'Fallback image', 'directories-pro' ),
				'type'  => Controls_Manager::MEDIA,
			)
		);
	}
}
