<?php

/**
 * WooCommerce Checkout Class.
 *
 * @package Kubelio\WooCommerce
 * @since 1.0.0
 */

namespace Kubelio\WooCommerce;

if (! defined('ABSPATH')) {
	exit;
}

/**
 * Checkout Class
 */
class Checkout {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action('woocommerce_checkout_before_order_review_heading', array($this, 'wrap_order_review_start'), 5);
		add_action('woocommerce_checkout_after_order_review', array($this, 'wrap_order_review_end'), 20);

		add_action('woocommerce_review_order_before_payment', array($this, 'move_coupon_form'));

		// Move Cross Sell
		remove_action('woocommerce_cart_collaterals', 'woocommerce_cross_sell_display');
		add_action('woocommerce_after_cart_table', 'woocommerce_cross_sell_display');

		add_filter('template_include', array($this, 'distraction_free_template'), 99);
	}

	/**
	 * Wrap Order Review Start
	 */
	public function wrap_order_review_start() {
		echo '<div class="rt-checkout-order-review">';
	}

	/**
	 * Wrap Order Review End
	 */
	public function wrap_order_review_end() {
		echo '</div>';
	}

	/**
	 * Move coupon form
	 */
	public function move_coupon_form() {
		if (wc_coupons_enabled()) {
			echo '<div class="rt-review-order-coupon rt_review_order_coupon"></div>';
		}
	}

	/**
	 * Distraction-Free Template
	 *
	 * @param string $template The path to the template.
	 * @return string
	 */
	public function distraction_free_template($template) {
		if (kubelio_is_woocommerce(array('cart', 'checkout', 'thank_page')) && get_theme_mod('checkout_focus', true)) {
			$custom_template = get_stylesheet_directory() . '/woocommerce/checkout-template.php';

			if (file_exists($custom_template)) {
				return $custom_template;
			}
		}
		return $template;
	}
}
