<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Image_Size;
use Elementor\Utils;
use Elementor\Plugin;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;

class Sifency_Velocity_Grid extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Master_Typography;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-velocity-grid',
			'title'          => 'Sifency Velocity Skew Grid',
			'icon'           => 'eicon-gallery-grid',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'gsap', 'gsap-scrolltrigger', 'sifency-velocity-script', 'lenis' ),
			'style_depends'  => array( 'sifency-creative-style' ),
			'keywords'       => array( 'velocity', 'skew', 'momentum', 'grid', 'gsap', 'template' ),
		);
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_items',
			array( 'label' => esc_html__( 'Grid Items', 'sifency-addons' ) )
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'content_type',
			array(
				'label'   => esc_html__( 'Content Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'custom',
				'options' => array(
					'custom'   => esc_html__( 'Custom Content', 'sifency-addons' ),
					'template' => esc_html__( 'Elementor Template', 'sifency-addons' ),
				),
			)
		);

		// --- Template Selection ---
		$repeater->add_control(
			'template_id',
			array(
				'label'     => esc_html__( 'Choose Template', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => function_exists( 'sifency_addons_get_elementor_templates' ) ? sifency_addons_get_elementor_templates() : array(),
				'condition' => array( 'content_type' => 'template' ),
			)
		);

		// --- Custom Content ---
		$repeater->add_control(
			'image',
			array(
				'label'     => esc_html__( 'Choose Image', 'sifency-addons' ),
				'type'      => Controls_Manager::MEDIA,
				'default'   => array( 'url' => Utils::get_placeholder_image_src() ),
				'condition' => array( 'content_type' => 'custom' ),
			)
		);

		$repeater->add_control(
			'title',
			array(
				'label'       => esc_html__( 'Title', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Project Title', 'sifency-addons' ),
				'label_block' => true,
				'condition'   => array( 'content_type' => 'custom' ),
			)
		);

		$repeater->add_control(
			'category',
			array(
				'label'     => esc_html__( 'Category/Subtitle', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Web Design', 'sifency-addons' ),
				'condition' => array( 'content_type' => 'custom' ),
			)
		);

		$repeater->add_control(
			'description',
			array(
				'label'     => esc_html__( 'Description (HTML Supported)', 'sifency-addons' ),
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => 'Add some detailed description here.',
				'condition' => array( 'content_type' => 'custom' ),
			)
		);

		$this->add_control(
			'grid_items',
			array(
				'label'       => esc_html__( 'Velocity Cards', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'title'        => 'Project Alpha',
						'category'     => 'Branding',
						'content_type' => 'custom',
					),
					array(
						'title'        => 'Project Beta',
						'category'     => 'UI/UX',
						'content_type' => 'custom',
					),
				),
				'title_field' => '{{{ title }}}',
			)
		);

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

		$this->end_controls_section();

		// --- Velocity Effect Settings ---
		$this->start_controls_section(
			'section_velocity_settings',
			array( 'label' => esc_html__( 'Velocity Effect Settings', 'sifency-addons' ) )
		);

		$this->add_control(
			'max_skew',
			array(
				'label'   => esc_html__( 'Maximum Skew Angle', 'sifency-addons' ),
				'type'    => Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min'  => 1,
						'max'  => 30,
						'step' => 1,
					),
				),
				'default' => array( 'size' => 15 ),
			)
		);

		$this->add_control(
			'sensitivity',
			array(
				'label'   => esc_html__( 'Sensitivity', 'sifency-addons' ),
				'type'    => Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min'  => 50,
						'max'  => 500,
						'step' => 10,
					),
				),
				'default' => array( 'size' => 100 ),
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'content_',
				'label'    => esc_html__( 'Content Alignment', 'sifency-addons' ),
				'selector' => '.sifency-velocity-content',
			)
		);
		$this->end_controls_section();

		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'selector' => '.sifency-velocity-item',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'title_',
				'label'      => esc_html__( 'Title', 'sifency-addons' ),
				'selector'   => '.sifency-velocity-title',
				'color_only' => true,
				'exclude'    => array( 'bg', 'border', 'radius', 'stroke', 'tabs', 'transitions', 'shadow' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'category_',
				'label'      => esc_html__( 'Category', 'sifency-addons' ),
				'selector'   => '.sifency-velocity-category',
				'color_only' => true,
				'exclude'    => array( 'bg', 'border', 'radius', 'stroke', 'tabs', 'transitions', 'shadow' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'description_',
				'label'      => esc_html__( 'Description', 'sifency-addons' ),
				'selector'   => '.sifency-velocity-description',
				'color_only' => true,
				'exclude'    => array( 'bg', 'border', 'radius', 'stroke', 'tabs', 'transitions', 'shadow' ),
			)
		);
	}

	protected function render_dom( array $context ) {
		$settings   = $this->get_settings_for_display();
		$wrapper_id = esc_attr( $context['dom_id'] );
		$items      = $settings['grid_items'];

		if ( empty( $items ) ) {
			return;
		}

		$max_skew    = $settings['max_skew']['size'] ?? 15;
		$sensitivity = $settings['sensitivity']['size'] ?? 100;
		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" class="sifency-velocity-wrapper" 
			data-max-skew="<?php echo esc_attr( $max_skew ); ?>" 
			data-sensitivity="<?php echo esc_attr( $sensitivity ); ?>">
			
			<div class="sifency-velocity-grid">
				<?php
				foreach ( $items as $item ) :
					$item_id      = $item['_id'];
					$content_type = $item['content_type'];
					?>
					<div class="sifency-velocity-item elementor-repeater-item-<?php echo esc_attr( $item_id ); ?>">
						
						<?php if ( 'template' === $content_type && ! empty( $item['template_id'] ) ) : ?>
							<!-- Render Template -->
							<div class="sifency-velocity-template-content">
								<?php echo wp_kses_post( Plugin::$instance->frontend->get_builder_content_for_display( $item['template_id'] ) ); ?>
							</div>
						
						<?php else : ?>
							<!-- Render Custom Content -->
							<div class="sifency-velocity-img-wrapper">
								<?php
								$image_url = Group_Control_Image_Size::get_attachment_image_src( $item['image']['id'], 'image', $settings );
								if ( ! $image_url ) {
									$image_url = $item['image']['url'];
								}
								?>
								<img class="sifency-velocity-img" src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( $item['title'] ); ?>">
							</div>
							<div class="sifency-velocity-content">
								<h3 class="sifency-velocity-title"><?php echo esc_html( $item['title'] ); ?></h3>
								<span class="sifency-velocity-cat"><?php echo esc_html( $item['category'] ); ?></span>
								<?php if ( ! empty( $item['description'] ) ) : ?>
									<div class="sifency-velocity-desc">
										<?php echo wp_kses_post( $item['description'] ); ?>
									</div>
								<?php endif; ?>
							</div>
						<?php endif; ?>

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