<?php
namespace Prodigy\Includes\Frontend\Mappers;

use Prodigy\Includes\Helpers\Prodigy_Formatting;
use Prodigy\includes\helpers\Prodigy_Elementor_Layouts;
use Prodigy\Includes\Helpers\Prodigy_Url_Helper;
use Prodigy\Includes\Helpers\Prodigy_Widget_Options;
use Prodigy\Includes\Models\Prodigy_Attribute_Taxonomies;

/**
 * Prodigy filters data mapper
 *
 * @version    2.8.0
 * @package    Prodigy
 * @subpackage Prodigy/includes
 */
class Prodigy_Active_Filter_Data_Mapper extends Prodigy_Main_Data_Mapper {

	/**
	 * @param array $options
	 *
	 * @return array
	 */
	public function get_default_parameters( array $options ): array {
		$args_request = self::get_all_values_attr();
		$params       = array(
			'args_request'        => $args_request,
			'attr_shortcode'      => $options,
			'show_attribute_name' => $options['show_attribute_name'] ?? '',
		);

		$params['idwidget']                = $options['attr_shortcode']['idwidget'] ?? '';
		$params['heading_active_filter']   = $options['attr_shortcode']['active_filters_style_heading_text'] ?? 'Active Filter';
		$params['current_url']             = Prodigy_Url_Helper::prodigy_parse_request_uri();
		$params['path_clear']              = $params['current_url']['path'] ?? '/';
		$params['customizer_shop_options'] = $this->customizer_shop_options;
		$params['elementor_conditions']    = get_option( 'elementor_pro_theme_builder_conditions' );
		$params['filter_nonce']            = $this->filter_nonce;

		if ( Prodigy_Elementor_Layouts::is_elementor_live_preview() || Prodigy_Elementor_Layouts::is_elementor_template() ) {
			$params['is_show_active_filter_name'] = isset( $options['attr_shortcode']['active_filters_show_attribute_name'] ) && Prodigy_Widget_Options::$bool_mapper[ $options['attr_shortcode']['active_filters_show_attribute_name'] ];
		} else {
			$params['is_show_active_filter_name'] = $options['show_attribute_name'] ?? '';
		}

		return $params;
	}

	/**
	 * @param array $list
	 *
	 * @return array
	 */
	public function flattenArray( array $list ): array {
		$result = array();

		foreach ( $list as $key => $value ) {
			$this->flattenSubArray( $result, $key, $value );
		}

		return $result;
	}

	/**
	 * @param array $result
	 * @param $key
	 * @param $value
	 *
	 * @return void
	 */
	private function flattenSubArray( array &$result, $key, $value ) {
		if ( is_array( $value ) ) {
			foreach ( $this->flattenArray( $value ) as $nestedKey => $nestedValue ) {
				$result[ $nestedKey ] = $nestedValue;
			}
		} else {
			$result[ $key ] = $value;
		}
	}

	private static function sanitize_array( array $input ): array {
		return array_map( 'sanitize_text_field', $input );
	}

	/**
	 * @return array
	 */
	public static function get_all_values_attr(): array {

		if ( isset( $_GET['_wpnonce'] ) && ! wp_verify_nonce( sanitize_key( wp_unslash( $_GET['_wpnonce'] ) ), 'filter-nonce' ) ) {
			return array();
		}

		$attr      = isset( $_GET['attr'] ) ? self::sanitize_array( $_GET['attr'] ) : array();
		$price_min = isset( $_GET['price_min'] ) ? sanitize_text_field( wp_unslash( $_GET['price_min'] ) ) : null;
		$price_max = isset( $_GET['price_max'] ) ? sanitize_text_field( wp_unslash( $_GET['price_max'] ) ) : null;

		if ( ! isset( $attr ) && ! isset( $price_min, $price_max ) ) {
			return array();
		}

		$result = array();
		if ( isset( $attr ) || isset( $price_min, $price_max ) ) {
			$get_list_args = $attr;
			if ( isset( $price_min, $price_max ) ) {
				$get_list_args['Price'] = get_option( 'pg_currency_type' ) . sanitize_text_field( Prodigy_Formatting::prodigy_price_format( $price_min ) ) . ' - ' . get_option( 'pg_currency_type' ) . sanitize_text_field( Prodigy_Formatting::prodigy_price_format( $price_max ) );
			}

			foreach ( $get_list_args as $id => $value ) {
				$id        = sanitize_text_field( $id );
				$attribute = Prodigy_Attribute_Taxonomies::get_by_remote_id( (int) $id );
				$value     = sanitize_text_field( $value );
				$values    = explode( ';', $value );

				foreach ( $values as $key => $value_name ) {
					$term = get_term_by( 'slug', $value_name, $attribute->slug ?? '' );
					$result[ $attribute->name ?? '' ][ $id ][ $key ]['name'] = sanitize_text_field( $term->name ?? $value_name );
					$result[ $attribute->name ?? '' ][ $id ][ $key ]['slug'] = sanitize_text_field( $value_name );
				}
			}

			return $result;
		}
	}

	/**
	 * Get elementor settings for widget
	 *
	 * @return false|mixed|null
	 */
	public static function get_widget_settings() {
		if ( Prodigy_Elementor_Layouts::is_elementor_live_preview() || Prodigy_Elementor_Layouts::is_elementor_template() ) {
			$widget_id = isset( $_GET['filter_widget_id'] ) ? sanitize_key( wp_unslash( $_GET['filter_widget_id'] ) ) : '';

			return get_option( $widget_id );
		}

		return unserialize( get_option( 'prodigy_active_shortcode_settings' ) );
	}
}
