<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Repeater;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Icon_List_Icon_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Iconlist extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Icon_List_Icon_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-addons-icon-list',
			'title'         => 'Sifency Icon List',
			'icon'          => 'eicon-bullet-list',
			'category'      => 'sifency-addons-widgets',
			'keywords'      => array( 'icon', 'list', 'items', 'features' ),
			'style_depends' => array( 'sifency-typo-style', 'sifency-frontend-style', 'sifency-component-style', 'sifency-icon' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Icon List', 'sifency-addons' ) )
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'text',
			array(
				'label'       => esc_html__( 'Text', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXTAREA,
				'default'     => esc_html__( 'List Item', 'sifency-addons' ),
				'label_block' => true,
			)
		);
		$repeater->add_control(
			'icon',
			array(
				'label'   => esc_html__( 'Icon', 'sifency-addons' ),
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'icon-check',
					'library' => 'sifency-icon',
				),
			)
		);
		$repeater->add_control(
			'link',
			array(
				'label'   => esc_html__( 'Link', 'sifency-addons' ),
				'type'    => Controls_Manager::URL,
				'default' => array( 'is_external' => 'true' ),
			)
		);
		$this->add_control(
			'icon_list',
			array(
				'label'       => esc_html__( 'List Items', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array( 'text' => esc_html__( 'List Item #1', 'sifency-addons' ) ),
					array( 'text' => esc_html__( 'List Item #2', 'sifency-addons' ) ),
				),
				'title_field' => '{{{ text }}}',
			)
		);
		$this->add_control(
			'layout',
			array(
				'label'        => esc_html__( 'Layout', 'sifency-addons' ),
				'type'         => Controls_Manager::CHOOSE,
				'options'      => array(
					'traditional' => array(
						'title' => 'Default',
						'icon'  => 'eicon-editor-list-ul',
					),
					'inline'      => array(
						'title' => 'Inline',
						'icon'  => 'eicon-ellipsis-h',
					),
				),
				'default'      => 'traditional',
				'prefix_class' => 'elementor-icon-list--layout-',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_style_list',
			array(
				'label' => esc_html__( 'List', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'space_between',
			array(
				'label'     => esc_html__( 'Space Between', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array( 'max' => 50 ),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-icon-list-items' => 'display: flex; gap: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-icon-list-items:not(.elementor-inline-items) .sifency-icon-list-item:not(:last-child)' => 'padding-bottom: calc({{SIZE}}{{UNIT}}/2)',
					'{{WRAPPER}} .sifency-icon-list-items:not(.elementor-inline-items) .sifency-icon-list-item:not(:first-child)' => 'margin-top: calc({{SIZE}}{{UNIT}}/2)',
					'{{WRAPPER}} .sifency-icon-list-items.elementor-inline-items .sifency-icon-list-item' => 'margin-right: calc({{SIZE}}{{UNIT}}/2); margin-left: calc({{SIZE}}{{UNIT}}/2)',
					'{{WRAPPER}} .sifency-icon-list-items.elementor-inline-items' => 'margin-right: calc(-{{SIZE}}{{UNIT}}/2); margin-left: calc(-{{SIZE}}{{UNIT}}/2)',
				),
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'    => 'flex_',
				'label'     => esc_html__( 'Item Alignment', 'sifency-addons' ),
				'selector'  => '.sifency-icon-list-item',
				'condition' => array(),
			)
		);
		$this->end_controls_section();

		$this->add_sifency_addons_item_style_controls(
			array(
				'label'    => esc_html__( 'Icon List Item Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-icon-list-item',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'content_style',
				'label'    => 'Icon List Content',
				'selector' => '.sifency-content-style',
				'exclude'  => array( 'alignment' ),
			)
		);

		$this->add_sifency_icon_list_icon_controls();

		$this->add_Sifency_Addons_Animation_Controls();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		if ( empty( $settings['icon_list'] ) ) {
			return;
		}
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$wrapper_classes = array( 'sifency-icon-list-items' );
		if ( 'inline' === $settings['layout'] ) {
			$wrapper_classes[] = 'elementor-inline-items';
		}
		$wrapper_attrs = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<ul <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php
			foreach ( $settings['icon_list'] as $index => $item ) :
				$item_classes = array(
					'sifency-icon-list-item',
					'elementor-repeater-item-' . $item['_id'],
				);

				$item_attrs = $animation_manager->get_item_attributes( $item_classes );

				$link_key = 'link_' . $item['_id'];
				$link_url = $item['link']['url'] ?? '';

				if ( ! empty( $link_url ) ) {
					$this->add_link_attributes( $link_key, $item['link'] );
				}
				?>
				<li <?php echo wp_kses_post( $item_attrs ); ?>>
					<a <?php echo wp_kses_post( $this->get_render_attribute_string( $link_key ) ); ?>>
						<?php if ( ! empty( $item['icon']['value'] ) ) : ?>
							<span class="sifency-icon-list-icon sifency-icon-style">
								<?php wp_kses_post( \Elementor\Icons_Manager::render_icon( $item['icon'], array( 'aria-hidden' => 'true' ) ) ); ?>
							</span>
						<?php endif; ?>
						<span class="sifency-icon-list-text sifency-content-style">
							<?php echo esc_html( $item['text'] ); ?>
						</span>
					</a>
				</li>
			<?php endforeach; ?>
		</ul>
		<?php
	}
}
