<?php

namespace Prodigy\Includes\Support\Addons\Elementor\Widgets\Archive;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Widget_Base;

if ( ! defined( 'ABSPATH' ) ) {
	die( '-1' );
}

/**
 * @version 2.0.7
 */
class Prodigy_Sorting extends Widget_Base {

	/**
	 * @inheritDoc
	 */
	public function get_name(): string {
		return 'pae-sorting';
	}

	/**
	 * @inheritDoc
	 */
	public function get_title(): string {
		return __( 'Product Sorting', 'prodigy-commerce' );
	}

	/**
	 * @inheritDoc
	 */
	public function get_icon(): string {
		return 'prgicon prgicon-product-sorting';
	}

	/**
	 * @inheritDoc
	 */
	public function get_categories(): array {
		return array( 'prodigy-elements-archive' );
	}

	/**
	 * @inheritDoc
	 */
	protected function register_controls() {
		/*
		* TAB Content
		*/
		$this->start_controls_section(
			'prg_content_sorting',
			array(
				'label' => __( 'Content', 'prodigy-commerce' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'prg_sorting_options',
			array(
				'label'       => __( 'Sorting Options', 'prodigy-commerce' ),
				'type'        => Controls_Manager::REPEATER,
				'render_type' => 'template',
				'fields'      => array(
					array(
						'name'    => 'sort_key',
						'label'   => __( 'Sort Key', 'prodigy-commerce' ),
						'type'    => Controls_Manager::SELECT,
						'default' => '-created_at',
						'options' => array(
							'-created_at' => __( 'Date, new to old', 'prodigy-commerce' ),
							'created_at'  => __( 'Date, old to new', 'prodigy-commerce' ),
							'title'       => __( 'Title, A-Z', 'prodigy-commerce' ),
							'-title'      => __( 'Title, Z-A', 'prodigy-commerce' ),
							'-price'      => __( 'Price, high to low', 'prodigy-commerce' ),
							'price'       => __( 'Price, low to high', 'prodigy-commerce' ),
							'-rating'     => __( 'Rating, high to low', 'prodigy-commerce' ),
							'rating'      => __( 'Rating, low to high', 'prodigy-commerce' ),
						),
					),
					array(
						'name'        => 'label',
						'label'       => __( 'Label', 'prodigy-commerce' ),
						'type'        => Controls_Manager::TEXT,
						'default'     => '',
						'label_block' => true,
						'description' => __( 'Custom label for this sorting option. Leave empty to use default.', 'prodigy-commerce' ),
					),
					array(
						'name'         => 'enabled',
						'label'        => __( 'Enable', 'prodigy-commerce' ),
						'type'         => Controls_Manager::SWITCHER,
						'label_on'     => __( 'Yes', 'prodigy-commerce' ),
						'label_off'    => __( 'No', 'prodigy-commerce' ),
						'return_value' => 'yes',
						'default'      => 'yes',
					),
				),
				'default'     => array(
					array(
						'sort_key' => '-created_at',
						'label'    => '',
						'enabled'  => 'yes',
					),
					array(
						'sort_key' => 'created_at',
						'label'    => '',
						'enabled'  => 'yes',
					),
					array(
						'sort_key' => 'title',
						'label'    => '',
						'enabled'  => 'yes',
					),
					array(
						'sort_key' => '-title',
						'label'    => '',
						'enabled'  => 'yes',
					),
					array(
						'sort_key' => '-price',
						'label'    => '',
						'enabled'  => 'yes',
					),
					array(
						'sort_key' => 'price',
						'label'    => '',
						'enabled'  => 'yes',
					),
					array(
						'sort_key' => '-rating',
						'label'    => '',
						'enabled'  => 'yes',
					),
					array(
						'sort_key' => 'rating',
						'label'    => '',
						'enabled'  => 'yes',
					),
				),
				'title_field' => ( '<# var defaultLabels = {'
								. ' "-created_at": "' . esc_js( __( 'Date, new to old', 'prodigy-commerce' ) ) . '",'
								. ' "created_at": "' . esc_js( __( 'Date, old to new', 'prodigy-commerce' ) ) . '",'
								. ' "title": "' . esc_js( __( 'Title, A-Z', 'prodigy-commerce' ) ) . '",'
								. ' "-title": "' . esc_js( __( 'Title, Z-A', 'prodigy-commerce' ) ) . '",'
								. ' "-price": "' . esc_js( __( 'Price, high to low', 'prodigy-commerce' ) ) . '",'
								. ' "price": "' . esc_js( __( 'Price, low to high', 'prodigy-commerce' ) ) . '",'
								. ' "-rating": "' . esc_js( __( 'Rating, high to low', 'prodigy-commerce' ) ) . '",'
								. ' "rating": "' . esc_js( __( 'Rating, low to high', 'prodigy-commerce' ) ) . '"'
								. ' }; var displayLabel = label || defaultLabels[sort_key] || sort_key; #>{{ displayLabel }} <# if ( enabled !== "yes" ) { #>(' . esc_js( __( 'Disabled', 'prodigy-commerce' ) ) . ')<# } #>' ),
			)
		);

		$this->end_controls_section();

		/*
		* TAB Style - General (registered first so Style tab displays)
		*/
		$this->start_controls_section(
			'prg_style_gen',
			array(
				'label' => __( 'General', 'prodigy-commerce' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'prg_style_alignment',
			array(
				'label'          => __( 'Alignment', 'prodigy-commerce' ),
				'type'           => Controls_Manager::SELECT,
				'default'        => 'left',
				'tablet_default' => 'left',
				'options'        => array(
					'left'   => __( 'Left', 'prodigy-commerce' ),
					'center' => __( 'Center', 'prodigy-commerce' ),
					'right'  => __( 'Right', 'prodigy-commerce' ),
				),
				'selectors'      => array(
					'{{WRAPPER}} .jq-selectbox__select' => 'text-align: {{VALUE}} !important',
					'{{WRAPPER}} .jq-selectbox__select-text' => 'text-align: {{VALUE}} !important',
					'{{WRAPPER}} .prodigy-sort-button'  => 'text-align: {{VALUE}} !important',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'           => 'prg_style_sorting_typography',
				'label'          => __( 'Typography', 'prodigy-commerce' ),
				'selector'       => '{{WRAPPER}} .prodigy-sort, {{WRAPPER}} .prodigy-sort-button, {{WRAPPER}} .prodigy-custom-template .prodigy-custom-select.jq-selectbox .jq-selectbox__select .jq-selectbox__select-text',
				'fields_options' => array(
					'typography'     => array( 'default' => 'yes' ),
					'font_size'      => array(
						'default'        => array(
							'unit' => 'px',
							'size' => 15,
						),
						'tablet_default' => array(
							'unit' => 'px',
							'size' => 15,
						),
						'mobile_default' => array(
							'unit' => 'px',
							'size' => 15,
						),
					),
					'font_weight'    => array(
						'default' => 400,
					),
					'line_height'    => array(
						'default'        => array(
							'unit' => 'em',
							'size' => 1.8,
						),
						'tablet_default' => array(
							'unit' => 'em',
							'size' => 1.8,
						),
						'mobile_default' => array(
							'unit' => 'em',
							'size' => 1.8,
						),
					),
					'letter_spacing' => array(
						'default'        => array(
							'unit' => 'px',
							'size' => 0,
						),
						'tablet_default' => array(
							'unit' => 'px',
							'size' => 0,
						),
						'mobile_default' => array(
							'unit' => 'px',
							'size' => 0,
						),
					),
					'word_spacing'   => array(
						'default'        => array(
							'unit' => 'em',
							'size' => 0,
						),
						'tablet_default' => array(
							'unit' => 'em',
							'size' => 0,
						),
						'mobile_default' => array(
							'unit' => 'em',
							'size' => 0,
						),
					),
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'           => 'prg_style_sorting_mobile_title_typography',
				'label'          => __( 'Mobile Title Typography', 'prodigy-commerce' ),
				'selector'       => '{{WRAPPER}} .prodigy-custom-template .prodigy-select-md .prodigy-select-md__header .prodigy-select-md__header-title',
				'fields_options' => array(
					'typography'     => array( 'default' => 'yes' ),
					'font_size'      => array(
						'default'        => array(
							'unit' => 'px',
							'size' => 20,
						),
						'tablet_default' => array(
							'unit' => 'px',
							'size' => 20,
						),
						'mobile_default' => array(
							'unit' => 'px',
							'size' => 20,
						),
					),
					'font_weight'    => array( 'default' => 400 ),
					'line_height'    => array(
						'default'        => array(
							'unit' => 'em',
							'size' => 1.2,
						),
						'tablet_default' => array(
							'unit' => 'em',
							'size' => 1.2,
						),
						'mobile_default' => array(
							'unit' => 'em',
							'size' => 1.2,
						),
					),
					'letter_spacing' => array(
						'default'        => array(
							'unit' => 'px',
							'size' => 0,
						),
						'tablet_default' => array(
							'unit' => 'px',
							'size' => 0,
						),
						'mobile_default' => array(
							'unit' => 'px',
							'size' => 0,
						),
					),
					'word_spacing'   => array(
						'default'        => array(
							'unit' => 'em',
							'size' => 0,
						),
						'tablet_default' => array(
							'unit' => 'em',
							'size' => 0,
						),
						'mobile_default' => array(
							'unit' => 'em',
							'size' => 0,
						),
					),
				),
			)
		);

		$this->add_control(
			'prg_style_sorting_dropdown_width',
			array(
				'label'      => __( 'Dropdown Width', 'prodigy-commerce' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'default'    => array(
					'size' => 100,
					'unit' => '%',
				),
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-template .prodigy-custom-select.jq-selectbox .jq-selectbox__dropdown' => 'width: {{SIZE}}{{UNIT}} !important',
				),
			)
		);

		$this->add_control(
			'prg_style_sorting_color',
			array(
				'label'      => __( 'Text Color', 'prodigy-commerce' ),
				'type'       => Controls_Manager::COLOR,
				'input_type' => 'color',
				'default'    => '#666666',
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-sort' => 'color: {{VALUE}}',
				),
			)
		);

		$this->add_control(
			'prg_style_sorting_border_width',
			array(
				'label'      => __( 'Border Width', 'prodigy-commerce' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px' ),
				'default'    => array(
					'top'    => '0',
					'right'  => '0',
					'bottom' => '0',
					'left'   => '0',
					'unit'   => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-select .jq-selectbox__select' => 'border-width: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}',
				),
			)
		);

		$this->add_control(
			'prg_style_sorting_border_radius',
			array(
				'label'      => __( 'Border Radius', 'prodigy-commerce' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'default'    => array(
					'size' => 0,
					'unit' => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-select .jq-selectbox__select' => 'border-radius: {{SIZE}}{{UNIT}}',
				),
			)
		);

		$this->add_control(
			'prg_style_sorting_color_border',
			array(
				'label'      => __( 'Border Color', 'prodigy-commerce' ),
				'type'       => Controls_Manager::COLOR,
				'input_type' => 'color',
				'default'    => 'transparent',
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-select .jq-selectbox__select' => 'border-color: {{VALUE}}',
				),
			)
		);

		$this->add_control(
			'prg_style_sorting_border_width_focus',
			array(
				'label'      => __( 'Focus Border Width', 'prodigy-commerce' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px' ),
				'default'    => array(
					'top'    => '0',
					'right'  => '0',
					'bottom' => '0',
					'left'   => '0',
					'unit'   => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-select.focused .jq-selectbox__select' => 'border-width: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}',
				),
			)
		);

		$this->add_control(
			'prg_style_sorting_color_border_focus',
			array(
				'label'      => __( 'Focus Border Color', 'prodigy-commerce' ),
				'type'       => Controls_Manager::COLOR,
				'input_type' => 'color',
				'default'    => 'transparent',
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-select.focused .jq-selectbox__select' => 'border-color: {{VALUE}}',
				),
			)
		);

		$this->add_control(
			'prg_style_sorting_border_radius_focus',
			array(
				'label'      => __( 'Focus Border Radius', 'prodigy-commerce' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'default'    => array(
					'size' => 0,
					'unit' => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-select.focused .jq-selectbox__select' => 'border-radius: {{SIZE}}{{UNIT}}',
				),
			)
		);

		$this->start_controls_tabs( 'prg_style_sorting_tabs' );

		$this->start_controls_tab( 'prg_style_sorting_tab_selected', array( 'label' => __( 'Selected', 'prodigy-commerce' ) ) );
		$this->add_control(
			'prg_style_sorting_option_color_selected',
			array(
				'label'      => __( 'Option Color', 'prodigy-commerce' ),
				'type'       => Controls_Manager::COLOR,
				'input_type' => 'color',
				'default'    => '#000000',
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-select .jq-selectbox__dropdown li.selected' => 'color: {{VALUE}}',
				),
			)
		);
		$this->add_control(
			'prg_style_sorting_option_bg_color_selected',
			array(
				'label'      => __( 'Option Background', 'prodigy-commerce' ),
				'type'       => Controls_Manager::COLOR,
				'input_type' => 'color',
				'default'    => '#cccccc',
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-select .jq-selectbox__dropdown li.selected' => 'background-color: {{VALUE}}',
				),
			)
		);
		$this->end_controls_tab();

		$this->start_controls_tab( 'prg_style_sorting_tab_hover', array( 'label' => __( 'Hover', 'prodigy-commerce' ) ) );
		$this->add_control(
			'prg_style_sorting_option_color_hover',
			array(
				'label'      => __( 'Option Color', 'prodigy-commerce' ),
				'type'       => Controls_Manager::COLOR,
				'input_type' => 'color',
				'default'    => '#ffffff',
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-select .jq-selectbox__dropdown li:not(.selected):hover' => 'color: {{VALUE}}',
				),
			)
		);
		$this->add_control(
			'prg_style_sorting_option_bgcolor_hover',
			array(
				'label'      => __( 'Option Background', 'prodigy-commerce' ),
				'type'       => Controls_Manager::COLOR,
				'input_type' => 'color',
				'default'    => '#0088cc',
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-select .jq-selectbox__dropdown li:hover' => 'background-color: {{VALUE}}',
				),
			)
		);
		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_responsive_control(
			'prg_style_gen_bottom_margin',
			array(
				'label'          => __( 'Bottom Margin', 'prodigy-commerce' ),
				'type'           => Controls_Manager::SLIDER,
				'size_units'     => array( 'px' ),
				'default'        => array(
					'size' => 0,
					'unit' => 'px',
				),
				'tablet_default' => array(
					'size' => 0,
					'unit' => 'px',
				),
				'mobile_default' => array(
					'size' => 0,
					'unit' => 'px',
				),
				'selectors'      => array(
					'{{WRAPPER}} .prodigy-sort' => 'margin-bottom: {{SIZE}}{{UNIT}}',
				),
			)
		);

		$this->add_control(
			'prg_sorting_style_arrow_button_width',
			array(
				'label'      => __( 'Arrow Button Right', 'prodigy-commerce' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'default'    => array(
					'size' => 0,
					'unit' => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-template .jq-selectbox__trigger-arrow:before' => 'right: {{SIZE}}{{UNIT}}',
				),
			)
		);
		$this->add_control(
			'prg_sorting_style_arrow_button_height',
			array(
				'label'      => __( 'Arrow Button Top', 'prodigy-commerce' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'default'    => array(
					'size' => 24,
					'unit' => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-select .jq-selectbox__trigger-arrow' => 'height: {{SIZE}}{{UNIT}}',
				),
			)
		);
		$this->add_control(
			'prg_sorting_style_arrow_button_font_size',
			array(
				'label'      => __( 'Arrow Font Size', 'prodigy-commerce' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'default'    => array(
					'size' => 10,
					'unit' => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-select .jq-selectbox__trigger-arrow:before' => 'height: {{SIZE}}{{UNIT}} !important; width: {{SIZE}}{{UNIT}} !important',
				),
			)
		);
		$this->add_control(
			'prg_sorting_style_arrow_button_font_weight',
			array(
				'label'      => __( 'Arrow Font Weight', 'prodigy-commerce' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'default'    => array(
					'size' => 1,
					'unit' => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .prodigy-custom-select .jq-selectbox__trigger-arrow:before' => 'border-top-width: {{SIZE}}{{UNIT}} !important; border-left-width: {{SIZE}}{{UNIT}} !important',
				),
			)
		);

		$this->start_controls_tabs( 'prg_attrs_style_arrow_button_tabs' );
		$this->start_controls_tab( 'prg_attrs_style_arrow_button_tab_normal', array( 'label' => __( 'Normal', 'prodigy-commerce' ) ) );
		$this->add_control(
			'prg_sorting_style_arrow_button_color',
			array(
				'label'     => __( 'Arrow Color', 'prodigy-commerce' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#57617f',
				'selectors' => array(
					'{{WRAPPER}} .prodigy-custom-select .jq-selectbox__trigger-arrow:before' => 'border-color: {{VALUE}} !important',
				),
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab( 'prg_sorting_style_arrow_button_tab_focus', array( 'label' => __( 'Focus', 'prodigy-commerce' ) ) );
		$this->add_control(
			'prg_sorting_style_arrow_button_color_focus',
			array(
				'label'     => __( 'Arrow Color', 'prodigy-commerce' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#3a3a3a',
				'selectors' => array(
					'{{WRAPPER}} .opened .jq-selectbox__trigger-arrow:before' => 'border-color: {{VALUE}} !important',
				),
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->end_controls_section();
	}

	/**
	 * @inheritDoc
	 */
	protected function render() {
		$settings = $this->get_settings_for_display();
		if ( empty( trim( $settings['style_sorting_text'] ?? '' ) ) ) {
			$settings['style_sorting_text'] = 'Showing';
		}

		$settings['idWidget'] = $this->get_id();

		do_action( 'prodigy_shop_sortable_block', $settings );
	}

	/**
	 * Render widget output in the editor.
	 *
	 * @return void
	 */
	protected function content_template() {
		?>
		<#
		var settings = settings;
		if ( ! settings.style_sorting_text ) {
			settings.style_sorting_text = 'Showing';
		}
		settings.idWidget = view.getID();
		#>
		<div class="elementor-widget-container">
			<div class="prodigy-sort catalog-sort-js prodigy-catalog-sort d-flex flex-nowrap justify-content-between justify-content-md-end prodigy-custom-template" data-width="200">
				<button class="prodigy-filter-toggle-btn-2 prodigy-filter__sm-btn prodigy-filter__sm-btn-default" style="display: none;">
					<span class="icon icon-filter" role="presentation"></span>
					<?php esc_html_e( 'Filter', 'prodigy-commerce' ); ?>
				</button>
				<select id="sort-select-id-{{ view.getID() }}" class="prodigy-custom-select prodigy-custom-select__catalog-sort catalog-page-sort-js">
					<# if ( settings.prg_sorting_options && settings.prg_sorting_options.length ) {
						var defaultLabels = {
							'-created_at': '<?php echo esc_js( __( 'Date, new to old', 'prodigy-commerce' ) ); ?>',
							'created_at': '<?php echo esc_js( __( 'Date, old to new', 'prodigy-commerce' ) ); ?>',
							'title': '<?php echo esc_js( __( 'Title, A-Z', 'prodigy-commerce' ) ); ?>',
							'-title': '<?php echo esc_js( __( 'Title, Z-A', 'prodigy-commerce' ) ); ?>',
							'-price': '<?php echo esc_js( __( 'Price, high to low', 'prodigy-commerce' ) ); ?>',
							'price': '<?php echo esc_js( __( 'Price, low to high', 'prodigy-commerce' ) ); ?>',
							'-rating': '<?php echo esc_js( __( 'Rating, high to low', 'prodigy-commerce' ) ); ?>',
							'rating': '<?php echo esc_js( __( 'Rating, low to high', 'prodigy-commerce' ) ); ?>'
						};
						_.each( settings.prg_sorting_options, function( option ) {
							if ( option.enabled === 'yes' ) {
								var label = option.label || defaultLabels[ option.sort_key ] || option.sort_key;
								#>
								<option value="{{ option.sort_key }}">{{ label }}</option>
								<#
							}
						} );
					} else {
						#>
						<option value="-created_at"><?php echo esc_js( __( 'Date, new to old', 'prodigy-commerce' ) ); ?></option>
						<option value="created_at"><?php echo esc_js( __( 'Date, old to new', 'prodigy-commerce' ) ); ?></option>
						<option value="title"><?php echo esc_js( __( 'Title, A-Z', 'prodigy-commerce' ) ); ?></option>
						<option value="-title"><?php echo esc_js( __( 'Title, Z-A', 'prodigy-commerce' ) ); ?></option>
						<option value="-price"><?php echo esc_js( __( 'Price, high to low', 'prodigy-commerce' ) ); ?></option>
						<option value="price"><?php echo esc_js( __( 'Price, low to high', 'prodigy-commerce' ) ); ?></option>
						<option value="-rating"><?php echo esc_js( __( 'Rating, high to low', 'prodigy-commerce' ) ); ?></option>
						<option value="rating"><?php echo esc_js( __( 'Rating, low to high', 'prodigy-commerce' ) ); ?></option>
						<#
					}
					#>
				</select>
			</div>
		</div>
		<?php
	}
}
