<?php

namespace ThimPress\ThimBlocks\Gutenberg\Blocks\Team;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use ThimPress\ThimBlocks\Gutenberg\Blocks\AbstractBlockType;
use Throwable;

/**
 * Class TeamBlockType
 *
 * Handle register, render block template
 */
class TeamBlockType extends AbstractBlockType {

	public $block_name = 'team';

	public function get_supports(): array {
		return array(
			'align'       => true,
			'anchor'      => false,
			'innerBlocks' => true,
			'spacing'     => array(
				'margin'  => true,
				'padding' => true,
			),
		);
	}

	private function create_member_css_vars( $attr, $key ) {
		$vars = array();
		if ( empty( $attr ) ) {
			return $vars;
		}

		// Padding
		if ( ! empty( $attr['margin'] ) ) {
			foreach ( $attr['margin'] as $device => $value ) {
				if ( ! empty( $value ) ) {
					$top    = $value['top'] ?? 0;
					$right  = $value['right'] ?? 0;
					$bottom = $value['bottom'] ?? 0;
					$left   = $value['left'] ?? 0;

					$vars[] = sprintf(
						'--%s-margin-%s:%dpx %dpx %dpx %dpx;',
						$key,
						strtolower( $device ),
						$top,
						$right,
						$bottom,
						$left
					);
				}
			}
		}

		// Font Size
		if ( ! empty( $attr['fontSize'] ) ) {
			foreach ( $attr['fontSize'] as $device => $value ) {
				if ( $value || $value === 0 ) {
					$vars[] = sprintf(
						'--%s-font-size-%s:%d;',
						$key,
						strtolower( $device ),
						$value
					);
				}
			}
		}

		// Color
		if ( ! empty( $attr['color'] ) ) {
			$vars[] = sprintf(
				'--%s-color:%s;',
				$key,
				$attr['color']
			);
		}

		return $vars;
	}

	public function render_content_block_template( array $attributes, $content, $block ): string {
		$html = '';

		try {
			$layout       = ! empty( $attributes['layout'] ) ? $attributes['layout'] : 'layout-1';
			$columns      = $attributes['columns'] ?? array();
			$spaceBetween = $attributes['spaceBetween'] ?? array();

			$styles = sprintf(
				'--team-columns-mobile:%d; --team-columns-tablet:%d; --team-columns-desktop:%d; ' .
					'--team-space-mobile:%dpx; --team-space-tablet:%dpx; --team-space-desktop:%dpx;',
				$columns['Mobile'] ?? 1,
				$columns['Tablet'] ?? 2,
				$columns['Desktop'] ?? 4,
				$spaceBetween['Mobile'] ?? 10,
				$spaceBetween['Tablet'] ?? 20,
				$spaceBetween['Desktop'] ?? 30,
			);

			$member_vars = array_merge(
				$this->create_member_css_vars( $attributes['memberName'] ?? array(), 'memberName' ),
				$this->create_member_css_vars( $attributes['memberPosition'] ?? array(), 'memberPosition' ),
				$this->create_member_css_vars( $attributes['memberDescription'] ?? array(), 'memberDescription' )
			);

			$avatar_space  = $attributes['memberAvatar']['spaceContent'] ?? null;
			$avatar_radius = $attributes['memberAvatar']['radius'] ?? null;

			$avatar_vars = array();

			if ( ! empty( $avatar_space ) ) {
				$avatar_vars[] = sprintf( '--avatar-space:%dpx;', intval( $avatar_space ) );
			} else {
				$avatar_vars[] = '--avatar-space:20px;';
			}

			if (
				! empty( $avatar_radius['top'] ) ||
				! empty( $avatar_radius['right'] ) ||
				! empty( $avatar_radius['bottom'] ) ||
				! empty( $avatar_radius['left'] )
			) {
				$avatar_vars[] = sprintf(
					'--avatar-radius:%dpx %dpx %dpx %dpx;',
					intval( $avatar_radius['top'] ?? 0 ),
					intval( $avatar_radius['right'] ?? 0 ),
					intval( $avatar_radius['bottom'] ?? 0 ),
					intval( $avatar_radius['left'] ?? 0 )
				);
			}

			$styles .= implode( ' ', $member_vars );

			$styles .= ' ' . implode( ' ', $avatar_vars );

			$wrapper_attributes = get_block_wrapper_attributes(
				array(
					'class' => 'thim-blocks-team ' . $layout,
					'style' => $styles,
				)
			);

			ob_start();
			echo '<div ' . wp_kses_post( $wrapper_attributes ) . '>';
			echo wp_kses_post( $content );
			echo '</div>';

			$html = ob_get_clean();
		} catch ( Throwable $e ) {
			return $html;
		}

		return $html;
	}

}
