<?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_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Image_Size;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Slider_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Addons_Widget_Social_Feed extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Slider_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-social-feed',
			'title'          => 'Sifency Social Feed',
			'icon'           => 'eicon-social-icons',
			'category'       => 'sifency-advance-widgets',
			'keywords'       => array( 'social', 'feed', 'instagram', 'twitter', 'facebook' ),
			'style_depends'  => array( 'sifency-seo-style', 'sifency-icon', 'swiper', 'sifency-frontend-style', 'sifency-icon' ),
			'script_depends' => array( 'masonry', 'sifency-seo-script' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Feed Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'grid',
				'options' => array(
					'grid'     => esc_html__( '1. Modern Grid', 'sifency-addons' ),
					'masonry'  => esc_html__( '2. Masonry Wall', 'sifency-addons' ),
					'carousel' => esc_html__( '3. Feed Carousel', 'sifency-addons' ),
					'story'    => esc_html__( '4. Story Circle (Instagram Style)', 'sifency-addons' ),
					'list'     => esc_html__( '5. Minimal List', 'sifency-addons' ),
				),
			)
		);
		$this->add_responsive_control(
			'columns',
			array(
				'label'     => 'Columns',
				'type'      => Controls_Manager::SELECT,
				'default'   => '3',
				'options'   => array(
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
					'5' => '5',
					'6' => '6',
				),
				'condition' => array( 'layout_type' => array( 'grid', 'masonry' ) ),
				'selectors' => array( '{{WRAPPER}} .sifency-sf-grid' => 'grid-template-columns: repeat({{VALUE}}, 1fr);' ),
			)
		);
		$this->add_control(
			'show_icon',
			array(
				'label'   => 'Show Network Icon',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_overlay',
			array(
				'label'     => 'Show Hover Overlay',
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'condition' => array( 'layout_type!' => 'story' ),
			)
		);

		$repeater = new Repeater();
		$repeater->add_control(
			'network',
			array(
				'label'   => esc_html__( 'Network', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'instagram',
				'options' => array(
					'instagram' => 'Instagram',
					'twitter'   => 'Twitter (X)',
					'youtube'   => 'YouTube',
					'facebook'  => 'Facebook',
					'tiktok'    => 'TikTok',
					'linkedin'  => 'LinkedIn',
				),
			)
		);
		$repeater->add_control(
			'content_source',
			array(
				'label'   => 'Content Type',
				'type'    => Controls_Manager::SELECT,
				'default' => 'manual',
				'options' => array(
					'manual' => 'Manual Image/Text',
					'embed'  => 'oEmbed (URL)',
				),
			)
		);

		// Manual Content
		$repeater->add_control(
			'image',
			array(
				'label'     => 'Image',
				'type'      => Controls_Manager::MEDIA,
				'condition' => array( 'content_source' => 'manual' ),
			)
		);
		$repeater->add_control(
			'caption',
			array(
				'label'     => 'Caption',
				'type'      => Controls_Manager::TEXTAREA,
				'condition' => array( 'content_source' => 'manual' ),
			)
		);
		$repeater->add_control(
			'link',
			array(
				'label'     => 'Link',
				'type'      => Controls_Manager::URL,
				'condition' => array( 'content_source' => 'manual' ),
			)
		);
		$repeater->add_control(
			'author',
			array(
				'label'     => 'Author Name',
				'type'      => Controls_Manager::TEXT,
				'default'   => '@username',
				'condition' => array( 'content_source' => 'manual' ),
			)
		);

		// Embed Content
		$repeater->add_control(
			'embed_url',
			array(
				'label'       => 'Post URL',
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'https://instagram.com/p/...',
				'condition'   => array( 'content_source' => 'embed' ),
			)
		);

		$this->add_control(
			'items',
			array(
				'label'       => esc_html__( 'Feed Items', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'network' => 'instagram',
						'author'  => '@sifency',
					),
					array(
						'network' => 'twitter',
						'author'  => '@sifency_wp',
					),
					array(
						'network' => 'youtube',
						'author'  => 'Sifency Channel',
					),
				),
				'title_field' => '{{{ network }}} - {{{ author }}}',
			)
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'    => 'thumbnail',
				'default' => 'medium_large',
			)
		);

		$this->end_controls_section();

		// --- SLIDER CONTROLS (Trait) ---
		$this->add_sifency_addons_slider_controls(
			array(
				'condition' => array( 'layout_type' => 'carousel' ),
			)
		);
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'style_overlay',
			array(
				'label'     => 'Hover Overlay',
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_overlay' => 'yes' ),
			)
		);
		$this->add_control(
			'overlay_bg',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-sf-overlay' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_item_style_controls(
			array(
				'label'    => esc_html__( 'Item Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-social-feed-item',
				'exclude'  => array( 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'label'     => esc_html__( 'Image', 'sifency-addons' ),
				'selector'  => '.sifency-sf-image img',
				'selectors' => array(
					'image'      => '.sifency-sf-image img',
					'container'  => '.sifency-sf-image img',
					'dimensions' => '.sifency-sf-image img',
				),
				'exclude'   => array( 'tabs', 'transition' ),
			)
		);

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'author_text_',
				'label'      => esc_html__( 'Author Text', 'sifency-addons' ),
				'selector'   => '.sifency-sf-author',
				'color_only' => true,
				'exclude'    => array( 'tabs', 'transition', 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'caption_',
				'label'      => esc_html__( 'Caption Text', 'sifency-addons' ),
				'selector'   => '.sifency-sf-caption',
				'color_only' => true,
				'exclude'    => array( 'tabs', 'alignment', 'bg', 'radius', 'transition' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'   => 'icon_',
				'selector' => '.sifency-sf-icon i',
			)
		);
		$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-social-feed-wrapper' );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-sf-layout-' . $settings['layout_type'] );

		$container_class = 'sifency-sf-grid';
		if ( 'carousel' === $settings['layout_type'] ) {
			$container_class = 'swiper-container sifency-carousel-wrapper';
			$slider_options  = $this->get_sifency_swiper_options( $settings, count( $settings['items'] ) );
			$this->add_render_attribute( 'container', 'data-swiper-options', wp_json_encode( $slider_options ) );
		} elseif ( 'masonry' === $settings['layout_type'] ) {
			$container_class = 'sifency-sf-masonry';
		}

		$this->add_render_attribute( 'container', 'class', $container_class );
		$this->render_sifency_section_header_elements( $settings );

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?>>
			
			<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'container' ) ); ?>>
				
				<?php
				if ( 'carousel' === $settings['layout_type'] ) :
					?>
					<div class="swiper-wrapper"><?php endif; ?>

				<?php
				foreach ( $settings['items'] as $item ) :
					$img_src = '';
					if ( ! empty( $item['image']['id'] ) ) {
						$img_src = Group_Control_Image_Size::get_attachment_image_src( $item['image']['id'], 'thumbnail', $settings );
					} elseif ( ! empty( $item['image']['url'] ) ) {
						$img_src = $item['image']['url'];
					}

					$icon_class = $this->get_social_icon( $item['network'] );
					$item_class = ( 'carousel' === $settings['layout_type'] ) ? 'swiper-slide' : 'sifency-sf-col sifency-social-feed-item';
					?>
					<div class="<?php echo esc_attr( $item_class ); ?> sifency-sf-network-<?php echo esc_attr( $item['network'] ); ?>">
						
						<div class="sifency-sf-item-inner">
							
							<!-- EMBED MODE -->
							<?php if ( 'embed' === $item['content_source'] && ! empty( $item['embed_url'] ) ) : ?>
								<div class="sifency-sf-embed">
									<?php echo esc_html( wp_oembed_get( $item['embed_url'] ) ); ?>
								</div>
							
							<!-- MANUAL MODE -->
							<?php else : ?>
								<?php if ( $img_src ) : ?>
									<div class="sifency-sf-image">
										<img src="<?php echo esc_url( $img_src ); ?>" alt="<?php echo esc_attr( $item['network'] ); ?>">
										
										<!-- Overlay -->
										<?php if ( 'yes' === $settings['show_overlay'] ) : ?>
											<div class="sifency-sf-overlay">
												<div class="sifency-sf-caption"><?php echo esc_html( wp_trim_words( $item['caption'], 20 ) ); ?></div>
												<div class="sifency-sf-author"><?php echo esc_html( $item['author'] ); ?></div>
												<?php if ( ! empty( $item['link']['url'] ) ) : ?>
													<a href="<?php echo esc_url( $item['link']['url'] ); ?>" class="sifency-sf-link-overlay" target="_blank" rel="nofollow"></a>
												<?php endif; ?>
											</div>
										<?php endif; ?>
									</div>
								<?php endif; ?>

								<?php if ( 'yes' === $settings['show_icon'] ) : ?>
									<div class="sifency-sf-icon"><i class="<?php echo esc_attr( $icon_class ); ?>"></i></div>
								<?php endif; ?>

								<!-- Layout Specific: Story Ring -->
								<?php if ( 'story' === $settings['layout_type'] ) : ?>
									<div class="sifency-sf-story-meta">
										<span class="sifency-sf-story-user"><?php echo esc_html( $item['author'] ); ?></span>
									</div>
								<?php endif; ?>

							<?php endif; ?>

						</div>

					</div>
				<?php endforeach; ?>

				<?php
				if ( 'carousel' === $settings['layout_type'] ) :
					?>
					</div><?php endif; ?>

			</div>

			<?php $this->render_sifency_addons_slider_nav( $settings ); ?>

		</div>
		<?php
	}

	private function get_social_icon( $network ) {
		$icons = array(
			'instagram' => 'icon-instagram',
			'twitter'   => 'icon-twitter',
			'facebook'  => 'icon-facebook',
			'youtube'   => 'icon-youtube-play',
			'tiktok'    => 'icon-tiktok',
			'linkedin'  => 'icon-linkedin',
		);
		return $icons[ $network ] ?? 'fas fa-share-alt';
	}
}
