<?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_Platform_Settings;
use Prodigy\Includes\Helpers\Prodigy_Widget_Options;
use Prodigy\includes\models\Prodigy_Product_Attributes;
use Prodigy\Includes\Prodigy_Elementor_Template_Loader;

/**
 * Prodigy cart data mapper
 *
 * @version    3.0.4
 * @package    Prodigy
 * @subpackage Prodigy/includes
 */
class Prodigy_Product_Add_To_Cart_Data_Mapper extends Prodigy_Main_Data_Mapper {

	const DEFAULT_ADD_TO_CART_MESSAGE = 'has been added to your cart.';
	/** @var Prodigy_Product_Template_Builder */
	public Prodigy_Product_Template_Builder $product_template;

	/**
	 * @param  Prodigy_Product_Template_Builder $product_template
	 */
	public function __construct( Prodigy_Product_Template_Builder $product_template ) {
		$this->product_template = $product_template;
	}

	/**
	 * @param  array $options
	 *
	 * @return array
	 */
	public function get_default_parameters( array $options ): array {
		$parameters                                   = array();
		$product                                      = $this->get_product();
		$parameters['subscriptions']                  = $product->get_subscriptions();
		$parameters['price']                          = $product->get_remote_main_price();
		$parameters['personalizations']               = $product->get_personalizations_fields();
		$parameters['personalization_id']             = $parameters['personalizations']['personalization_id'] ?? null;
		$parameters['main_price']                     = $parameters['price'] ?? 0;
		$parameters['stock_status']                   = $product->get_remote_master_variant_info()['inventory']['attributes']['stock'] ?? 'in_stock';
		$parameters['content_truncate_chars']         = $this->get_content_truncate_chars( $options );
		$parameters['content_description_show']       = $options['prg_content_description_show'] ?? '';
		$parameters['style_anchor_type']              = $options['prg_style_anchor_type'] ?? '';
		$parameters['is_elementor_show_price']        = $options['general_add_to_cart_show_price'] ?? '';
		$parameters['is_elementor_show_availability'] = $options['general_add_to_cart_show_availability'] ?? '';
		$parameters['confirm_icon_type']              = $this->get_confirm_icon_type( $options );
		[
			$parameters['confirm_icon_class'],
			$parameters['confirm_svg_class'],
		]                                       = $this->get_confirm_icon_classes( $options );
		$parameters['is_quick_view']            = $GLOBALS['quick_view'] ?? false;
		$parameters['add_to_cart_message']      = $options['product_adding_confirmation_message_text'] ?? self::DEFAULT_ADD_TO_CART_MESSAGE;
		$parameters['additional_data_string']   = $this->get_additional_data_string( $product );
		$parameters['is_show_view_cart_button'] = $this->should_show_view_cart_button( $options );
		$parameters['is_redemption_store']      = Prodigy_Platform_Settings::is_redemption_store_status();
		$parameters['product']                  = $product;

		return $parameters;
	}

	/**
	 * @return mixed|Prodigy_Product_Template_Item
	 */
	private function get_product() {
		return $GLOBALS['prodigy_product'] ?? $this->product_template->set_product( (int) Prodigy_Product_Template_Builder::get_random_product_id() );
	}

	/**
	 * @param  array $options
	 *
	 * @return string
	 */
	private function get_content_truncate_chars( array $options ): string {
		if ( isset( $options['prg_content_description_truncate_chars']['size'] ) ) {
			return $options['prg_content_description_truncate_chars']['size'] + 3;
		}

		return '';
	}

	/**
	 * @param  array $options
	 *
	 * @return string
	 */
	private function get_confirm_icon_type( array $options ): string {
		if ( ! empty( $options['product_subscription_confirmation_message_icon']['value']['url'] ) ) {
			return 'svg';
		}

		return 'icon';
	}

	/**
	 * @param  array $options
	 *
	 * @return array
	 */
	private function get_confirm_icon_classes( array $options ): array {
		$confirmIconClass = 'icon icon-check';
		if ( isset( $options['product_subscription_confirmation_message_icon']['value'] ) ) {
			$iconValue = $options['product_subscription_confirmation_message_icon']['value'];
			if ( is_string( $iconValue ) ) {
				$confirmIconClass = $iconValue;
			} elseif ( is_array( $iconValue ) ) {
				$confirmIconClass = 'icon icon-check';
			}
		}
		$confirmSvgClass = 'icon-img';
		if ( isset( $options['product_subscription_confirmation_message_icon_position'] ) &&
			$options['product_subscription_confirmation_message_icon_position'] === 'right' ) {
			$confirmIconClass .= ' order-last';
			$confirmSvgClass  .= ' order-last';
		}

		return array( $confirmIconClass, $confirmSvgClass );
	}

	/**
	 * @param $product
	 *
	 * @return string
	 */
	private function get_additional_data_string( $product ): string {
		if ( ! empty( $product->get_remote_variants() ) ) {
			return 'Select ' . Prodigy_Product_Attributes::concat_attribute_names( $product->get_variant_options() );
		}

		return '';
	}

	/**
	 * @param  array $options
	 *
	 * @return bool
	 */
	private function should_show_view_cart_button( array $options ): bool {
		$isUseProductTemplate       = Prodigy_Elementor_Template_Loader::is_use_product_template();
		$viewCartShow               = isset( $options['attributes_view_cart_show'] ) ? Prodigy_Widget_Options::$bool_mapper[ $options['attributes_view_cart_show'] ] : false;
		$customizer_product_options = get_option( 'prodigy_product_settings' );
		$customizer_option          = $customizer_product_options['prodigy_product_view_cart'] ?? false;

		return $isUseProductTemplate
			? $viewCartShow
			: $customizer_option;
	}
}
