<?php
namespace Glozin\Addons\Elementor\Builder\Widgets;

use Glozin\Addons\Elementor\Builder\Current_Query_Renderer;
use Glozin\Addons\Elementor\Builder\Products_Renderer;
use Elementor\Controls_Manager;
use Elementor\Widget_Base;
use Elementor\Group_Control_Typography;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

class Archive_Products extends Widget_Base {

	public function get_name() {
		return 'glozin-archive-products';
	}

	public function get_title() {
		return esc_html__( '[Glozin] Archive Products', 'glozin-addons' );
	}

	public function get_icon() {
		return 'eicon-products';
	}

	public function get_keywords() {
		return [ 'woocommerce', 'shop', 'store', 'archive', 'product' ];
	}

	public function get_categories() {
		return [ 'glozin-addons-archive-product' ];
	}

	protected function register_controls() {
        $this->start_controls_section(
			'section_content',
			[
				'label'     => __( 'Content', 'glozin-addons' ),
			]
		);

		$this->add_control(
			'show_default_view',
			[
				'label' => esc_html__( 'Shop Default View', 'glozin-addons' ),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'list'       => esc_html__( 'List', 'glozin' ),
					'grid'       => esc_html__( 'Grid', 'glozin' ),
				],
				'default' => 'grid',
			]
		);

        $this->add_responsive_control(
			'shop_columns',
			[
				'label'     => esc_html__( 'Shop Grid Columns', 'glozin-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => [
					'2'  => esc_attr__( '2 Columns', 'glozin-addons' ),
					'3'  => esc_attr__( '3 Columns', 'glozin-addons' ),
					'4'  => esc_attr__( '4 Columns', 'glozin-addons' ),
					'5'  => esc_attr__( '5 Columns', 'glozin-addons' ),
					'6'  => esc_attr__( '6 Columns', 'glozin-addons' ),
				],
				'default'   => '3',
			]
		);

		$this->add_control(
			'shop_rows',
			[
				'label'   => esc_html__( 'Shop Grid Rows', 'glozin-addons' ),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 1,
				'default' => 4,
			]
		);

        $this->add_control(
			'pagination',
			[
				'label'     => esc_html__( 'Pagination', 'glozin-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => [
					'numeric'  => esc_attr__( 'Numeric', 'glozin' ),
					'infinite' => esc_attr__( 'Infinite Scroll', 'glozin' ),
					'loadmore' => esc_attr__( 'Load More', 'glozin' ),
				],
				'default'   => 'numeric',
			]
		);

		$this->add_control(
			'loadmore_text',
			[
				'label' => __( 'Text', 'glozin-addons' ),
				'type' => Controls_Manager::TEXT,
				'placeholder' => __( 'Load More', 'glozin-addons' ),
				'label_block' => true,
				'condition' => [
					'pagination' => 'loadmore',
				]
			]
		);

        $this->end_controls_section();

		$this->start_controls_section(
			'section_pagination_style',
			[
				'label' => esc_html__( 'Pagination', 'glozin-addons' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'pagination_spacing_top',
			[
				'label' => __( 'Spacing Top', 'glozin-addons' ),
				'type' => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .woocommerce-pagination' => 'margin-top: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_control(
			'pagination_gap',
			[
				'label' => __( 'Gap', 'glozin-addons' ),
				'type' => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .woocommerce-pagination ul' => 'gap: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'pagination_typography',
				'selector' => '{{WRAPPER}} .woocommerce-pagination a, {{WRAPPER}} .woocommerce-pagination ul .page-numbers',
			]
		);

		$this->add_control(
			'pagination_icon_size',
			[
				'label' => __( 'Icon Size', 'glozin-addons' ),
				'type' => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .woocommerce-pagination ul .page-numbers.prev' => '--gz-button-icon-size: {{SIZE}}{{UNIT}}; font-size: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .woocommerce-pagination ul .page-numbers.next' => '--gz-button-icon-size: {{SIZE}}{{UNIT}}; font-size: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_responsive_control(
			'pagination_min_width',
			[
				'label' => __( 'Min Width', 'glozin-addons' ),
				'type' => Controls_Manager::SLIDER,
				'range'     => [
					'px' => [
						'min' => 0,
						'max' => 500,
					],
				],
				'size_units' => [ 'px', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .woocommerce-pagination.woocommerce-pagination--loadmore .woocommerce-pagination-button' => 'min-width: {{SIZE}}{{UNIT}};',
				],
				'condition' => [
					'pagination' => 'loadmore',
				]
			]
		);

		$this->add_control(
			'pagination_color',
			[
				'label' => esc_html__( 'Color', 'glozin-addons' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .woocommerce-pagination a, {{WRAPPER}} .woocommerce-pagination ul .page-numbers' => '--gz-button-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'pagination_background_color',
			[
				'label' => esc_html__( 'Background Color', 'glozin-addons' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .woocommerce-pagination a, {{WRAPPER}} .woocommerce-pagination ul .page-numbers' => '--gz-button-bg-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'pagination_border_color',
			[
				'label' => esc_html__( 'Border Color', 'glozin-addons' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .woocommerce-pagination a, {{WRAPPER}} .woocommerce-pagination ul .page-numbers' => '--gz-button-border-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'pagination_padding',
			[
				'label'      => __( 'Padding', 'glozin-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em' ],
				'selectors'  => [
					'{{WRAPPER}} .woocommerce-pagination a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					'{{WRAPPER}} .woocommerce-pagination ul .page-numbers' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					'.rtl {{WRAPPER}} .glozin-cart-tracking__badges' => 'padding: {{TOP}}{{UNIT}} {{LEFT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{RIGHT}}{{UNIT}};',
					'.rtl {{WRAPPER}} .woocommerce-pagination ul .page-numbers' => 'padding: {{TOP}}{{UNIT}} {{LEFT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{RIGHT}}{{UNIT}};',
				],
			]
		);

		$this->add_control(
			'pagination_border_radius',
			[
				'label'      => __( 'Border Radius', 'glozin-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em' ],
				'selectors'  => [
					'{{WRAPPER}} .woocommerce-pagination a' => '--gz-button-rounded: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					'{{WRAPPER}} .woocommerce-pagination ul .page-numbers' => '--gz-button-rounded: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					'.rtl {{WRAPPER}} .glozin-cart-tracking__badges' => '--gz-button-rounded: {{TOP}}{{UNIT}} {{LEFT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{RIGHT}}{{UNIT}};',
					'.rtl {{WRAPPER}} .woocommerce-pagination ul .page-numbers' => '--gz-button-rounded: {{TOP}}{{UNIT}} {{LEFT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{RIGHT}}{{UNIT}};',
				],
			]
		);

		$this->add_control(
			'pagination_hover_active_heading',
			[
				'label'     => esc_html__( 'Hover & Active', 'glozin-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);

		$this->add_control(
			'pagination_hover_color',
			[
				'label' => esc_html__( 'Color', 'glozin-addons' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .woocommerce-pagination a' => '--gz-button-color-hover: {{VALUE}};',
					'{{WRAPPER}} .woocommerce-pagination ul .page-numbers.current' => '--gz-button-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'pagination_hover_background_color',
			[
				'label' => esc_html__( 'Background Color', 'glozin-addons' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .woocommerce-pagination a' => '--gz-button-bg-color-hover: {{VALUE}};',
					'{{WRAPPER}} .woocommerce-pagination ul .page-numbers.current' => '--gz-button-bg-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'pagination_hover_border_color',
			[
				'label' => esc_html__( 'Border Color', 'glozin-addons' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .woocommerce-pagination a' => '--gz-button-border-color-hover: {{VALUE}};',
					'{{WRAPPER}} .woocommerce-pagination ul .page-numbers.current' => '--gz-button-border-color: {{VALUE}};',
				],
			]
		);

        $this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$settings['paginate'] = true;

		add_filter( 'glozin_product_card_loop_columns', [ $this, 'get_shop_columns' ] );
		remove_action( 'woocommerce_after_shop_loop', 'woocommerce_pagination' );

		if ( \Glozin\Addons\Elementor\Builder\Helper::is_elementor_editor_mode() ) {
			$shortcode = new Products_Renderer( $settings, 'products' );
		} else {
			add_action( 'woocommerce_after_shop_loop', [ $this, 'pagination' ] );
			$shortcode = new Current_Query_Renderer( $settings, 'current_query' );
		}

		$content = $shortcode->get_content();

		echo '<div id="glozin-shop-content" class="glozin-archive-products glozin-shop-content">';
		if ( $content ) {
			$content = str_replace( '<ul class="products', '<ul class="products products-elementor', $content );

			echo $content;
		} else {
			echo '<div class="elementor-nothing-found glozin-products-nothing-found woocommerce-info">' . esc_html__('No products were found matching your selection.', 'glozin-addons') . '</div>';
			echo '<ul class="products products-elementor columns-'. esc_attr( $this->get_shop_columns() ) .'"></ul>';
		}

		echo '</div>';

		remove_filter( 'glozin_product_card_loop_columns', [ $this, 'get_shop_columns' ] );
		remove_action( 'woocommerce_after_shop_loop', [ $this, 'pagination' ] );
	}

	public function get_shop_columns() {
		$settings = $this->get_settings_for_display();
		$shop_columns = get_option( 'woocommerce_catalog_columns', 4 );
		if ( ! \Glozin\Addons\Elementor\Builder\Helper::is_elementor_editor_mode() ) {
			$shop_columns = isset( $_COOKIE['catalog_view'] ) && $_COOKIE['catalog_view'] == 'list' ? 1 : $shop_columns;
		}
		return $shop_columns;
	}

	/**
	 * Products pagination.
	 */
	public function pagination() {
		$settings = $this->get_settings_for_display();
		$nav_type = apply_filters( 'glozin_product_catalog_pagination_builder', $settings['pagination'] );

		if ( 'numeric' == $nav_type ) {
			woocommerce_pagination();
			return;
		}

		global $wp_query;

		$max_page = $wp_query->max_num_pages;

		if( $max_page <= 1 ) {
			return;
		}


		$classes = array(
			'woocommerce-pagination',
			'woocommerce-pagination--catalog',
			'next-posts-pagination',
			'woocommerce-pagination--ajax',
			'woocommerce-pagination--' . esc_attr( $nav_type ),
			'text-center'
		);

		add_filter( 'next_posts_link_attributes', array( $this, 'glozin_next_posts_link_attributes' ), 10, 1 );

		echo '<nav class="' . esc_attr( implode( ' ', $classes ) ) . '">';
			if ( \Glozin\Addons\Elementor\Builder\Helper::is_elementor_editor_mode() ) {
				?>
					<a href="#" class="woocommerce-pagination-button gz-button gz-button-outline <?php echo $nav_type == 'infinite' ? 'loading' : ''; ?>">
						<span><?php echo ! empty( $settings['loadmore_text'] ) ? $settings['loadmore_text'] : esc_html__( 'Load more', 'glozin' ); ?></span>
					</a>
				<?php
			} else {
				$text = ! empty( $settings['loadmore_text'] ) ? $settings['loadmore_text'] : esc_html__( 'Load more', 'glozin' );
				next_posts_link( '<span>' . $text . '</span>', wc_get_loop_prop( 'total_pages' ) );
			}
		echo '</nav>';
	}

	/**
	 * Next posts link attributes
	 *
	 * @return string $attr
	 */
	public function glozin_next_posts_link_attributes( $attr ) {
		$settings = $this->get_settings_for_display();
		$nav_type = apply_filters( 'glozin_product_catalog_pagination_builder', $settings['pagination'] );
		if( $nav_type !== 'numeric' ) {
			$attr = 'class="woocommerce-pagination-button gz-button gz-button-outline"';
		}

		return $attr;
	}
}