<?php

namespace Prodigy\Includes\Frontend\Mappers;

use Prodigy\Includes\Frontend\Prodigy_Product_Template_Builder;
use Prodigy\Includes\Frontend\Prodigy_Product_Template_Item;
use Prodigy\includes\helpers\Prodigy_Elementor;
use Prodigy\Includes\Helpers\Prodigy_Widget_Options;
use Prodigy\Includes\Support\Addons\Elementor\Widgets\Archive\Prodigy_Products;
use Prodigy\Includes\Support\Customizer\Prodigy_Customizer;

/**
 * Prodigy product image data mapper
 *
 * @version    3.1.2
 * @package    Prodigy
 * @subpackage Prodigy/includes
 */
class Prodigy_Product_Image_Data_Mapper extends Prodigy_Main_Data_Mapper {

	/**
	 * @param   array $options
	 *
	 * @return array
	 */
	public function get_default_parameters( array $options = array() ): array {
		$product_template = new Prodigy_Product_Template_Builder( new Prodigy_Product_Template_Item() );
		$product          = $GLOBALS['prodigy_product']
							?? $product_template->set_product( (int) Prodigy_Product_Template_Builder::get_random_product_id() );

		$attachments = $options['images'] ?? array();
		if ( isset( $product ) ) {
			$product_images      = $product->get_images();
			$post_thumbnail_info = $product_images !== null ? $product_images : $attachments;
		} else {
			$post_thumbnail_info = $attachments;
		}

		$options['attachments']       = $post_thumbnail_info;
		$options['post_thumbnail_id'] = is_array( $post_thumbnail_info ) && ! empty( $post_thumbnail_info )
			? array_shift( $post_thumbnail_info )
			: false;

		$options['is_quick_view']     = $GLOBALS['quick_view'] ?? false;
		$options['gallery_classname'] = $this->get_gallery_classname( $options );

		$options['price'] = isset( $product ) ? $product->get_remote_main_price() : 0;

		$options['image_ratio']     = Prodigy_Customizer::get_images_ratio();
		$thumbnails_setting         = $options['content_thumbnails'] ?? $options['prg_content_thumbnails'] ?? 'yes';
		$options['show_thumbnails'] = Prodigy_Widget_Options::$bool_mapper[ $thumbnails_setting ] ?? true;

		$options['ratio'] = ( $options['style_image_ratio_active_image']['width'] ?? 3 )
							/ ( $options['style_image_ratio_active_image']['height'] ?? 4 );

		$products_widget_id = isset( $_POST['products_widget_id'] ) ? sanitize_key( wp_unslash( $_POST['products_widget_id'] ) ) : '';
		if ( Prodigy_Elementor::is_elementor_enabled() ) {
			$archive_products_settings = Prodigy_Elementor::get_widget_options( ( new Prodigy_Products() )->get_name(), $products_widget_id );
			$sale_setting              = ( is_array( $archive_products_settings ) && isset( $archive_products_settings['content_archive_products_content_sale'] ) )
				? $archive_products_settings['content_archive_products_content_sale']
				: 'no';
			$options['sale_badge']     = Prodigy_Widget_Options::$bool_mapper[ $sale_setting ] ?? false;
		}

		return $options;
	}

	/**
	 * @param   array $options
	 *
	 * @return string
	 */
	private function get_gallery_classname( array $options ): string {
		$classname = 'prodigy-product__gallery-container prodigy-order-2 prodigy-order-md-0';

		if ( ! isset( $options['idWidget'] ) ) {
			return $classname . ' col-md-6';
		}

		switch ( $options['style_thumbnails_position'] ?? '' ) {
			case 'top':
				$classname .= ' prodigy-product__gallery-container--top';
				break;
			case 'left':
				$classname .= ' prodigy-product__gallery-container--left';
				break;
			case 'right':
				$classname .= ' prodigy-product__gallery-container--right';
				break;
		}

		if ( isset( $options['post_thumbnail_id'] ) && ! $options['post_thumbnail_id'] ) {
			$classname .= ' prodigy-product__gallery--no-image';
		}

		return $classname;
	}
}
