<?php
/**
 * Thank you page layout type.
 *
 * @package Woodmart
 */

namespace XTS\Modules\Layouts;

use Automattic\WooCommerce\Internal\Utilities\Users;
use WC_Order;
use WC_Product_Factory;

/**
 * Thank you page layout type class.
 */
class Thank_You_Page extends Layout_Type {
	/**
	 * Check.
	 *
	 * @param array  $condition Condition.
	 * @param string $type      Layout type.
	 */
	public function check( $condition, $type = '' ) {
		global $post, $order;

		$order = wc_get_order( get_query_var( 'order-received' ) ); // phpcs:ignore.

		if ( ! is_a( $order, 'WC_Order' ) ) {
			return false;
		}

		$verify_known_shoppers = apply_filters( 'woocommerce_order_received_verify_known_shoppers', true );
		$order_customer_id     = $order->get_customer_id();
		$order_id              = $order->get_id();
		$nonce_is_valid        = wp_verify_nonce( filter_input( INPUT_POST, 'check_submission' ), 'wc_verify_email' );
		$supplied_email        = null;
		$is_active             = false;

		if ( $nonce_is_valid ) {
			$supplied_email = sanitize_email( wp_unslash( filter_input( INPUT_POST, 'email' ) ) );
		}

		if ( Users::should_user_verify_order_email( $order_id, $supplied_email, 'order-received' ) || ( $verify_known_shoppers && $order_customer_id && get_current_user_id() !== $order_customer_id ) ) {
			return false;
		}

		if ( empty( $condition['condition_type'] ) || 'all' === $condition['condition_type'] ) {
			$is_active = true;
		} else {
			if ( 'product_type' !== $condition['condition_type'] ) {
				$condition['condition_query'] = apply_filters( 'wpml_object_id', $condition['condition_query'], $condition['condition_type'] );
			}

			foreach ( $order->get_items() as $item ) {
				switch ( $condition['condition_type'] ) {
					case 'products':
						$is_active = (int) $item->get_product_id() === (int) $condition['condition_query'] || ( $item->get_variation_id() && (int) $item->get_variation_id() === (int) $condition['condition_query'] );
						break;
					case 'product_type':
						$is_active = WC_Product_Factory::get_product_type( $item->get_product_id() ) === $condition['condition_query'];
						break;
					case 'product_cat':
					case 'product_tag':
					case 'product_brand':
					case 'product_attr_term':
					case 'product_shipping_class':
						$terms = wp_get_post_terms( $item->get_product_id(), get_taxonomies(), array( 'fields' => 'ids' ) );

						if ( $terms ) {
							$is_active = in_array( (int) $condition['condition_query'], $terms, true );
						}
						break;
					case 'product_cat_children':
						$terms         = wp_get_post_terms( $item->get_product_id(), get_taxonomies(), array( 'fields' => 'ids' ) );
						$term_children = get_term_children( $condition['condition_query'], 'product_cat' );

						if ( $terms ) {
							$is_active = count( array_diff( $terms, $term_children ) ) !== count( $terms );
						}
						break;
					case 'order_total':
					case 'order_subtotal':
					case 'order_subtotal_after_discount':
						$cart_total = $order->get_total();

						switch ( $condition['condition_type'] ) {
							case 'cart_subtotal':
								$cart_total = $order->get_subtotal();
								break;
							case 'cart_subtotal_after_discount':
								$cart_total = $order->get_subtotal() - $order->get_total_discount();
								break;
						}

						$cart_total = apply_filters( 'woodmart_thank_you_page_condition_cart_total', $cart_total, $order );

						$min = isset( $condition['condition_query_number_min'] ) ? $condition['condition_query_number_min'] : '';
						$max = isset( $condition['condition_query_number_max'] ) ? $condition['condition_query_number_max'] : '';

						if ( '' !== $min && '' !== $max ) {
							$is_active = $cart_total >= $min && $cart_total <= $max;
						} elseif ( '' !== $min ) {
							$is_active = $cart_total >= $min;
						} elseif ( '' !== $max ) {
							$is_active = $cart_total <= $max;
						}
						break;
					case 'order_payment_gateway':
						$is_active = $order->get_payment_method() === $condition['condition_query'];
						break;
					case 'order_shipping_method':
						$shipping_methods = $order->get_shipping_methods();
						$method_ids       = array();

						foreach ( $shipping_methods as $shipping_method ) {
							$method_ids[] = $shipping_method->get_method_id();
						}

						$is_active = in_array( $condition['condition_query'], $method_ids, true );
						break;
					case 'order_shipping_country':
						$is_active = in_array( $order->get_shipping_country(), $condition['condition_query'], true );
						break;
					case 'order_billing_country':
						$is_active = in_array( $order->get_billing_country(), $condition['condition_query'], true );
						break;
					case 'user_logged_in':
						$is_active = is_user_logged_in();
						break;
					case 'user_logged_out':
						$is_active = ! is_user_logged_in();
						break;
				}

				if ( $is_active ) {
					break;
				}
			}
		}

		return ( $is_active && woodmart_is_thank_you_page() ) || ( wp_is_serving_rest_request() && ! empty( $post ) && 'page' === $post->post_type );
	}

	/**
	 * Override templates.
	 *
	 * @param  string $template  Template.
	 *
	 * @return bool|string
	 */
	public function override_template( $template ) {
		if ( woodmart_woocommerce_installed() && woodmart_is_thank_you_page() && Main::get_instance()->has_custom_layout( 'thank_you_page' ) ) {
			$this->display_template();
			return false;
		}

		return $template;
	}

	/**
	 * Display custom template on the single page.
	 */
	protected function display_template() {
		parent::display_template();
		$this->before_template_content();
		?>
		<div class="woocommerce-order entry-content">
		<?php while ( have_posts() ) : ?>
			<?php the_post(); ?>
			<?php $this->template_content( 'thank_you_page' ); ?>
		<?php endwhile; ?>
		</div>
		<?php
		$this->after_template_content();
	}


	/**
	 * Before template content.
	 */
	public function before_template_content() {
		get_header();
		?>
		<div class="wd-content-area site-content">
		<?php
	}

	/**
	 * Before template content.
	 */
	public function after_template_content() {
		?>
		</div>
		<?php
		get_footer();
	}

	/**
	 * Setup post data.
	 */
	public static function setup_postdata() {
		global $post, $order, $wp_query, $wp;

		$products       = wc_get_products( array( 'limit' => 1 ) );
		$random_product = ! empty( $products ) ? $products[0] : false;

		if (
			! Main::is_layout_type( 'thank_you_page' ) ||
			! $random_product ||
			(
				( 'woodmart_layout' !== $post->post_type ) &&
				! is_singular( 'woodmart_layout' ) &&
				! wp_doing_ajax() &&
				( ! isset( $_POST['action'] ) || 'editpost' !== $_POST['action'] ) && // phpcs:ignore WordPress.Security
				( ! defined( 'DOING_AUTOSAVE' ) || ! DOING_AUTOSAVE )
			)
		) {
			return;
		}

		$args = array(
			'limit'      => 1,
			'meta_query' => array( // phpcs:ignore.
				'relation' => 'OR',
				array(
					'key'     => '_refund_amount',
					'compare' => 'NOT EXISTS',
				),
				array(
					'key'     => '_refund_amount',
					'value'   => 0,
					'compare' => '=',
					'type'    => 'NUMERIC',
				),
			),
		);

		$orders       = wc_get_orders( $args );
		$random_order = ! empty( $orders ) ? $orders[0] : false;

		if ( ! $random_order && ! is_a( $order, 'WC_Order' ) ) {
			$dummy_customer_data = array(
				'first_name' => 'John',
				'last_name'  => 'Doe',
				'email'      => 'john.doe@example.com',
				'phone'      => '+1234567890',
				'address_1'  => '123 Dummy Street',
				'address_2'  => '',
				'city'       => 'Dummytown',
				'state'      => 'CA',
				'postcode'   => '12345',
				'country'    => 'US',
			);

			$dummy_order = new WC_Order();
			$dummy_order->add_product( $random_product, 1 );
			$dummy_order->set_address( $dummy_customer_data, 'billing' );
			$dummy_order->set_address( $dummy_customer_data, 'shipping' );
			$dummy_order->set_payment_method( 'cod' );
			$dummy_order->calculate_totals( false );
			$dummy_order->set_status( 'cancelled' );
			$dummy_order->save();

			$random_order = $dummy_order;
		}

		$order            = $random_order; // phpcs:ignore
		$checkout_page_id = wc_get_page_id( 'checkout' );
		if ( $checkout_page_id && get_post_status( $checkout_page_id ) === 'publish' ) {
			$post = get_post( $checkout_page_id ); // phpcs:ignore
			$wp_query->query_vars['order-received'] = $order->get_id();
			$wp->query_vars['order-received']       = $order->get_id();
		}
	}

	/**
	 * Reset post data.
	 */
	public static function reset_postdata() {
		global $order;

		if ( is_singular( 'woodmart_layout' ) || wp_doing_ajax() ) {
			wp_reset_postdata();
			unset( $order );
		}
	}
}

Thank_You_Page::get_instance();
