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

use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Combine_Button_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Addons_Widget_Unfold extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Combine_Button_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-unfold',
			'title'          => 'Sifency Expand & Unfold',
			'icon'           => 'eicon-text-align-center',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'sifency-unfold-script' ),
			'style_depends'  => array( 'sifency-moon-style' ),
		);
	}
	protected function register_controls() {
		$this->start_controls_section( 'section_content', array( 'label' => 'Text Content / Template Unfold' ) );

		$this->add_control(
			'content_source',
			array(
				'label'   => 'Content Source',
				'type'    => Controls_Manager::SELECT,
				'default' => 'text',
				'options' => array(
					'text'            => 'Text Content Unfold',
					'template'        => 'Unfold for Elementor Template Content',
					'html'            => 'Custom HTML',
					'container'       => 'Container Based Unfold',
					'css-class-or-id' => 'CSS Class or ID Based Unfold',
				),
			)
		);

		$this->add_control(
			'text_content',
			array(
				'label'     => 'Text Content',
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => 'Your long text goes here...',
				'condition' => array( 'content_source' => 'text' ),
			)
		);

		$this->add_control(
			'html_content',
			array(
				'label'     => 'Custom HTML / Shortcode',
				'type'      => Controls_Manager::CODE,
				'language'  => 'html',
				'condition' => array( 'content_source' => 'html' ),
			)
		);

		$this->add_control(
			'template_id',
			array(
				'label'     => 'Select Template',
				'type'      => Controls_Manager::SELECT2,
				'options'   => function_exists( 'sifency_addons_get_elementor_templates' ) ? sifency_addons_get_elementor_templates() : array(),
				'condition' => array( 'content_source' => 'template' ),
			)
		);
		$this->add_control(
			'target_container_id',
			array(
				'label'       => 'Elementor Container ID',
				'type'        => Controls_Manager::TEXT,
				'description' => 'Enter the ID of the Container (without the #). Example: my-container',
				'condition'   => array( 'content_source' => 'container' ),
			)
		);
		$this->add_control(
			'target_css_selector',
			array(
				'label'       => 'CSS Class or ID',
				'type'        => Controls_Manager::TEXT,
				'description' => 'Enter exact selector (e.g., .my-custom-class or #my-id).',
				'condition'   => array( 'content_source' => 'css-class-or-id' ),
			)
		);

		$this->end_controls_section();
		$this->start_controls_section( 'section_behavior', array( 'label' => 'Unfold Settings & Fade Out' ) );
		$this->add_responsive_control(
			'collapsed_height',
			array(
				'label'     => 'Collapsed Height (px)',
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 50,
						'max' => 1000,
					),
				),
				'default'   => array(
					'size' => 150,
					'unit' => 'px',
				),
				'selectors' => array(
					'{{WRAPPER}}' => '--sf-collapsed-height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'fade_out_heading',
			array(
				'label'     => 'Fade Out Content With Opacity Controls',
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'enable_fade',
			array(
				'label'   => 'Enable Fade Out',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);

		$this->add_control(
			'fade_color',
			array(
				'label'     => 'Fade Background Color',
				'type'      => Controls_Manager::COLOR,
				'default'   => '#ffffff',
				'condition' => array( 'enable_fade' => 'yes' ),
			)
		);

		$this->add_control(
			'behavior_heading',
			array(
				'label'     => 'Adjust Transition Duration & Alignment',
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'transition_duration',
			array(
				'label'   => 'Transition Duration (ms)',
				'type'    => Controls_Manager::NUMBER,
				'default' => 500,
			)
		);

		$this->add_control(
			'scroll_to_top',
			array(
				'label' => 'Scroll to Top on Expand',
				'type'  => Controls_Manager::SWITCHER,
			)
		);
		$this->end_controls_section();
		$this->start_controls_section( 'section_button', array( 'label' => 'Expand / Collapse Button' ) );

		$this->add_control(
			'button_position',
			array(
				'label'   => 'Place Button on Top or Bottom of the Content',
				'type'    => Controls_Manager::SELECT,
				'default' => 'bottom',
				'options' => array(
					'top'    => 'Top',
					'bottom' => 'Bottom',
				),
			)
		);

		$this->add_responsive_control(
			'button_align',
			array(
				'label'     => 'Button Alignment',
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array(
						'title' => 'Left',
						'icon'  => 'eicon-text-align-left',
					),
					'center'     => array(
						'title' => 'Center',
						'icon'  => 'eicon-text-align-center',
					),
					'flex-end'   => array(
						'title' => 'Right',
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'   => 'center',
				'selectors' => array( '{{WRAPPER}} .sifency-unfold-actions' => 'justify-content: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'text_expand',
			array(
				'label'   => 'Expand Button Text',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Read More',
			)
		);
		$this->add_control(
			'text_collapse',
			array(
				'label'   => 'Collapse Button Text',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Read Less',
			)
		);

		$this->add_control(
			'media_type',
			array(
				'label'   => 'Button Media',
				'type'    => Controls_Manager::SELECT,
				'default' => 'icon',
				'options' => array(
					'none'   => 'None',
					'icon'   => 'Add Icon in Expand Button',
					'lottie' => 'Add Lottie Animation in Expand Button',
				),
			)
		);
		$this->add_control(
			'icon_expand',
			array(
				'label'     => 'Expand Icon',
				'type'      => Controls_Manager::ICONS,
				'condition' => array( 'media_type' => 'icon' ),
			)
		);
		$this->add_control(
			'icon_collapse',
			array(
				'label'     => 'Collapse Icon',
				'type'      => Controls_Manager::ICONS,
				'condition' => array( 'media_type' => 'icon' ),
			)
		);
		$this->add_control(
			'lottie_url',
			array(
				'label'     => 'Lottie JSON URL',
				'type'      => Controls_Manager::TEXT,
				'condition' => array( 'media_type' => 'lottie' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_combine_button_content_controls(
			array(
				'prefix' => 'primary_btn',
				'label'  => 'Additional Button',
			)
		);
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'style_general_section',
			array(
				'label' => 'Customize Everything',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'button_style_preset',
			array(
				'label'   => '10+ Button Layout Styles',
				'type'    => Controls_Manager::SELECT,
				'default' => 'btn-classic',
				'options' => array(
					'btn-classic'    => '1. Classic Solid',
					'btn-outline'    => '2. Clean Outline',
					'btn-pill'       => '3. Modern Pill',
					'btn-glass'      => '4. Glassmorphism',
					'btn-neumorph'   => '5. Neumorphic',
					'btn-cyberpunk'  => '6. Cyberpunk Neon',
					'btn-brutal'     => '7. Brutalism',
					'btn-ghost'      => '8. Ghost Underline',
					'btn-gradient'   => '9. Gradient Mesh',
					'btn-shadow-pop' => '10. 3D Shadow Pop',
				),
			)
		);
		$this->end_controls_section();
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'selector' => '.sf-unfold-btn',
			)
		);
		$this->add_Sifency_Addons_Combine_Button_Controls(
			array(
				'prefix'   => 'primary_btn',
				'selector' => '.sifency-addons-primary-button',
			)
		);
	}

	protected function render_dom( array $context ) {
		$settings = $this->get_settings_for_display();

		$data_settings = array(
			'source'      => $settings['content_source'],
			'target'      => '',
			'duration'    => $settings['transition_duration'] ?: 500,
			'scrollTop'   => $settings['scroll_to_top'] === 'yes',
			'txtExpand'   => $settings['text_expand'],
			'txtCollapse' => $settings['text_collapse'],
			'enableFade'  => $settings['enable_fade'] === 'yes',
			'fadeColor'   => $settings['fade_color'],
		);

		if ( $settings['content_source'] === 'container' && ! empty( $settings['target_container_id'] ) ) {
			$data_settings['target'] = '#' . ltrim( $settings['target_container_id'], '#' );
		} elseif ( $settings['content_source'] === 'css-class-or-id' && ! empty( $settings['target_css_selector'] ) ) {
			$data_settings['target'] = $settings['target_css_selector'];
		}

		$is_internal = in_array( $settings['content_source'], array( 'text', 'template', 'html' ) );
		?>
		<div class="sifency-unfold-wrapper" data-unfold='<?php echo json_encode( $data_settings ); ?>'>
			
			<?php
			if ( $settings['button_position'] === 'top' ) {
				$this->render_buttons( $settings ); }
			?>

			<?php if ( $is_internal ) : ?>
				<div class="sifency-unfold-content-wrap">
					<div class="sifency-unfold-content">
						<?php
						if ( $settings['content_source'] === 'text' ) {
							echo wp_kses_post( $settings['text_content'] ); } elseif ( $settings['content_source'] === 'html' ) {
							echo do_shortcode( $settings['html_content'] ); } elseif ( $settings['content_source'] === 'template' && ! empty( $settings['template_id'] ) ) {
                            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally.
								echo \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $settings['template_id'] );
							}
							?>
					</div>
					<?php if ( $settings['enable_fade'] === 'yes' ) : ?>
						<div class="sifency-unfold-fade" style="background: linear-gradient(to bottom, transparent, <?php echo esc_attr( $settings['fade_color'] ); ?>);"></div>
					<?php endif; ?>
				</div>
			<?php endif; ?>

			<?php
			if ( $settings['button_position'] === 'bottom' ) {
				$this->render_buttons( $settings ); }
			?>

		</div>
		<?php
	}

	private function render_buttons( $settings ) {
		$btn_class = $settings['button_style_preset'];
		?>
		<div class="sifency-unfold-actions">
			<button class="sf-unfold-btn <?php echo esc_attr( $btn_class ); ?>">
				<span class="sf-btn-text"><?php echo esc_html( $settings['text_expand'] ); ?></span>
				<?php if ( $settings['media_type'] === 'icon' ) : ?>
					<span class="sf-btn-icon icon-expand"><?php \Elementor\Icons_Manager::render_icon( $settings['icon_expand'] ); ?></span>
					<span class="sf-btn-icon icon-collapse" style="display:none;"><?php \Elementor\Icons_Manager::render_icon( $settings['icon_collapse'] ); ?></span>
				<?php elseif ( $settings['media_type'] === 'lottie' && ! empty( $settings['lottie_url'] ) ) : ?>
					<span class="sf-btn-lottie"><lottie-player src="<?php echo esc_url( $settings['lottie_url'] ); ?>" background="transparent" speed="1" style="width: 24px; height: 24px;" loop autoplay></lottie-player></span>
				<?php endif; ?>
			</button>
			<?php $this->add_sifency_addons_combine_button_render( $settings, 'primary_btn', 'sifency-addons-primary-button' ); ?>
		</div>
		<?php
	}
}
