<?php

namespace Brandy\Builder\Footer;

use Brandy\Builder\Element\ElementBuilder;
use Brandy\Traits\SingletonTrait;
use Brandy\Utils\Helpers;

class FooterBuilder {

	use SingletonTrait;

	protected function __construct() {
		add_action( 'brandy_footer', array( $this, 'render_footer' ) );
		add_action( 'brandy_render_footer_placement', array( $this, 'render_placement' ), 10, 3 );
	}

	public function render_footer() {
		get_template_part( 'template-parts/builder/footer/footer-layout' );
	}

	public function render_placement( string $placement, $device, $row_settings ) {

		$current_template = brandy_get_footer_template();
		$columns          = $current_template['placements'][ $device ][ $placement ];
		foreach ( $columns as $col_index => $col_elements ) :
			$col_attributes = array(
				'class' => 'footer-col',
			);
			foreach ( brandy_get_devices() as $d ) {
				$check_value                                 = array(
					'desktop' => $row_settings['column_items_direction']['desktop'][ 'column_' . ( $col_index + 1 ) ] ?? 'vertical',
					'tablet'  => $row_settings['column_items_direction']['tablet'][ 'column_' . ( $col_index + 1 ) ] ?? null,
					'mobile'  => $row_settings['column_items_direction']['mobile'][ 'column_' . ( $col_index + 1 ) ] ?? null,
				);
				$get_value                                   = Helpers::get_device_value( $check_value, $d );
				$col_attributes[ "data-items-direction-$d" ] = $get_value;
			}
			?>
			<div <?php brandy_print_dom_attributes( $col_attributes ); ?>>
			<?php
			foreach ( $col_elements as $element_id ) {
				$element  = $current_template['elements'][ $element_id ];
				$renderer = new ElementBuilder( 'footer', $element, $device );
				$renderer->render();
			}
			?>
			</div>
			<?php
		endforeach;
	}
}
