<?php

namespace Prodigy\Includes\Frontend\Mappers;

defined( 'ABSPATH' ) || exit;

use Prodigy\Includes\Content\Prodigy_Main_Content;
use Prodigy\Includes\Content\Prodigy_Request_Maker;
use Prodigy\Includes\Frontend\Traits\Prodigy_Mlc_Charge_Trait;
use Prodigy\Includes\Frontend\Prodigy_Product_Template_Builder;
use Prodigy\includes\models\Prodigy_Order;
use Prodigy\Includes\Prodigy_Cache;
use Prodigy\Includes\Prodigy_User;

abstract class Prodigy_Main_Data_Mapper {

	use Prodigy_Mlc_Charge_Trait;

	/**
	 * Widget display type
	 */
	const SLIDER = 'slider';

	/**
	 * Widget display type
	 */
	const GRID = 'grid';

	/** @var Prodigy_Cache */
	public Prodigy_Cache $cache;
	/**
	 * @var Prodigy_Main_Content
	 */
	public Prodigy_Main_Content $main_content;
	/**
	 * @var Prodigy_Product_Template_Builder
	 */
	protected Prodigy_Product_Template_Builder $product;
	/**
	 * @var false|mixed|null
	 */
	protected $customizer_shop_options;
	/**
	 * @var false|mixed|null
	 */
	protected $customizer_product_options;
	/** @var false|string */
	protected $filter_nonce;
	/** @var false|string */
	protected $buy_now_nonce;
	/**
	 * @var string|null
	 */
	protected ?string $cart_user_cookie;
	/** @var Prodigy_Order $order_model */
	protected Prodigy_Order $order_model;
	/** @var string */
	protected $order_token;
	/** @var Prodigy_Request_Maker */
	protected Prodigy_Request_Maker $request_maker;

	public function __construct() {
		$this->buy_now_nonce    = wp_create_nonce( 'buy-now' );
		$this->filter_nonce     = wp_create_nonce( 'filter-nonce' );
		$this->main_content     = new Prodigy_Main_Content();
		$this->order_model      = new Prodigy_Order();
		$this->request_maker    = Prodigy_Request_Maker::get_instance();
		$this->cart_user_cookie = Prodigy_User::get_user_session();
		if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'buy-now' ) ) {
			$this->order_token = sanitize_text_field( wp_unslash( $_GET['order_token'] ?? null ) );
		} else {
			$this->order_token = ! empty( $this->cart_user_cookie ) ? $this->order_model->get_order_token( $this->cart_user_cookie ) : null;
		}
		$this->customizer_shop_options    = get_option( 'prodigy_shop_settings' );
		$this->customizer_product_options = get_option( 'prodigy_product_settings' );
	}

	/**
	 * @param   string $sort
	 *
	 * @return string
	 */
	public static function mapping_sort_params( string $sort ): string {
		switch ( $sort ) {
			case 'id':
				return 'id';
			case 'title':
				return 'name';
			case 'price':
				return 'price';
			case 'date':
				return 'created_at';
			case 'rating':
				return 'rating';
			case 'in_stock':
				return 'In stock';
			case 'out_of_stock':
				return 'Out of stock';
			default:
				return 'id';
		}
	}

	/**
	 * @param   array $prepared_params
	 * @param   array $options
	 *
	 * @return string
	 */
	public function build_query_params( array $prepared_params, array $options ): string {
		$query_params = $this->main_content->set_sort_products_query_parameter( $prepared_params, $options );
		$category     = $this->main_content->get_catalog_category_param( $prepared_params['tax_slug'] ?? '', $prepared_params['tax_name'] ?? '' );

		if ( ! empty( $category ) ) {
			$query_params = $category . '&' . $query_params;
		}

		return $query_params;
	}

	/**
	 * @param   array $options
	 *
	 * @return array
	 */
	abstract public function get_default_parameters( array $options ): array;

	/**
	 * @param   array $params
	 *
	 * @return array
	 */
	protected function clear_catalog_params( array $params ): array {
		if ( isset( $params['action'], $params['_'] ) ) {
			unset( $params['action'], $params['_'] );
		}
		if ( isset( $params['tax_slug'], $params['tax_name'] ) ) {
			unset( $params['tax_slug'], $params['tax_name'] );
		}

		return $params;
	}
}
