<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Label_Content_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Draggable_Object extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Label_Content_Controls;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-draggable-object',
			'title'          => esc_html__( 'Sifency Draggable Object', 'sifency-addons' ),
			'icon'           => 'eicon-drag-n-drop',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'sifency-draggable-script' ),
			'style_depends'  => array( 'sifency-creative-style' ),
			'keywords'       => array( 'drag', 'floating', 'sticker', 'move', 'vanilla' ),
		);
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Object Content', 'sifency-addons' ) )
		);
		$this->add_control(
			'content_type',
			array(
				'label'   => esc_html__( 'Content Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'text',
				'options' => array(
					'text'      => esc_html__( 'Text / Badge', 'sifency-addons' ),
					'editor'    => esc_html__( 'Editor Text', 'sifency-addons' ),
					'image'     => esc_html__( 'Image / Sticker', 'sifency-addons' ),
					'elementor' => esc_html__( 'Elementor Template', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'elementor_template',
			array(
				'label'     => esc_html__( 'Select Template', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => '0',
				'options'   => function_exists( 'sifency_addons_get_elementor_templates' ) ? sifency_addons_get_elementor_templates() : array(),
				'condition' => array( 'content_type' => 'elementor' ),
			)
		);
		$this->add_control(
			'object_text',
			array(
				'label'     => esc_html__( 'Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Drag Me 🖐️', 'sifency-addons' ),
				'condition' => array( 'content_type' => 'text' ),
			)
		);
		$this->add_control(
			'object_editor',
			array(
				'label'     => esc_html__( 'Editor Text', 'sifency-addons' ),
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => esc_html__( 'Drag Me 🖐️', 'sifency-addons' ),
				'condition' => array( 'content_type' => 'editor' ),
			)
		);
		$this->add_control(
			'object_image',
			array(
				'label'     => esc_html__( 'Choose Image', 'sifency-addons' ),
				'type'      => Controls_Manager::MEDIA,
				'default'   => array( 'url' => \Elementor\Utils::get_placeholder_image_src() ),
				'condition' => array( 'content_type' => 'image' ),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_drag_settings',
			array( 'label' => esc_html__( 'Drag Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'drag_bounds',
			array(
				'label'   => esc_html__( 'Movement Boundary', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'window',
				'options' => array(
					'window' => esc_html__( 'Entire Screen (Fixed)', 'sifency-addons' ),
					'parent' => esc_html__( 'Inside Parent Section (Absolute)', 'sifency-addons' ),
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_label_content_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'text_',
				'label'           => 'Typography',
				'selector'        => '.sifency-draggable-content',
				'color_only'      => true,
				'target_children' => array( 'p', 'span', 'h1', 'h2', 'h3' ),
				'exclude'         => array( 'stroke', 'alignment', 'tabs', 'transition', 'stroke', 'shadow' ),
				'condition'       => array( 'content_type!' => 'image' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'selector'            => '.sifency-draggable-content img',
				'base_hover_selector' => '.sifency-draggable-content',
				'selectors'           => array(
					'image'      => '.sifency-draggable-content img',
					'container'  => '.sifency-draggable-content img',
					'dimensions' => '.sifency-draggable-content img',
				),
				'show_overlay'        => false,
				'condition'           => array( 'content_type' => 'image' ),
			)
		);
		$this->start_controls_section(
			'section_style',
			array(
				'label' => esc_html__( 'Positioning', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'initial_x',
			array(
				'label'      => esc_html__( 'Initial X Position', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%', 'vw' ),
				'range'      => array(
					'px' => array(
						'min' => -500,
						'max' => 1500,
					),
				),
				'selectors'  => array( '{{WRAPPER}} .sifency-draggable-wrapper' => 'left: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			'initial_y',
			array(
				'label'      => esc_html__( 'Initial Y Position', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%', 'vh' ),
				'range'      => array(
					'px' => array(
						'min' => -500,
						'max' => 1500,
					),
				),
				'selectors'  => array( '{{WRAPPER}} .sifency-draggable-wrapper' => 'top: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'label_style',
				'label'     => esc_html__( 'Label Style', 'sifency-addons' ),
				'selector'  => '.sifency-label-style',
				'exclude'   => array( 'alignment', 'tabs', 'transition' ),
				'condition' => array( 'show_label' => 'yes' ),
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
	}

	protected function render_dom( array $context ) {
		$settings          = $this->get_settings_for_display();
		$wrapper_id        = esc_attr( $context['dom_id'] );
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$bounds            = $settings['drag_bounds'] ?? 'window';
		$position_class    = ( $bounds === 'window' ) ? 'sifency-pos-fixed' : 'sifency-pos-absolute';
		$wrapper_classes   = array( 'sifency-draggable-wrapper', $position_class );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $animation_attrs ); ?> data-bounds="<?php echo esc_attr( $bounds ); ?>">
			<div class="sifency-draggable-inner">
				<?php if ( 'yes' === $settings['show_label'] && ! empty( $settings['label_text'] ) ) : ?>
					<span class="sifency-label-style">
						<?php echo esc_html( $settings['label_text'] ); ?>
					</span>
				<?php endif; ?>

				<div class="sifency-draggable-content">
					<?php
					switch ( $settings['content_type'] ) {
						case 'text':
							echo wp_kses_post( $settings['object_text'] );
							break;
						case 'editor':
							echo wp_kses_post( $settings['object_editor'] );
							break;
						case 'elementor':
							if ( ! empty( $settings['elementor_template'] ) && '0' !== $settings['elementor_template'] ) {
								echo wp_kses_post( \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $settings['elementor_template'] ) );
							}
							break;
						case 'image':
							if ( ! empty( $settings['object_image']['url'] ) ) {
								printf(
									'<img class="sifency-draggable-img" src="%s" alt="%s">',
									esc_url( $settings['object_image']['url'] ),
									esc_attr( $settings['object_image']['alt'] ?: 'Sticker' )
								);
							}
							break;
					}
					?>
				</div>
			</div>
		</div>
		<?php
	}
}
