<?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_Typography;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Label_Content_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Widget_Service_Features extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Label_Content_Controls;
	use Sifency_Addons_Master_Typography;
	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-features',
			'title'         => 'Service Features',
			'icon'          => 'eicon-bullet-list',
			'category'      => array( 'sifency-service-components' ),
			'keywords'      => array( 'service', 'features', 'list', 'repeater', 'meta', 'dynamic', 'sifency' ),
			'style_depends' => array( 'sifency-typo-style', 'sifency-theme-builder-service-style' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_label_content_controls();

		$this->update_control( 'label_text', array( 'default' => esc_html__( 'Key Features', 'sifency-addons' ) ) );
		$this->update_control( 'label_spacing', array( 'selectors' => array( '{{WRAPPER}} .sifency-features-title' => 'margin-bottom: {{SIZE}}{{UNIT}};' ) ) );
		$this->update_control( 'section_label_style', array( 'label' => esc_html__( 'Title', 'sifency-addons' ) ) );

		$this->start_controls_section(
			'section_list_content',
			array( 'label' => esc_html__( 'List Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'icon',
			array(
				'label' => esc_html__( 'List Icon', 'sifency-addons' ),
				'type'  => Controls_Manager::ICONS,
			)
		);

		$this->add_responsive_control(
			'align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-features-list' => 'align-items: {{VALUE}};' ),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_list_style',
			array(
				'label' => esc_html__( 'List Items', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'item_spacing',
			array(
				'label'     => esc_html__( 'Spacing Between', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .sifency-features-list' => 'gap: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'selector'   => '.sifency-label-style',
				'label'      => esc_html__( 'Label', 'sifency-addons' ),
				'condition'  => array( 'show_label' => 'yes' ),
				'color_only' => true,
				'exclude'    => array( 'shadow', 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'feature_text_',
				'selector'   => '.sifency-feature-item-text',
				'label'      => esc_html__( 'Feature Text', 'sifency-addons' ),
				'color_only' => true,
				'exclude'    => array( 'shadow', 'tabs', 'transition', 'alignment' ),
			)
		);

		$this->add_sifency_addons_icon_style_controls(
			array(
				'selector'        => '.sifency-feature-icon',
				'show_dimensions' => true,
				'condition'       => array( 'icon[value]!' => '' ),
			)
		);
		$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();
		$features   = sifency_get_field( 'service_features' );

		if ( empty( $features ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				$features = array(
					array( 'feature_text' => 'High-Quality Design' ),
					array( 'feature_text' => '24/7 Premium Support' ),
					array( 'feature_text' => 'Responsive on All Devices' ),
				);
			} else {
				return;

			}
		}

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array() );

		?>
		<div id="<?php echo esc_attr ( $wrapper_id ); ?>" class="sifency-features-wrapper" <?php echo $animation_attrs; // phpcs:ignore ?>>
			<?php if ( 'yes' === $settings['show_label'] && ! empty( $settings['label_text'] ) ) : ?>
				<h3 class="sifency-features-title sifency-label-style"><?php echo esc_html( $settings['label_text'] ); ?></h3>
			<?php endif; ?>

			<ul class="sifency-features-list">
				<?php
				foreach ( $features as $item ) :
					if ( empty( $item['feature_text'] ) ) {
						continue;
					}
					?>
					<li class="sifency-feature-item">
						<?php if ( ! empty( $settings['icon']['value'] ) ) : ?>
							<span class="sifency-feature-icon"><?php Icons_Manager::render_icon( $settings['icon'], array( 'aria-hidden' => 'true' ) ); ?></span>
						<?php endif; ?>
						<span class="sifency-feature-item-text"><?php echo esc_html( $item['feature_text'] ); ?></span>
					</li>
				<?php endforeach; ?>
			</ul>
		</div>
		<?php
	}
}
