<?php
/**
 * Container block settings.
 *
 * @package Tasty_Roundups
 */

namespace Tasty_Roundups\Blocks;

use Tasty_Roundups\Block_Editor;

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

if ( ! class_exists( '\Tasty_Roundups\Blocks\Container_Block' ) ) {
	/**
	 * The container block class.
	 */
	class Container_Block {
		/**
		 * The global settings.
		 *
		 * @var array
		 */
		private $settings = array();

		/**
		 * The block content.
		 *
		 * @var string
		 */
		private $content = '';

		/**
		 * The block attributes.
		 *
		 * @var array
		 */
		private $attributes = array();

		/**
		 * Construct the object.
		 *
		 * @param array  $attributes The block attributes.
		 * @param string $content    The block content.
		 */
		public function __construct( $attributes, $content ) {
			$this->settings   = Block_Editor::get_customization_settings();
			$this->content    = $content;
			$this->attributes = wp_parse_args( $attributes, $this->get_default_attributes() );
		}

		/**
		 * Render the block.
		 *
		 * @return mixed
		 */
		public function render() {
			wp_enqueue_style( 'tasty-roundups-block' );

			$container_classes = 'tasty-roundups tasty-roundups-rendered';
			$display_numbers   = 'on' === $this->settings['numbers'] ? true : false;

			switch ( $this->attributes['widthOption']['value'] ) {
				case 'wide':
					$container_classes .= ' tasty-roundups-wide-container';
					break;
				case 'full':
					$container_classes .= ' tasty-roundups-full-container';
					break;
			}

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

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

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

			$affiliate_url = '';
			if ( ! empty( $this->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,
						$this->settings['shareasale_affiliate_id']
					)
				);
			}

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

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

		/**
		 * Get the default attributes for the block.
		 *
		 * @return array
		 */
		private function get_default_attributes() {
			return array(
				'widthOption'        => array(
					'icon'  => 'alignNone',
					'value' => 'none',
				),
				'displayNumbers'     => 'global',
				'columns'            => '1',
				'imageShape'         => 'square',
				'customImageShape'   => array(
					'type'  => 'all',
					'value' => '1',
				),
				'imagePosition'      => $this->settings['media_position'],
				'buttonStyle'        => 'filled',
				'disableImage'       => false,
				'disableDescription' => false,
				'disableButton'      => false,
				'disableComments'    => false,
				'disableRating'      => false,
				'externalURLError'   => '',
				'resetSearch'        => false,
			);
		}
	}
}
