<?php

/**
 * The admin backend preview.
 *
 * @link        https://themeatelier.com/
 * @since      2.4.0
 *
 * @package    domain_for_sale
 * @subpackage domain_for_sale/Admin
 */

namespace ThemeAtelier\DomainForSale\Admin\Preview;

use ThemeAtelier\DomainForSale\Frontend\Helpers\DomainListingLoopHtml;
use ThemeAtelier\DomainForSale\Frontend\Templates\Template_01;

/**
 * The admin preview.
 */
class Preview
{


	/**
	 * Script and style suffix
	 *
	 * @since 2.4.0
	 * @access protected
	 * @var string
	 */
	protected $main;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    2.4.0
	 */
	public function __construct()
	{
		$this->dfs_preview_action();
		$this->main = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG || defined('WP_DEBUG') && WP_DEBUG ? '' : '.min';
	}

	/**
	 * Public Action
	 *
	 * @return void
	 */
	private function dfs_preview_action()
	{
		// Admin Preview.
		add_action('wp_ajax_dfs_preview_meta_box', array($this, 'domain_for_sale_backend_preview'));
	}

	/**
	 * Function Backed preview.
	 *
	 * @since 2.4.0
	 */
	public function domain_for_sale_backend_preview()
	{
		$nonce = isset($_POST['ajax_nonce']) ? sanitize_text_field(wp_unslash($_POST['ajax_nonce'])) : ''; // phpcs:ignore
		if (! wp_verify_nonce($nonce, 'dfs_metabox_nonce')) {
			return;
		}

		$setting = array();
		// XSS ok.
		// No worries, This "POST" requests is sanitizing in the below array map.
		$data = ! empty($_POST['data']) ? wp_unslash($_POST['data'])  : ''; // phpcs:ignore
		parse_str($data, $setting);
		$post_id = $setting['post_ID'];
		// Preset Layouts.
		$template_options  = $setting['dfs_template_options'];
		$layout            = $setting['dfs_layouts'];
		$dfs_layout_preset = isset($layout['dfs_layout_preset']) ? $layout['dfs_layout_preset'] : '';
		$listing_template_options  = $setting['listing_template_options'];
		$listing_layout            = $setting['listing_layouts'];
		$listing_layout_preset = isset($listing_layout['listing_layout_preset']) ? $listing_layout['listing_layout_preset'] : '';

		$enqueue_fonts = array();
		// Google fonts.
		$body_typo            = isset($template_options['body_typo']) ? $template_options['body_typo'] : '';
		$domain_name_typo                = isset($template_options['domain_name_typo']) ? $template_options['domain_name_typo'] : '';
		$sale_title_typo                 = isset($template_options['sale_title_typo']) ? $template_options['sale_title_typo'] : '';
		$dfs_form_title                 = isset($template_options['dfs_form_title']) ? $template_options['dfs_form_title'] : '';
		$all_fonts                      = array();
		$domain_for_sale_typography     = array();
		$domain_for_sale_typography[]   = $body_typo;
		$domain_for_sale_typography[]   = $domain_name_typo;
		$domain_for_sale_typography[]   = $sale_title_typo;
		$domain_for_sale_typography[]   = $dfs_form_title;
		$domain_for_sale_typography[]   = isset($template_options['read_more_typography']) ? $template_options['read_more_typography'] : array(
			'font-family'        => '',
			'font-weight'        => '600',
			'subset'             => '',
			'font-size'          => '12',
			'tablet-font-size'   => '12',
			'mobile-font-size'   => '10',
			'line-height'        => '18',
			'tablet-line-height' => '18',
			'mobile-line-height' => '16',
			'letter-spacing'     => '0',
			'text-align'         => 'left',
			'text-transform'     => 'uppercase',
			'type'               => '',
			'unit'               => 'px',
		);

		if (! empty($domain_for_sale_typography)) {
			foreach ($domain_for_sale_typography as $font) {
				if (isset($font['font-family']) && isset($font['type']) && 'google' === $font['type']) {
					$variant     = (isset($font['font-weight']) && '' !== $font['font-weight']) ? ':' . $font['font-weight'] : '';
					$all_fonts[] = $font['font-family'] . $variant;
				}
			}
		}
		if ($all_fonts) {
			$enqueue_fonts[] = $all_fonts;
		}

		// Enqueue Google fonts.
		if (! empty($enqueue_fonts)) {
			// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
			echo '<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=' . implode('|', array_merge(...$enqueue_fonts)) . '" media="all">';
		}

		$style   = DOMAIN_FOR_SALE_URL . '/src/Frontend/assets/css/domain-for-sale-style' . $this->main . '.css';
		$preview = DOMAIN_FOR_SALE_URL . '/src/Admin/assets/css/admin-preview' . $this->main . '.css';

		// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
		echo '<link rel="stylesheet" href="' . DOMAIN_FOR_SALE_URL . '/src/Frontend/assets/css/ta-grid' . $this->main . '.css' . '" >';
		// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
		echo '<link rel="stylesheet" href="' . esc_url($style) . '" >';
		// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
		echo '<link rel="stylesheet" href="' . esc_url($preview) . '" >';
		$post_type = get_post_type($post_id);
?>
		<div class="dfs_preview_header">
			<div class="dfs_dot_wrap">
				<span class="dot dot-red"></span>
				<span class="dot dot-yellow"></span>
				<span class="dot dot-success"></span>
			</div>
		</div>
		<?php
		if ($post_type === 'dfs_template') {

			if ($dfs_layout_preset === 'layout_01') {
				Template_01::content($template_options);
			}
		} elseif ($post_type === 'domain_listing') {

			$section_title = get_the_title($post_id);
			echo '<div class="admin_preview">';
			DomainListingLoopHtml::domain_listing_html_show(
				$post_id,
				$listing_template_options,
				$section_title,
				$listing_layout_preset
			);
			echo '</div>';
		}
		?>
		<!-- phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet -->
		<script src="<?php echo esc_url(DOMAIN_FOR_SALE_URL . '/src/Frontend/assets/js/domain-for-sale-script' . $this->main . '.js'); ?>"></script>
		<!-- phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet -->
		<script src="<?php echo esc_url(DOMAIN_FOR_SALE_URL . '/src/Admin/assets/js/admin-preview' . $this->main . '.js'); ?>"></script>
<?php
		die();
	}
}
