<?php

namespace ThemeAtelier\DomainForSale\Frontend\Helpers;


/**
 * The file of query insides.
 *
 * @package domain-for-sale
 * @subpackage domain-for-sale/public
 *
 * @since 2.0.0
 */

// If this file is called directly, abort.
if (!defined('WPINC')) {
	die;
}
/**
 * Event all html method.
 *
 * @since 2.0.0
 */
class DomainListingLoopHtml
{
	/**
	 * EFP shortcode markup wrapper classes.
	 *
	 * @param string $layout_preset The selected layout name.
	 * @param int    $shortcode_id The shortcode ID.
	 *
	 */
	public static function domain_listing_wrapper_classes($options, $layout_preset, $domain_listing_gl_id)
	{
		$wrapper_class = "ta-domain-listing-section ta-container domain-listing-wrapper-{$domain_listing_gl_id}";
		echo esc_attr($wrapper_class);
	}

	/**
	 * Preloader
	 *
	 * @param bool $preloader show preloader.
	 * @return void
	 */
	public static function domain_listing_preloader($preloader)
	{
		if ($preloader) {
			ob_start();
			include Helpers::dfs_locate_template('preloader.php');
			$preloader = apply_filters('domain_listing_preloader', ob_get_clean());
			echo wp_kses_post($preloader);
		}
	}

	/**
	 * Get all query posts.
	 *
	 * @param array  $listing_template_options Views listing_template_options.
	 * @param array  $layout Layout preset.
	 * @param object $domain_listing_query post query.
	 * @param int    $view_id Shortcode ID.
	 * @return void
	 */
	public static function domain_listing_get_posts($listing_template_options, $layout, $domain_listing_posts, $shortcode_id, $price_currency)
	{
		$domain_listing_count = 1;
		foreach ($domain_listing_posts as $key => $domain) {
			// Run your post loop display logic
			self::domain_listing_post_loop($listing_template_options, $layout, $domain_listing_count, $shortcode_id, $domain, $price_currency);
			$domain_listing_count++;
		}
	}

	/**
	 * Event Loop.
	 *
	 * @param array  $listing_template_options Views listing_template_options.
	 * @param string $layout Layout preset.
	 * @param int    $shortcode_id Shortcode ID.
	 * @return void
	 */
	public static function domain_listing_post_loop($listing_template_options, $layout, $domain_listing_count, $shortcode_id, $domain, $price_currency)
	{
		$number_of_columns = isset($listing_template_options['domain_number_of_columns']) ? $listing_template_options['domain_number_of_columns'] : '';
		$item_wrapper_class = "dfs_domain domain_item_{$domain->ID}";
		$domain_id = $domain->ID;
		$permalink = get_permalink($domain_id);
		$domain_options = get_post_meta($domain_id, 'dfs_template_options', true);
		$domain_name = isset($domain_options['domain_name']) ? $domain_options['domain_name'] : '';
		$domain_logo = isset($domain_options['domain_logo']) ? $domain_options['domain_logo'] : '';
		$thumbnail_url = isset($domain_logo['url']) ? $domain_logo['url'] : '';
		$price_options = isset($domain_options['price_options']) ? $domain_options['price_options'] : '';
		$sell_price = isset($domain_options['sell_price']) ? $domain_options['sell_price'] : '';
		$dfs_content = isset($domain_options['sale_content']) ? $domain_options['sale_content'] : '';

		$show_domain_content = isset($listing_template_options['show_domain_content']) ? $listing_template_options['show_domain_content'] : true;
		$domain_content_length = '';
		$domain_content_length_limit = isset($domain_content_length['all']) ? $domain_content_length['all'] : '25';
		$domain_content_length_unit = isset($domain_content_length['unit']) ? $domain_content_length['unit'] : 'words';

		if ($domain_content_length_limit > 0) {
			if ('words' === $domain_content_length_unit) {
				$dfs_content = wp_trim_words($dfs_content, (int) $domain_content_length_limit, '...');
			}
		} else {
			$dfs_content;
		}

		if ('grid_layout' === $layout) {
			echo '<div class="' . esc_attr(self::domain_listing_responsive_columns($layout, $number_of_columns)) . '">';
			echo '<div class="' . esc_attr($item_wrapper_class) . '" data-id="' . esc_attr($domain->ID) . '">';

			if ($domain_name) {
				echo '<h2 class="dfs_domain_name">';
				echo '<a href="' . esc_url($permalink) . '">';
				echo esc_html($domain_name);
				echo '</a>';
				echo '</h2>';
			}

			if (!empty($thumbnail_url)) : ?>
				<div class="dfs_domain_logo">
					<a href="<?php echo esc_url($permalink); ?>">
						<img src="<?php echo esc_url($thumbnail_url); ?>"
							alt="<?php echo esc_attr($domain_name); ?>">
					</a>
				</div>
			<?php endif;
			if ($dfs_content && $show_domain_content) : ?>
				<p class="dfs_domain_description">
					<?php echo wp_kses_post($dfs_content); ?>
				</p>
			<?php
			endif;
			echo '<div class="dfs_content_bottom">';
			echo '<a class="dfs_primary_button" href="' . esc_url($permalink) . '">' . esc_html__('Details', 'domain-for-sale-pro') . '</a>';
			echo '</div>';
			echo '</div>';
			echo '</div>';
		} elseif ('list_layout' === $layout) {
			echo '<div class="' . esc_attr(self::domain_listing_responsive_columns($layout, $number_of_columns)) . '">';
			echo '<div class="' . esc_attr($item_wrapper_class) . '" data-id="' . esc_attr($domain->ID) . '">';

			if (!empty($thumbnail_url)) : ?>
				<div class="dfs_domain_logo">
					<a href="<?php echo esc_url($permalink); ?>">
						<img src="<?php echo esc_url($thumbnail_url); ?>"
							alt="<?php echo esc_attr($domain_name); ?>">
					</a>
				</div>
			<?php endif;

			echo '<div class="dfs_domain_content">';
			if ($domain_name) {
				echo '<h2 class="dfs_domain_name">';
				echo '<a href="' . esc_url($permalink) . '">';
				echo esc_html($domain_name);
				echo '</a>';
				echo '</h2>';
			}
			if ($dfs_content && $show_domain_content) : ?>
				<p class="dfs_domain_description">
					<?php echo wp_kses_post($dfs_content); ?>
				</p>
<?php
			endif;
			echo '<div class="dfs_content_bottom">';
			echo '<a class="dfs_primary_button" href="' . esc_url($permalink) . '">' . esc_html__('Details', 'domain-for-sale-pro') . '</a>';
			echo '</div>';
			echo '</div>';
			echo '</div>';
			echo '</div>';
		}
	}

	/**
	 * Event responsive columns class.
	 *
	 * @param string $layout Layout preset.
	 * @param string $columns Columns number.
	 * @return string
	 */
	public static function domain_listing_responsive_columns($layout, $columns)
	{
		$column_lg_desktop = !empty($columns['lg_desktop']) ? $columns['lg_desktop'] : '3';
		$column_desktop = !empty($columns['desktop']) ? $columns['desktop'] : '3';
		$column_tablet = !empty($columns['tablet']) ? $columns['tablet'] : '2';
		$column_mobile_landscape = !empty($columns['mobile_landscape']) ? $columns['mobile_landscape'] : '1';
		$column_mobile = !empty($columns['mobile']) ? $columns['mobile'] : '1';

		$domain_listing_columns = '';
		if ('list_layout' === $layout) {
			$domain_listing_columns = 'ta-col-xs-1 list_layout';
		} else {
			$domain_listing_columns .= " ta-col-xs-$column_mobile ta-col-sm-$column_mobile_landscape ta-col-md-$column_tablet ta-col-lg-$column_desktop ta-col-xl-$column_lg_desktop";
		}
		return $domain_listing_columns;
	}

	public static function domain_listing_html_show($listing_gl_id, $listing_template_options, $section_title, $listing_layout_preset)
	{
		$show_section_title = isset($listing_template_options['section_title']) ? $listing_template_options['section_title'] : false;
		$include_posts = isset($listing_template_options['include_only_domains']) ? $listing_template_options['include_only_domains'] : '';
		$exclude_domains     = !empty($listing_template_options['exclude_domains']) && isset($listing_template_options['exclude_domains']) ? $listing_template_options['exclude_domains'] : '';
		$filter_order = isset($listing_template_options['filter_order']) ? $listing_template_options['filter_order'] : 'ASC';
		$show_domain_pagination = isset($listing_template_options['show_domain_pagination']) ? $listing_template_options['show_domain_pagination'] : true;
		$post_par_page = isset($listing_template_options['post_par_page']) ? $listing_template_options['post_par_page'] : 12;

		$options = get_option('dfs-opt');
		$price_currency = $options['price_currency'] ?? 'usd';

		switch ($price_currency) {
			case 'eur':
				$price_currency = '€';
				break;
			case 'aud':
				$price_currency = 'A$';
				break;
			case 'cad':
				$price_currency = '$';
				break;
			case 'gbp':
				$price_currency = '£';
				break;
			default:
				$price_currency = '$';
		}

		$domain_limit      = !empty($listing_template_options['domain_limit']) ? $listing_template_options['domain_limit'] : 10000;
		if($domain_limit == '-1') {
			$domain_limit = 1000000000;
		}
		$domain_per_page   = isset($listing_template_options['domain_per_page']) ? $listing_template_options['domain_per_page'] : 12;
		$show_domain_pagination = isset($listing_template_options['show_domain_pagination']) ? $listing_template_options['show_domain_pagination'] : false;
		$domain_per_page   = ($domain_per_page > $domain_limit) ? $domain_limit : $domain_per_page;
		$domain_per_page   = (!$show_domain_pagination) ? $domain_limit : $domain_per_page;

		$paged_var = 'paged' . $listing_gl_id;
		$paged     = isset($_GET["$paged_var"]) ? sanitize_text_field(wp_unslash($_GET["$paged_var"])) : 1;

		$limit               = (empty($limit) || '-1' === $limit) ? 10000000 : $limit;
		$offset              = (int) $domain_per_page * ($paged - 1);
		$final_post_per_page = $domain_per_page;
		if (intval($domain_per_page) > $limit - $offset) {
			$final_post_per_page = $limit - $offset;
		}
		$domain_per_page = $final_post_per_page;

		$args = array(
			'post_type'      => 'dfs_template',
			'post_status'   => 'publish',
			'posts_per_page' => $domain_per_page,
			'paged'         => $paged,
			'orderby'        => 'date',
			'order'          => $filter_order,
		);

		$exclude_post_list = array();
		if (!empty($exclude_domains)) {
			foreach ($exclude_domains as $exclude_post) {
				$exclude_post_list[] = intval($exclude_post);
			}
		}
		if (!empty($exclude_post_list) && !empty($include_posts)) {
			$include_posts = array_diff($include_posts, $exclude_post_list);
		} elseif (!empty($exclude_post_list)) {
			$args['post__not_in'] = ($exclude_post_list);
		}
		// Include specific domain.
		if (!empty($include_posts)) {
			$args['post__in'] = $include_posts;
		}

		$domain_listing_query = new \WP_Query($args);
		$domain_listing_posts = $domain_listing_query->posts;
		$args['posts_per_page'] = -1;
		$domain_listing_query = new \WP_Query($args);
		$domain_found = $domain_listing_query->post_count;

		$domain_border = isset($listing_template_options['domain_border']) ? $listing_template_options['domain_border'] : '';
		$border_all = isset($domain_border['all']) ? $domain_border['all'] : '';
		$border_style = isset($domain_border['style']) ? $domain_border['style'] : '';
		$border_color = isset($domain_border['color']) ? $domain_border['color'] : '';
		$domain_border = "{$border_all}px {$border_style} {$border_color}";
		$domain_border_radius = isset($listing_template_options['domain_border_radius']) ? $listing_template_options['domain_border_radius'] : '';
		$domain_border_radius_length   = isset($domain_border_radius['all']) ? $domain_border_radius['all'] : '0';
		$domain_border_radius_unit     = isset($domain_border_radius['unit']) ? $domain_border_radius['unit'] : 'px';
		$domain_border_radius = $domain_border_radius_length > 0 ? $domain_border_radius_length . $domain_border_radius_unit : '0';
		$domain_inner_padding = isset($listing_template_options['domain_inner_padding']) ? $listing_template_options['domain_inner_padding'] : '';
		$domain_inner_padding_top = isset($domain_inner_padding['top']) ? $domain_inner_padding['top'] : '0';
		$domain_inner_padding_right = isset($domain_inner_padding['right']) ? $domain_inner_padding['right'] : '0';
		$domain_inner_padding_bottom = isset($domain_inner_padding['bottom']) ? $domain_inner_padding['bottom'] : '0';
		$domain_inner_padding_left = isset($domain_inner_padding['left']) ? $domain_inner_padding['left'] : '0';
		$domain_inner_padding = "{$domain_inner_padding_top}px {$domain_inner_padding_right}px {$domain_inner_padding_bottom}px {$domain_inner_padding_left}px";
		$show_domain_box_shadow = isset($listing_template_options['show_domain_box_shadow']) ? $listing_template_options['show_domain_box_shadow'] : '';

		$domain_box_shadow_property = '';
		if ($show_domain_box_shadow) {
			$domain_box_shadow = isset($listing_template_options['domain_box_shadow']) ? $listing_template_options['domain_box_shadow'] : '';

			$box_shadow_h             = isset($domain_box_shadow['horizontal']) ? $domain_box_shadow['horizontal'] : '0';
			$box_shadow_v             = isset($domain_box_shadow['vertical']) ? $domain_box_shadow['vertical'] : '0';
			$box_shadow_blur          = isset($domain_box_shadow['blur']) ? $domain_box_shadow['blur'] : '6';
			$box_shadow_spread        = isset($domain_box_shadow['spread']) ? $domain_box_shadow['spread'] : '-2';
			$box_shadow_color         = isset($domain_box_shadow['color']) ? $domain_box_shadow['color'] : 'rgb(187, 187, 187)';
			$box_shadow_style         = 'outset' === $domain_box_shadow['style'] ? '' : $domain_box_shadow['style'];
			$box_shadow_margin_top    = 'inset' === $box_shadow_style ? '0' : ($box_shadow_spread - $box_shadow_v + 0.5 * $box_shadow_blur);
			$box_shadow_margin_right  = 'inset' === $box_shadow_style ? '0' : ($box_shadow_spread + $box_shadow_h + 0.5 * $box_shadow_blur);
			$box_shadow_margin_bottom = 'inset' === $box_shadow_style ? '0' : ($box_shadow_spread + $box_shadow_v + 0.5 * $box_shadow_blur);
			$box_shadow_margin_left   = 'inset' === $box_shadow_style ? '0' : ($box_shadow_spread - $box_shadow_h + 0.5 * $box_shadow_blur);

			$domain_item_box_shadow = "{$box_shadow_h}px {$box_shadow_v}px {$box_shadow_blur}px {$box_shadow_spread}px {$box_shadow_color} {$box_shadow_style}";
			$domain_item_box_margin = "{$box_shadow_margin_top}px {$box_shadow_margin_right}px {$box_shadow_margin_bottom}px {$box_shadow_margin_left}px";
			$domain_box_shadow_property = "--domain_box_shadow: $domain_item_box_shadow; --domain_box_margin: $domain_item_box_margin;";
		}
		$domain_background = isset($listing_template_options['domain_background']) ? $listing_template_options['domain_background'] : '';
		$domain_background_color = isset($domain_background['color']) ? $domain_background['color'] : '';
		$domain_background_hover_color = isset($domain_background['hover_color']) ? $domain_background['hover_color'] : '';

		$margin_between_domain      = isset($listing_template_options['margin_between_domain']['all']) ? (int) $listing_template_options['margin_between_domain']['all'] : 20;
		$margin_between_domain_half = $margin_between_domain / 2;

		$item_style_var = "--domain_border: $domain_border;--domain_border_radius: $domain_border_radius;--domain_inner_padding: $domain_inner_padding;$domain_box_shadow_property";

		$item_style_var .= "--domain_background_color: $domain_background_color;--domain_background_hover_color: $domain_background_hover_color;--margin_between_domain: {$margin_between_domain}px;--margin_between_domain_half: {$margin_between_domain_half}px;";


		if ($domain_listing_posts) {
			if ('grid_layout' === $listing_layout_preset) {
				include Helpers::dfs_locate_template('Template_grid.php');
			} elseif ('list_layout' === $listing_layout_preset) {
				include Helpers::dfs_locate_template('Template_list.php');
			}
		}
	}
}
