<?php
/**
 * Add an element to fusion-builder.
 *
 * @package fusion-builder
 * @since 1.0
 */

if ( ! class_exists( 'Fusion_Row_Element' ) ) {
	/**
	 * Shortcode class.
	 *
	 * @since 1.0
	 */
	class Fusion_Row_Element extends Fusion_Element {
		/**
		 * Shortcode attribute ID.
		 *
		 * @var string
		 */
		public $shortcode_attr_id = '';

		/**
		 * Shortcode CSS class name.
		 *
		 * @var string
		 */
		public $shortcode_classname = '';

		/**
		 * Shortcode name.
		 *
		 * @var string
		 */
		public $shortcode_name = '';

		/**
		 * The filter-name we want to apply using apply_filters.
		 *
		 * @var string
		 */
		public $content_filter = '';

		/**
		 * Constructor.
		 *
		 * @access public
		 * @param string $shortcode         The shortcode we want to add.
		 * @param string $shortcode_attr_id The shortcode attribute-ID.
		 * @param string $classname         The shortcode's CSS classname.
		 * @param string $content_filter    The filter-name we want to apply using apply_filters.
		 * @since 3.0
		 */
		public function __construct( $shortcode, $shortcode_attr_id, $classname, $content_filter ) {
			parent::__construct();
			add_shortcode( $shortcode, [ $this, 'render' ] );

			$this->shortcode_attr_id   = $shortcode_attr_id;
			$this->shortcode_classname = $classname;
			$this->shortcode_name      = $shortcode;
			$this->content_filter      = $content_filter;

			add_filter( "fusion_attr_{$this->shortcode_attr_id}-shortcode", [ $this, 'attr' ] );
		}

		/**
		 * Gets the default values.
		 *
		 * @static
		 * @access public
		 * @since 3.0
		 * @return array
		 */
		public static function get_element_defaults() {
			return [
				'id'    => '',
				'class' => '',
			];
		}

		/**
		 * Used to set any other variables for use on front-end editor template.
		 *
		 * @static
		 * @access public
		 * @since 3.0
		 * @return array
		 */
		public static function get_element_extras() {
			$fusion_settings = awb_get_fusion_settings();
			return [
				'site_width' => $fusion_settings->get( 'site_width' ),
			];
		}

		/**
		 * Maps settings to extra variables.
		 *
		 * @static
		 * @access public
		 * @since 3.0
		 * @return array
		 */
		public static function settings_to_extras() {
			return [
				'site_width' => 'site_width',
			];
		}

		/**
		 * Render the shortcode
		 *
		 * @access public
		 * @since 3.0
		 * @param  array  $atts    Shortcode parameters.
		 * @param  string $content Content between shortcode.
		 * @return string          HTML output.
		 */
		public function render( $atts, $content = '' ) {
			$this->args = FusionBuilder::set_shortcode_defaults( static::get_element_defaults(), $atts, $this->shortcode_name );

			$html = '<div ' . FusionBuilder::attributes( $this->shortcode_attr_id . '-shortcode' ) . '>' . do_shortcode( fusion_builder_fix_shortcodes( $content ) ) . '</div>';

			$this->on_render();

			return apply_filters( $this->content_filter, $html, $atts );
		}

		/**
		 * Row attributes.
		 *
		 * @access public
		 * @since 3.0
		 * @return array
		 */
		public function attr() {
			$fusion_settings = awb_get_fusion_settings();

			$attr = [
				'class' => 'fusion-builder-row ' . $this->shortcode_classname,
				'style' => '',
			];

			// Get information about parent container.
			$container_args                 = false;
			$this->args['flex']             = false;
			$this->args['nested_container'] = 'fusion_builder_row_inner' === $this->shortcode_name;

			if ( function_exists( 'fusion_builder_container' ) ) {
				$container_args               = fusion_builder_container()->args;
				$this->args['flex']           = fusion_builder_container()->is_flex();
				$this->args['column_spacing'] = fusion_library()->sanitize->size( fusion_builder_container()->get_column_spacing(), 'px' );

				// Not an inner row, check if we are nested though.
				if ( ! $this->args['nested_container'] ) {
					$nesting_depth = fusion_builder_container()->get_nesting_depth();
					$override_name = function_exists( 'Fusion_Template_Builder' ) ? Fusion_Template_Builder()->get_current_override_name() : false;

					$this->args['nested_container'] = ( 1 === $nesting_depth && ( 'content' === $override_name || FusionBuilder()->mega_menu_data['is_rendering'] ) ) ? false : fusion_builder_container()->is_nested();
				}
			}

			// We have container arguments, set styling to row.
			if ( $container_args ) {
				if ( $this->args['flex'] ) {
					$flex_align_items     = isset( $this->args['flex_align_items'] ) && $this->args['flex_align_items'] ? $this->args['flex_align_items'] : $container_args['flex_align_items'];
					$align_content        = isset( $this->args['align_content'] ) && $this->args['align_content'] ? $this->args['align_content'] : $container_args['align_content'];
					$flex_justify_content = isset( $this->args['flex_justify_content'] ) && $this->args['flex_justify_content'] ? $this->args['flex_justify_content'] : $container_args['flex_justify_content'];

					// Minimum and maximum height get handled below in get_style_vars().

					$attr['class'] .= ' fusion-flex-align-items-' . $flex_align_items;

					if ( 'stretch' !== $align_content ) {
						$attr['class'] .= ' fusion-flex-align-content-' . $align_content;
					}
					if ( 'flex-start' !== $flex_justify_content ) {
						$attr['class'] .= ' fusion-flex-justify-content-' . $flex_justify_content;
					}

					$attr['class'] .= ' fusion-flex-content-wrap';

					// Flex wrap gets handled below in get_style_vars().

					$attr['style'] .= $this->get_style_vars();

					// If this is not nested row, then increase width with negative margins to match column margin.
					$width = 'yes' === $container_args['hundred_percent'] || $this->args['nested_container'] ? '100%' : $fusion_settings->get( 'site_width' );

					// In case Global Options are still not set.
					if ( '' === $width ) {
						$width = '1200px';
					}

					$column_spacing_unit = Fusion_Sanitize::get_unit( $this->args['column_spacing'] );

					if ( '%' === $column_spacing_unit ) {
						$column_spacing_value = Fusion_Sanitize::number( $this->args['column_spacing'] );
						$width_value          = Fusion_Sanitize::number( $width );
						$width_unit           = Fusion_Sanitize::get_unit( $width );

						$width = ( $width_value * ( 1 + $column_spacing_value / 100 ) ) . $width_unit;
					} else {
						$width = 'calc( ' . $width . ' + ' . $this->args['column_spacing'] . ' )';
					}

					if ( 'no' === $container_args['hundred_percent'] && ! $this->args['nested_container'] ) {
						$attr['style'] .= 'max-width:' . $width . ';';
					} else {
						$attr['style'] .= 'width:' . $width . ' !important;';
						$attr['style'] .= 'max-width:' . $width . ' !important;';
					}

					$attr['style'] .= 'margin-left: calc(-' . $this->args['column_spacing'] . ' / 2 );';
					$attr['style'] .= 'margin-right: calc(-' . $this->args['column_spacing'] . ' / 2 );';
				}
			}

			if ( '' !== $this->args['id'] ) {
				$attr['id'] = $this->args['id'];
			}

			if ( '' !== $this->args['class'] ) {
				$attr['class'] .= ' ' . $this->args['class'];
			}

			return $attr;
		}

		/**
		 * Get the styling vars.
		 *
		 * @since 3.14.0
		 * @return string
		 */
		public function get_style_vars() {
			return '';
		}
	}
}
