<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Image_Size;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Addons_Widget_Feature_Showcase extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Image_Style_Controls;
	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-feature-showcase',
			'title'         => 'Sifency Feature Showcase',
			'icon'          => 'eicon-product-related',
			'category'      => 'sifency-business-widgets',
			'keywords'      => array( 'feature', 'showcase', 'item', 'grid' ),
			'style_depends' => array( 'sifency-item-showcase-grid-style', 'sifency-icon' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_main_image',
			array(
				'label' => esc_html__( 'Central Image', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'standard',
				'options' => array(
					'standard'  => esc_html__( '1. Standard (Icon Inside)', 'sifency-addons' ),
					'connected' => esc_html__( '2. Connected (Lines)', 'sifency-addons' ),
					'cards'     => esc_html__( '3. Modern Cards', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'image',
			array(
				'label'   => esc_html__( 'Product Image', 'sifency-addons' ),
				'type'    => Controls_Manager::MEDIA,
				'default' => array(
					'url' => \Elementor\Utils::get_placeholder_image_src(),
				),
			)
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'    => 'image',
				'default' => 'large',
			)
		);
		$this->add_control(
			'image_vertical_align',
			array(
				'label'     => esc_html__( 'Vertical Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'flex-start' => 'Top',
					'center'     => 'Middle',
					'flex-end'   => 'Bottom',
				),
				'default'   => 'center',
				'selectors' => array(
					'{{WRAPPER}} .sifency-fs-col.center' => 'align-self: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_features',
			array(
				'label' => esc_html__( 'Features List', 'sifency-addons' ),
			)
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'position',
			array(
				'label'   => esc_html__( 'Position', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => array(
					'left'  => array(
						'title' => 'Left Side',
						'icon'  => 'eicon-h-align-left',
					),
					'right' => array(
						'title' => 'Right Side',
						'icon'  => 'eicon-h-align-right',
					),
				),
				'default' => 'left',
			)
		);
		$repeater->add_control(
			'title',
			array(
				'label'   => esc_html__( 'Title', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => 'Feature Title',
			)
		);
		$repeater->add_control(
			'description',
			array(
				'label'   => esc_html__( 'Description', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXTAREA,
				'default' => 'Add details about this feature here.',
			)
		);
		$repeater->add_control(
			'icon',
			array(
				'label'   => esc_html__( 'Icon', 'sifency-addons' ),
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'icon-right',
					'library' => 'sifency-icons',
				),
			)
		);
		$repeater->add_control(
			'link',
			array(
				'label' => esc_html__( 'Link', 'sifency-addons' ),
				'type'  => Controls_Manager::URL,
			)
		);
		$this->add_control(
			'features',
			array(
				'label'       => esc_html__( 'Features', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'title'    => 'Fast Performance',
						'position' => 'left',
					),
					array(
						'title'    => 'Easy Setup',
						'position' => 'left',
					),
					array(
						'title'    => 'High Quality',
						'position' => 'right',
					),
					array(
						'title'    => '24/7 Support',
						'position' => 'right',
					),
				),
				'title_field' => '{{{ title }}}',
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'style_items',
			array(
				'label' => esc_html__( 'Feature Items', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'item_gap',
			array(
				'label'     => 'Vertical Gap',
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .sifency-fs-col' => 'gap: {{SIZE}}px;' ),
			)
		);

		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'    => 'left_item_flex_',
				'label'     => esc_html__( 'Left Items', 'sifency-addons' ),
				'selector'  => '.sifency-fs-item.sifency-fs-side-left',
				'condition' => array(),
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'    => 'right_item_flex_',
				'label'     => esc_html__( 'Right Items', 'sifency-addons' ),
				'selector'  => '.sifency-fs-item.sifency-fs-side-right',
				'condition' => array(),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_responsive',
			array(
				'label' => esc_html__( 'Mobile Behavior', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'mobile_order',
			array(
				'label'        => esc_html__( 'Image Order on Mobile', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'default'      => 'first',
				'options'      => array(
					'first'  => esc_html__( 'Image First (Top)', 'sifency-addons' ),
					'middle' => esc_html__( 'Image Middle (Between Left & Right)', 'sifency-addons' ),
					'last'   => esc_html__( 'Image Last (Bottom)', 'sifency-addons' ),
				),
				'prefix_class' => 'sifency-fs-mobile-order-',
			)
		);

		$this->end_controls_section();

		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'contet_wrapper_style',
				'label'    => 'Card Wrapper',
				'selector' => '.sifency-fs-item',
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'selector'     => '.sifency-fs-col img',
				'show_overlay' => false,
				'exclude'      => array( 'transition', 'filters', 'opacity' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'heading_style',
				'label'    => 'Heading Style',
				'selector' => '.sifency-heading-style',
				'exclude'  => array( 'radius', 'bg' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'content_style',
				'label'      => 'Content Style',
				'selector'   => '.sifency-content-style',
				'color_only' => true,
				'exclude'    => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'          => 'icon_',
				'label'           => esc_html__( 'Icon Style', 'sifency-addons' ),
				'selector'        => '.sifency-icon-style',
				'hover_selector'  => '.sifency-fs-item:hover .sifency-icon-style',
				'exclude'         => array(),
				'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();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-feature-showcase-wrapper' );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-fs-layout-' . $settings['layout_type'] );

		$left_items  = array();
		$right_items = array();

		foreach ( $settings['features'] as $index => $item ) {
			$item['original_index'] = $index;
			if ( 'left' === $item['position'] ) {
				$left_items[] = $item;
			} else {
				$right_items[] = $item;
			}
		}

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?>>
			
			<div class="sifency-fs-col left">
				<?php $this->render_items( $left_items, 'left', $settings ); ?>
			</div>

			<div class="sifency-fs-col center">
				<?php if ( ! empty( $settings['image']['url'] ) ) : ?>
					<?php echo wp_kses_post( Group_Control_Image_Size::get_attachment_image_html( $settings, 'image', 'image' ) ); ?>
				<?php endif; ?>
			</div>

			<div class="sifency-fs-col right">
				<?php $this->render_items( $right_items, 'right', $settings ); ?>
			</div>

		</div>
		<?php
	}

	protected function render_items( $items, $side, $settings ) {
		foreach ( $items as $item ) {
			$key = 'item_' . $item['original_index'];
			$this->add_render_attribute( $key, 'class', array( 'sifency-fs-item', 'sifency-fs-side-' . $side ) );

			if ( ! empty( $item['link']['url'] ) ) {
				$this->add_link_attributes( $key, $item['link'] );
			}

			$tag = ! empty( $item['link']['url'] ) ? 'a' : 'div';
			?>
			<<?php echo wp_kses_post( $tag ); ?> <?php echo wp_kses_post( $this->get_render_attribute_string( $key ) ); ?>>
				
				<div class="sifency-fs-content">
					<h4 class="sifency-fs-title sifency-heading-style"><?php echo esc_html( $item['title'] ); ?></h4>
					<div class="sifency-fs-desc sifency-content-style"><?php echo wp_kses_post( $item['description'] ); ?></div>
				</div>

				<div class="sifency-fs-icon-wrap">
					<div class="sifency-fs-icon sifency-icon-style">
						<?php Icons_Manager::render_icon( $item['icon'], array( 'aria-hidden' => 'true' ) ); ?>
					</div>
				</div>

			</<?php echo wp_kses_post( $tag ); ?>>
			<?php
		}
	}
}
