<?php

if ( ! defined( 'ABSPATH' ) )
	exit; // Exit if accessed directly

use Elementor\Widget_Base;
use Elementor\Icons_Manager;

class Widget_List_Group extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
		return 'banae_list_group';
	}

	/**
	 * Get widget title.
	 *
	 * @return string Widget title.
	 */
	public function get_title() {
		return __( 'List Group', 'banana-addons-for-elementor' );
	}

	/**
	 * Get widget icon.
	 *
	 * @return string Widget icon.
	 */
	public function get_icon() {
		return 'eicon-editor-list-ul';
	}

	/**
	 * Get widget categories.
	 *
	 * @return array Widget categories.
	 */
	public function get_categories() {
		return [ 'banana-addons-category' ];
	}

	/**
	 * Get widget keywords for search
	 * 
	 * @return string[]
	 */
	public function get_keywords() {
		return [ 'info', 'box', 'list', 'group' ];
	}

	/**
	 * Enqueue dependend styles
	 * 
	 * Retrieve the list of style dependencies the widget requires.
	 * 
	 * @return string[]
	 */
	public function get_style_depends() {
		return [ 'banae-list-group-style' ];
	}

	protected function is_dynamic_content(): bool {
		return false;
	}

	/**
	 * Register widget content controls
	 */
	protected function register_controls() {

		// Instantiate the control class
		require_once 'register-controls.php';

		// Call to the register controls method
		Banae_List_Group_Controls::add_register_controls( $this );

	}

	protected function render() {
		$settings = $this->get_settings_for_display();

		if ( empty( $settings['list_item'] ) ) {
			return;
		}

		// Enable Hover Direction animation
		$item_wrap_class = "";
		if ( $settings['direction_animation'] == 'yes' ) {
			$item_wrap_class = "banae-list-item-custom";
		}
		?>
		<div class="banae-list-group">
			<ul class="banae-list-wrap">
				<?php foreach ( $settings['list_item'] as $index => $item ) :
					// title
					$title_key = $this->get_repeater_setting_key( 'title', 'list_item', $index );
					//$this->add_inline_editing_attributes( $title_key, 'basic' );
					$this->add_render_attribute( $title_key, 'class', 'banae-list-title' );

					// description
					$description_key = $this->get_repeater_setting_key( 'description', 'list_item', $index );
					//$this->add_inline_editing_attributes( $description_key, 'basic' );
					$this->add_render_attribute( $description_key, 'class', 'banae-list-detail' );

					// badge
					$badge_key = $this->get_repeater_setting_key( 'badge_text', 'list_item', $index );
					//$this->add_inline_editing_attributes( $badge_key, 'basic' );
					$this->add_render_attribute( $badge_key, 'class', 'banae-badge' );

					// link
					if ( $item['link']['url'] ) {
						$link_key = $this->get_repeater_setting_key( 'link', 'list_item', $index );
						$this->add_render_attribute( $link_key, 'class', 'banae-link' );
						$this->add_link_attributes( $link_key, $item['link'] );
					}

					?>

					<li class="banae-list-item <?= $item_wrap_class; ?> elementor-repeater-item-<?php echo $item['_id']; ?>">

						<?php if ( ! empty( $item['link']['url'] ) ) : ?>

							<a <?php $this->print_render_attribute_string( $link_key ); ?>>
							<?php endif; ?>

							<div class="banae-item-wrap">

								<?php if ( ! empty( $item['icon']['value'] ) ) : ?>
									<div class="banae-icon icon">
										<?php Icons_Manager::render_icon( $item['icon'], [ 'aria-hidden' => 'true' ] ); ?>
									</div>

								<?php elseif ( $item['number'] ) : ?>
									<div class="banae-icon number">
										<span><?php echo esc_html( $item['number'] ); ?></span>
									</div>

								<?php elseif ( $item['image'] ) :

									$images = wp_get_attachment_image_src( $item['image']['id'], 'thumbnail', false );
									if ( $images ) {
										$image = $images[0];
									} else {
										$image = $item['image']['url'];
									}
									?>
									<div class="banae-icon image">
										<img src="<?php echo esc_url( $image ); ?>" alt="<?php echo esc_attr( $item['title'] ); ?>" />
									</div>

									<?php
								endif;
								?>

								<div class="banae-text">
									<!-- title tag start -->
									<<?php echo esc_attr( $settings['title_tag'], 'h4' ) . ' ' . $this->get_render_attribute_string( $title_key ); ?>>
										<?php echo esc_html( $item['title'] ); ?>
										<?php if ( $item['badge_text'] ) : ?>
											<span <?php echo $this->get_render_attribute_string( $badge_key ); ?>><?php echo esc_html( $item['badge_text'] ); ?></span>
										<?php endif; ?>
									</<?php echo esc_attr( $settings['title_tag'], 'h2' ); ?>>
									<!-- title tag end -->

									<?php if ( $item['description'] ) : ?>
										<p <?php $this->print_render_attribute_string( $description_key ); ?>>
											<?php echo esc_html( $item['description'] ); ?>
										</p>
									<?php endif; ?>
								</div>

								<?php if ( $item['direction']['value'] ) : ?>
									<div class="banae-direction">
										<?php Icons_Manager::render_icon( $item['direction'], [ 'aria-hidden' => 'true' ] ); ?>
									</div>
								<?php endif; ?>

							</div>

							<?php if ( ! empty( $item['link']['url'] ) ) : ?>
							</a>
						<?php endif; ?>

					</li>

				<?php endforeach; ?>
			</ul>
		</div>
		<?php
	}
}