<?php
namespace Sifency\Traits;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;

trait Sifency_Addons_Numeric_Pagination {

	protected function add_sifency_addons_numeric_pagination_controls( $args = array() ) {
		$defaults = array(
			'prefix'    => 'pagination_',
			'selector'  => '.sifency-numeric-pagination',
			'condition' => array(),
		);

		$args     = wp_parse_args( $args, $defaults );
		$prefix   = $args['prefix'];
		$base_sel = '{{WRAPPER}} ' . $args['selector'];

		$this->start_controls_section(
			$prefix . 'content_section',
			array(
				'label'     => esc_html__( 'Pagination', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_CONTENT,
				'condition' => $args['condition'],
			)
		);

		$this->add_control(
			$prefix . 'enabled',
			array(
				'label'        => esc_html__( 'Show Pagination', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'sifency-addons' ),
				'label_off'    => esc_html__( 'No', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'no',
			)
		);

		$this->add_responsive_control(
			$prefix . 'align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'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',
					),
				),
				'selectors' => array( $base_sel => 'justify-content: {{VALUE}};' ),
				'condition' => array( $prefix . 'enabled' => 'yes' ),
			)
		);

		$this->add_responsive_control(
			$prefix . 'spacing',
			array(
				'label'     => esc_html__( 'Top Margin', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'selectors' => array( $base_sel => 'margin-top: {{SIZE}}{{UNIT}};' ),
				'condition' => array( $prefix . 'enabled' => 'yes' ),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			$prefix . 'style_section',
			array(
				'label'     => esc_html__( 'Pagination Style', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( $prefix . 'enabled' => 'yes' ),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => $prefix . 'typography',
				'selector' => $base_sel . ' .page-numbers',
			)
		);

		$this->add_responsive_control(
			$prefix . 'item_size',
			array(
				'label'     => esc_html__( 'Box Size (WxH)', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 20,
						'max' => 100,
					),
				),
				'selectors' => array(
					$base_sel . ' .page-numbers' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}}; line-height: {{SIZE}}{{UNIT}}; text-align: center;',
				),
			)
		);

		$this->add_responsive_control(
			$prefix . 'item_gap',
			array(
				'label'     => esc_html__( 'Gap Between', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 50,
					),
				),
				'selectors' => array( $base_sel => 'gap: {{SIZE}}{{UNIT}};' ),
			)
		);

		$this->start_controls_tabs( $prefix . 'tabs' );

		// --- NORMAL STATE ---
		$this->start_controls_tab( $prefix . 'normal', array( 'label' => 'Normal' ) );
		$this->add_control(
			$prefix . 'color',
			array(
				'label'     => 'Text Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $base_sel . ' .page-numbers' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			$prefix . 'bg',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $base_sel . ' .page-numbers' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => $prefix . 'border',
				'selector' => $base_sel . ' .page-numbers',
			)
		);
		$this->end_controls_tab();

		// --- HOVER STATE ---
		$this->start_controls_tab( $prefix . 'hover', array( 'label' => 'Hover' ) );
		$this->add_control(
			$prefix . 'color_h',
			array(
				'label'     => 'Text Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $base_sel . ' .page-numbers:hover' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			$prefix . 'bg_h',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $base_sel . ' .page-numbers:hover' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			$prefix . 'border_h',
			array(
				'label'     => 'Border Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $base_sel . ' .page-numbers:hover' => 'border-color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();

		// --- ACTIVE STATE ---
		$this->start_controls_tab( $prefix . 'active', array( 'label' => 'Active' ) );
		$this->add_control(
			$prefix . 'color_a',
			array(
				'label'     => 'Text Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $base_sel . ' .page-numbers.current' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			$prefix . 'bg_a',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $base_sel . ' .page-numbers.current' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			$prefix . 'border_a',
			array(
				'label'     => 'Border Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $base_sel . ' .page-numbers.current' => 'border-color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_responsive_control(
			$prefix . 'radius',
			array(
				'label'     => 'Border Radius',
				'type'      => Controls_Manager::DIMENSIONS,
				'separator' => 'before',
				'selectors' => array( $base_sel . ' .page-numbers' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Render the Numeric Pagination
	 *
	 * @param object $wp_query The WP_Query instance
	 * @param array  $settings Widget settings
	 * @param string $prefix   Control prefix
	 */
	protected function render_sifency_addons_numeric_pagination( $wp_query, $settings, $prefix = 'pagination_' ) {
		if ( 'yes' !== $settings[ $prefix . 'enabled' ] ) {
			return;
		}

		$total_pages = $wp_query->max_num_pages;

		if ( $total_pages > 1 ) {
			$current_page = max( 1, get_query_var( 'paged' ), get_query_var( 'page' ) );

			echo '<nav class="sifency-numeric-pagination">';
			$pagination = paginate_links(
				array(
					'base'      => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
					'format'    => '?paged=%#%',
					'current'   => $current_page,
					'total'     => $total_pages,
					'prev_text' => '<i class="eicon-chevron-left"></i>',
					'next_text' => '<i class="eicon-chevron-right"></i>',
					'type'      => 'plain',
				)
			);
			echo wp_kses_post( $pagination );
			echo '</nav>';
		}
	}
}
