<?php
namespace Prodigy\includes\helpers;

/**
 * Prodigy elementor helper.
 */
class Prodigy_Elementor {
	const ELEMENTOR_QUICK_VIEW_OPTION = 'content_archive_products_content_quick_view';
	const ELEMENTOR_RATING_OPTION     = 'content_archive_products_content_rating';
	const ELEMENTOR_SALE_OPTION       = 'content_archive_products_content_sale';

	const OPTION_NAME              = 'pg_elementor_';
	const BUILDER_SAVE_ACTION_NAME = 'save_builder';


	/**
	 * @return int|null
	 */
	public static function is_elementor_enabled(): ?int {
		return did_action( 'elementor/loaded' );
	}

	/**
	 * @return bool
	 */
	public static function is_editor_mode(): bool {
		if ( isset( $_GET['action'] ) && $_GET['action'] === 'elementor' ) {
			return true;
		}

		return false;
	}

	/**
	 * @param array $post
	 *
	 * @return bool
	 */
	public static function is_save_request( array $post ): bool {
		$result = false;
		if ( $_SERVER['REQUEST_METHOD'] === 'POST' && isset( $post['actions'] ) ) {
			$actions = stripslashes( $post['actions'] );
			if ( is_string( $actions ) ) {
				$actions = json_decode( $actions, true );

				$result = isset( $actions[ key( $actions ) ]['action'] ) && $actions[ key( $actions ) ]['action'] === self::BUILDER_SAVE_ACTION_NAME;
			}
		}

		return $result;
	}

	/**
	 * @param string $key
	 * @param string $id
	 * @param array  $settings
	 *
	 * @return void
	 */
	public static function set_widget_options( string $key, string $id, array $settings ): void {
		if ( self::is_elementor_enabled() ) {
			if ( self::is_save_request( $_POST ) || ! self::get_widget_options( $key, $id ) ) {
				update_option( self::OPTION_NAME . $key . '_' . $id, $settings );
			}
		}
	}

	/**
	 * @param string $key
	 * @param string $id
	 *
	 * @return false|mixed|null
	 */
	public static function get_widget_options( string $key, string $id = '' ) {
		global $wpdb;
		$options = false;
		if ( ! empty( $id ) ) {
			$options = get_option( self::OPTION_NAME . $key . '_' . $id );
		} else {
			$search_key   = self::OPTION_NAME . $key;
			$like_pattern = $wpdb->esc_like( $search_key ) . '%';

			$option_value = $wpdb->get_var(
				$wpdb->prepare(
					"SELECT option_value
					 FROM $wpdb->options
					 WHERE option_name LIKE %s
					 LIMIT 1",
					$like_pattern
				)
			);

			if ( ! is_null( $option_value ) ) {
				$options = maybe_unserialize( $option_value );
			}
		}

		return $options;
	}

	/**
	 * @return false|mixed|null
	 */
	public static function get_sorted_product_attributes() {
		return get_option( 'prodigy_sorted_product_attributes' );
	}

	/**
	 * @param   array $array_to_sort
	 * @param   array $sorted_attributes
	 *
	 * @return bool
	 */
	public static function has_at_least_two_keys( array $array_to_sort, array $sorted_attributes ): bool {
		$matchingKeys = array_intersect( $sorted_attributes, array_keys( $array_to_sort ) );

		return count( $matchingKeys ) >= 2;
	}


	/**
	 * @param   array $array_to_sort
	 * @param   array $sorted_attributes
	 *
	 * @return array
	 */
	public static function sort_attributes( array $array_to_sort, array $sorted_attributes ): array {
		if ( ! self::has_at_least_two_keys( $array_to_sort, $sorted_attributes ) ) {
			return $array_to_sort;
		}

		$sorted_array = array();
		foreach ( $sorted_attributes as $key ) {
			if ( array_key_exists( $key, $array_to_sort ) ) {
				$sorted_array[ $key ] = $array_to_sort[ $key ];
			}
		}

		foreach ( $array_to_sort as $key => $value ) {
			if ( ! array_key_exists( $key, $sorted_array ) ) {
				$sorted_array[ $key ] = $value;
			}
		}

		return $sorted_array;
	}

	/**
	 * @param array $settings
	 *
	 * @return array
	 */
	public static function clear_widget_params( array $settings ): array {
		$attr = array();
		foreach ( $settings as $key => $setting ) {
			if ( ! is_array( $setting ) && strpos( $key, 'prg_' ) === 0 ) {
				$attr[ str_replace( 'prg_', '', $key ) ] = $setting;
			}
		}

		return $attr;
	}
}
