<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
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_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Post_Navigator_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;
class Sifency_Widget_NextPrev_Nav extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Post_Navigator_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-post-nav',
			'title'         => 'Sifency Post Nav',
			'icon'          => 'eicon-post-navigation',
			'category'      => array( 'sifency-post-components' ),
			'keywords'      => array( 'navigation', 'post', 'article', 'text', 'dynamic', 'sifency' ),
			'style_depends' => array( 'sifency-frontend-style', 'sifency-icon', 'sifency-post-single-style' ),
		);}
	protected function register_controls() {
		$this->add_sifency_post_navigator_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'item_container',
				'label'    => esc_html__( 'Nav Item Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-item-style .sifency-post-navigation__link',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'prev_next_text_style_',
				'label'          => 'Typography',
				'selector'       => '.sifency-post-navigation__link-label',
				'hover_selector' => '.sifency-nav-item:hover .sifency-post-navigation__link .sifency-post-navigation__link-content .sifency-post-navigation__link-label',
				'color_only'     => true,
				'condition'      => array(
					'prev_text!' => '',
					'next_text!' => '',
				),
				'exclude'        => array( 'box_model', 'shadow', 'transition', 'alignment', 'bg', 'border', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'heading_style',
				'label'          => 'Dynamic Post Title Override',
				'selector'       => '.sifency-post-navigation__link-title',
				'hover_selector' => '.sifency-nav-item:hover .sifency-post-navigation__link-title',
				'condition'      => array( 'show_post_title' => 'yes' ),
				'show_max_width' => true,
				'exclude'        => array( 'box_model', 'shadow', 'transition', 'alignment', 'bg', 'border', 'radius' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'label'          => esc_html__( 'Icon Style Override', 'sifency-addons' ),
				'selector'       => '.sifency-post-navigation__arrow',
				'hover_selector' => '.sifency-nav-item:hover .sifency-post-navigation__arrow',
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

	/**
	 * Render the widget output on the frontend.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	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 );
		$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 = array( 'sifency-post-navigation' );
		$wrapper_attrs   = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?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-item-style sifency-nav-item sifency-button-style">
					<a class="sifency-post-navigation__link" href="<?php echo esc_url( get_permalink( $prev_post ) ); ?>">
						<?php if ( ! empty( $settings['prev_icon']['value'] ) ) : ?>
							<span class="sifency-post-navigation__arrow sifency-icon-style">
								<?php \Elementor\Icons_Manager::render_icon( $settings['prev_icon'], array( 'aria-hidden' => 'true' ) ); ?>
							</span>
						<?php endif; ?>
						<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"><?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-item-style sifency-nav-item 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"><?php echo esc_html( $next_title ); ?></span>
							<?php endif; ?>
						</span>
						<?php if ( ! empty( $settings['next_icon']['value'] ) ) : ?>
							<span class="sifency-post-navigation__arrow sifency-icon-style">
								<?php \Elementor\Icons_Manager::render_icon( $settings['next_icon'], array( 'aria-hidden' => 'true' ) ); ?>
							</span>
						<?php endif; ?>
					</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'], array( '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'], array( '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
	}
}
