<?php
/**
 * Legacy blocks.
 *
 * @package Tasty_Roundups
 */

namespace Tasty_Roundups\Blocks;

use Tasty_Roundups\Block_Editor;

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

if ( ! class_exists( '\Tasty_Roundups\Blocks\Legacy_Blocks' ) ) {
	/**
	 * The container block class.
	 */
	class Legacy_Blocks {
		/**
		 * Renders a container block on the frontend.
		 *
		 * @param array  $attributes Block attributes.
		 * @param string $content    Block content.
		 * @return string
		 */
		public static function render_container_block( $attributes, $content ) {

			$settings          = Block_Editor::get_customization_settings();
			$container_classes = 'tasty-roundups-legacy';

			$display_numbers = 'on' === $settings['numbers'] ? true : false;

			if ( isset( $attributes['displayNumbers'] ) && 'on' === $attributes['displayNumbers'] ) {
				$display_numbers = true;
			}

			if ( isset( $attributes['displayNumbers'] ) && 'off' === $attributes['displayNumbers'] ) {
				$display_numbers = false;
			}

			if ( $display_numbers ) {
				$container_classes .= ' tasty-roundup-legacy-items-have-numbers';
			}

			$affiliate_url = '';
			if ( ! empty( $settings['shareasale_affiliate_id'] ) ) {
				$affiliate_url = esc_url(
					sprintf(
						'https://shareasale.com/r.cfm?b=%s&u=%s&m=69860&urllink=&afftrack=trattr',
						TASTY_ROUNDUPS_SHAREASALE_PRODUCT_ID,
						$settings['shareasale_affiliate_id']
					)
				);
			}

			$styles   = '';
			$css_file = dirname( TASTY_ROUNDUPS_PLUGIN_FILE ) . '/assets/dist/legacy-tasty-roundups-block.css';
			if ( file_exists( $css_file ) ) {
				// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
				$styles = file_get_contents( $css_file );

				if ( false === $styles ) {
					$styles = '';
				}
			}
			if ( $styles ) {
				$styles = '<style type="text/css" style="display: none !important;">' . $styles . '</style>';
			}

			/**
			 * Filters the Roundups Container affiliate URL.
			 *
			 * @var string $affiliate_url Existing Roundups Container affilate URL.
			 */
			$affiliate_url = apply_filters( 'tasty_roundups_container_affiliate_url', $affiliate_url );

			return tasty_roundups_get_template_part(
				'legacy-tasty-roundups-container',
				array(
					'attributes'        => $attributes,
					'content'           => $content,
					'container_classes' => $container_classes,
					'styles'            => $styles,
					'settings'          => $settings,
					'affiliate_url'     => $affiliate_url,
				)
			);
		}

		/**
		 * Renders an item block on the frontend.
		 *
		 * @param array     $attributes Block attributes.
		 * @param string    $content    Block content.
		 * @param \WP_Block $block      The block.
		 * @return string
		 */
		public static function render_item_block( $attributes, $content, $block ) {

			$settings       = Block_Editor::get_customization_settings();
			$item_classes   = 'tasty-roundups-legacy-item';
			$media_position = ! empty( $attributes['mediaPosition'] ) ? $attributes['mediaPosition'] : $settings['media_position'];
			if ( ! empty( $media_position ) ) {
				$item_classes .= ' tasty-roundups-legacy-item-align-' . $media_position;
			}

			$display_numbers = 'on' === $settings['numbers'] ? true : false;

			if ( isset( $block->context['tasty-roundups/displayNumbers'] ) ) {
				$display_numbers = 'on' === $block->context['tasty-roundups/displayNumbers'];
			}

			$number_html = '';
			if ( $display_numbers ) {
				$number_html = '<i class="tasty-roundups-legacy-item-number" data-tasty-roundups-customization="number-text-color.color number-background-color.background-color number-border-color.border-color number-border-width.border-width"></i>';
			}

			$link_start = '';
			$link_end   = '';
			if ( ! empty( $attributes['href'] ) ) {
				$link_start = '<a';
				if ( ! empty( $attributes['linkTarget'] ) ) {
					$link_start .= ' target="' . esc_attr( $attributes['linkTarget'] ) . '"';
				}
				if ( ! empty( $attributes['linkRel'] ) ) {
					$link_start .= ' rel="' . esc_attr( $attributes['linkRel'] ) . '"';
				}
				$link_start .= ' href="' . esc_url( $attributes['href'] ) . '">';
				$link_end    = '</a>';
			}

			$media_size = 'medium_large';
			if ( 'top' === $media_position ) {
				$media_size = 'large';
			}
			$media_html = '';
			if ( ! empty( $attributes['mediaId'] ) ) {
				$pairs = array(
					'mediaAlt' => 'alt',
				);
				$attrs = array(
					'class' => 'tasty-roundups-legacy-image',
				);
				foreach ( $pairs as $key => $attr ) {
					if ( ! empty( $attributes[ $key ] ) ) {
						$attrs[ $attr ] = $attributes[ $key ];
					}
				}

				/**
				 * Filters item media size.
				 *
				 * @var string $media_size Media size.
				 * @var string $media_position Media position.
				 * @var array  $attributes Block attributes.
				 */
				$media_size = apply_filters( 'tasty_roundups_item_media_size', $media_size, $media_position, $attributes );

				/**
				 * Filters the attributes used in the media HTML.
				 *
				 * @var array $attrs      Existing attributes to be used.
				 * @var array $attributes Block attributes.
				 */
				$attrs         = apply_filters(
					'tasty_roundups_item_media_attrs',
					$attrs,
					$attributes
				);
				$media_html    = wp_get_attachment_image(
					$attributes['mediaId'],
					$media_size,
					false,
					$attrs
				);
				$item_classes .= ' tasty-roundups-legacy-item-has-image';
			}

			$button_text = ! empty( $attributes['buttonText'] ) ? $attributes['buttonText'] : $settings['button_text'];

			$button_html = '';
			if ( ! empty( $button_text ) ) {
				$custom_styles     = 'button-background-color.background button-text-color.color button-border-radius.border-radius';
				$button_link_start = str_replace(
					'<a ',
					'<a class="tasty-roundups-legacy-button" data-tasty-roundups-customization="' . esc_attr( $custom_styles ) . '" ',
					$link_start
				);
				$button_html       = $button_link_start . wp_kses_post( $button_text ) . $link_end;
			}

			$output = tasty_roundups_get_template_part(
				'legacy-tasty-roundups-item',
				array(
					'attributes'   => $attributes,
					'content'      => $content,
					'item_classes' => $item_classes,
					'link_start'   => $link_start,
					'link_end'     => $link_end,
					'number_html'  => $number_html,
					'media_html'   => $media_html,
					'button_html'  => $button_html,
				)
			);

			$output = Item_Block::apply_customization_settings( $output );
			return $output;
		}
	}
}
