<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Post_Navigator_Controls;

class Sifency_Widget_NextPrev_Nav extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Post_Navigator_Controls;

	public function get_name() {
		return 'sifency-post-nav';
	}
	
	public function get_title() {
		return esc_html__( 'Sifency Post Nav', 'sifency-addons' );
	}
	
	public function get_icon() {
		return 'eicon-post-navigation';
	}
	
	public function get_categories() {
		return [ 'sifency-post-components' ];
	}

    public function get_style_depends() {
	    return [ 'sifency-frontend-style' , 'sifency-icon', 'sifency-post-single-style' ];
    }
	
	protected function register_controls() {
		$this->add_sifency_post_navigator_controls();
		$this->add_sifency_animation_controls();
		$this->update_control(
            'sifency_animation_target',
            [
                'type' => Controls_Manager::HIDDEN,
                'default' => 'wrapper',
            ]
        );
	}
	
	/**
	 * Render the widget output on the frontend.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function render() {
		$settings = $this->get_settings_for_display();
		$animation_manager = new Sifency_Animation_Manager($settings);
		$prev_post = get_previous_post();
		$next_post = get_next_post();

		if ( ! $prev_post && ! $next_post ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				echo '<div class="sifency-placeholder">' . esc_html__('No previous or next post found.', 'sifency-addons') . '</div>';
			}
			return;
		}
		$wrapper_classes = ['sifency-post-navigation'];
		$wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
		?>
		<div <?php 
			
			echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php if ( $prev_post ) :
				$prev_title = 'yes' === $settings['show_post_title'] ? $prev_post->post_title : '';
				?>
				<div class="sifency-post-navigation__prev sifency-button-style">
					<a class="sifency-post-navigation__link" href="<?php echo esc_url(get_permalink($prev_post)); ?>">
						<span class="sifency-post-navigation__arrow sifency-icon-style">
							<?php \Elementor\Icons_Manager::render_icon( $settings['prev_icon'], [ 'aria-hidden' => 'true' ] ); ?>
						</span>
						<span class="sifency-post-navigation__link-content">
							<span class="sifency-post-navigation__link-label"><?php echo esc_html($settings['prev_text']); ?></span>
							<?php if ($prev_title) : ?>
								<span class="sifency-post-navigation__link-title sifency-heading-style"><?php echo esc_html($prev_title); ?></span>
							<?php endif; ?>
						</span>
					</a>
				</div>
			<?php endif; ?>
			
			<?php if ( $next_post ) :
				$next_title = 'yes' === $settings['show_post_title'] ? $next_post->post_title : '';
				?>
				<div class="sifency-post-navigation__next sifency-button-style">
						<a class="sifency-post-navigation__link" href="<?php echo esc_url(get_permalink($next_post)); ?>">
						<span class="sifency-post-navigation__link-content">
							<span class="sifency-post-navigation__link-label"><?php echo esc_html($settings['next_text']); ?></span>
							<?php if ($next_title) : ?>
								<span class="sifency-post-navigation__link-title sifency-heading-style"><?php echo esc_html($next_title); ?></span>
							<?php endif; ?>
						</span>
						<span class="sifency-post-navigation__arrow sifency-icon-style">
							<?php \Elementor\Icons_Manager::render_icon( $settings['next_icon'], [ 'aria-hidden' => 'true' ] ); ?>
						</span>
					</a>
				</div>
			<?php endif; ?>
		</div>
		<?php
	}

	/**
	 * Render the placeholder content for the editor.
	 */
	protected function render_editor_placeholder() {
		$settings = $this->get_settings_for_display();
		?>
		<div class="sifency-post-navigation">
			<div class="sifency-post-navigation__prev">
				<a class="sifency-post-navigation__link" href="#">
					<span class="sifency-post-navigation__arrow">
						<?php Icons_Manager::render_icon( $settings['prev_icon'], [ 'aria-hidden' => 'true' ] ); ?>
					</span>
					<span class="sifency-post-navigation__link-content">
						<span class="sifency-post-navigation__link-label"><?php echo esc_html($settings['prev_text']); ?></span>
						<?php if ('yes' === $settings['show_post_title']) : ?>
							<span class="sifency-post-navigation__link-title"><?php esc_html_e('Demo Previous Post Title', 'sifency-addons'); ?></span>
						<?php endif; ?>
					</span>
				</a>
			</div>
			<div class="sifency-post-navigation__next">
				<a class="sifency-post-navigation__link" href="#">
					<span class="sifency-post-navigation__link-content">
						<span class="sifency-post-navigation__link-label"><?php echo esc_html($settings['next_text']); ?></span>
						<?php if ('yes' === $settings['show_post_title']) : ?>
							<span class="sifency-post-navigation__link-title"><?php esc_html_e('Demo Next Post Title', 'sifency-addons'); ?></span>
						<?php endif; ?>
					</span>
					<span class="sifency-post-navigation__arrow">
						<?php Icons_Manager::render_icon( $settings['next_icon'], [ 'aria-hidden' => 'true' ] ); ?>
					</span>
				</a>
			</div>
		</div>
		<?php
	}

	/**
	 * Render the widget output in the editor for live preview.
	 */
	protected function _content_template() {
		?>
		<#
			var prevIconHTML = elementor.helpers.renderIcon( view, settings.prev_icon, { 'aria-hidden': 'true' }, 'i' , 'object' );
			var nextIconHTML = elementor.helpers.renderIcon( view, settings.next_icon, { 'aria-hidden': 'true' }, 'i' , 'object' );
		#>
		<div class="sifency-post-navigation">
			<div class="sifency-post-navigation__prev">
				<a class="sifency-post-navigation__link" href="#">
					<# if ( prevIconHTML.value ) { #>
					<span class="sifency-post-navigation__arrow">
						{{{ prevIconHTML.value }}}
					</span>
					<# } #>
					<span class="sifency-post-navigation__link-content">
						<# if ( settings.prev_text ) { #>
						<span class="sifency-post-navigation__link-label">{{{ settings.prev_text }}}</span>
						<# } #>
						<# if ( 'yes' === settings.show_post_title ) { #>
						<span class="sifency-post-navigation__link-title"><?php esc_html_e('Demo Previous Post Title', 'sifency-addons'); ?></span>
						<# } #>
					</span>
				</a>
			</div>
			<div class="sifency-post-navigation__next">
				<a class="sifency-post-navigation__link" href="#">
					<span class="sifency-post-navigation__link-content">
						<# if ( settings.next_text ) { #>
						<span class="sifency-post-navigation__link-label">{{{ settings.next_text }}}</span>
						<# } #>
						<# if ( 'yes' === settings.show_post_title ) { #>
						<span class="sifency-post-navigation__link-title"><?php esc_html_e('Demo Next Post Title', 'sifency-addons'); ?></span>
						<# } #>
					</span>
					<# if ( nextIconHTML.value ) { #>
					<span class="sifency-post-navigation__arrow">
						{{{ nextIconHTML.value }}}
					</span>
					<# } #>
				</a>
			</div>
		</div>
		<?php
	}
}