<?php
namespace SabaiApps\Directories\Component\Entity\Helper;

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Entity\Type\IEntity;

class CardHelper {

	public function help( Application $application, IEntity $entity, array $options = array() ) {
		$options += array(
			'width'            => 100,
			'image_overlay'    => false,
			'no_border'        => false,
			'stretch_link'     => false,
			'cover_field'      => null,
			'cover_field_size' => 'medium',
			'cover_height'     => 170,
			'thumbnail_field'  => null,
			'thumbnail_width'  => 60,
			'thumbnail_align'  => 'center',
			'icon_font'        => array(
				'font_size'     => 'rel',
				'font_size_rel' => 2,
				'font_weight'   => 'normal',
				'font_style'    => '',
				'font_color'    => array(
					'type' => '',
					'name' => '',
				),
			),
			'title_field'      => null,
			'title_font'       => array(
				'font_size'     => 'rel',
				'font_size_rel' => 1.4,
				'font_weight'   => 'bold',
				'font_style'    => '',
				'font_color'    => array(
					'type' => '',
					'name' => '',
				),
			),
			'title_align'      => 'center',
			'subtitle_field'   => null,
			'subtitle_font'    => array(
				'font_size'     => 'rel',
				'font_size_rel' => 0.9,
				'font_weight'   => 'light',
				'font_style'    => '',
				'font_color'    => array(
					'type' => '',
					'name' => '',
				),
			),
			'subtitle_align'   => 'center',
			'content'          => null,
			'content_align'    => 'start',
			'add_shadow'       => 'md',
			'hover_zoom'       => true,
			'hover_brighten'   => true,
		);

		$hover_brighten = $hover_zoom = false;

		// Init card
		$html       = array();
		$card_class = DRTS_BS_PREFIX . 'card';
		$card_style = 'width:' . $options['width'] . '%;height:100%;';
		if ( ! empty( $options['no_border'] ) ) {
			$card_class .= ' ' . DRTS_BS_PREFIX . 'border-0';
			$card_style .= 'border-radius:0;';
		}
		if ( ! empty( $options['add_shadow'] ) ) {
			$card_class .= ' ' . DRTS_BS_PREFIX . 'shadow' . ( $options['add_shadow'] === 'md' ? '' : '-' . $options['add_shadow'] );
		}

		// Add cover field
		if ( ! empty( $options['cover_field'] ) ) {
			if ( ( ! $field = $application->Entity_Field( $entity, $options['cover_field'] ) )
				|| ( ! $value = $entity->getSingleFieldValue( $field->getFieldName() ) )
				|| ( ! $url = $application->Field_Type( $field->getFieldType() )->fieldImageGetUrl( $value, $options['cover_field_size'] ) )
			) {
				$url = $application->System_NoImage( $options['cover_field_size'], true, $entity );
			}
			$hover_brighten          = $options['hover_brighten'];
			$hover_zoom              = $options['hover_zoom'];
			$card_img_class          = empty( $options['image_overlay'] ) ? DRTS_BS_PREFIX . 'card-img-top' : DRTS_BS_PREFIX . 'card-img';
			$options['cover_height'] = (int) $options['cover_height'];
			$cover_image             = '<div class="' . $card_img_class . ' drts-display-element-card-cover'
				. ( $hover_zoom ? ' drts-display-element-hover-zoom' : '' )
				. ( empty( $options['image_overlay'] ) && $hover_brighten ? ' drts-display-element-hover-brighten' : '' )
				. '" style="background-image:url(' . $url . ');height:' . $options['cover_height'] . 'px;"></div>';
			if ( empty( $options['image_overlay'] ) ) {
				$html[] = '<div style="height:' . (int) $options['cover_height'] . 'px;overflow:hidden;" class="'
					. ( $hover_zoom ? 'drts-display-element-hover-zoom-container' : '' )
					. '">' . $cover_image . '</div>';
			} else {
				$html[] = $cover_image;
				if ( $hover_zoom ) {
					$card_class .= ' drts-display-element-hover-zoom-container';
				}
			}
		}

		// Add thumbnail field
		$has_thumbnail = false;
		if ( ! empty( $options['thumbnail_field'] )
			&& ( $field = $application->Entity_Field( $entity, $options['thumbnail_field'] ) )
			&& ( $value = $entity->getSingleFieldValue( $field->getFieldName() ) )
		) {
			$has_thumbnail = true;
			$thumbnail     = '';
			if ( $field->getFieldType() === 'icon' ) {
				$icon_font_css   = $this->_getFontCss( $application, $options['icon_font'], $entity );
				$thumbnail       = '<span class="fa-stack fa-2x ' . $icon_font_css['class'] . '" style="' . $icon_font_css['style'] . '">'
					. '<i class="fa-solid fa-circle fa-stack-2x"></i>'
					. '<i class="' . $value . ' fa-stack-1x fa-inverse"></i></span>';
				$thumbnail_class = DRTS_BS_PREFIX . 'text-' . $options['thumbnail_align'];
			} else {
				$style = 'width:' . $options['thumbnail_width'] . 'px;height:' . $options['thumbnail_width'] . 'px;';
				if ( $options['thumbnail_width'] >= 80 ) {
					$url = $application->Field_Type( $field->getFieldType() )->fieldImageGetUrl( $value, 'thumbnail' );
				} else {
					$url = $application->Field_Type( $field->getFieldType() )->fieldImageGetIconUrl( $value, 'xl' );
				}
				if ( $url ) {
					$thumbnail_class = $options['thumbnail_type'] ?? 'rounded-circle';
					$thumbnail       = '<img class="' . DRTS_BS_PREFIX . $thumbnail_class . '" style="' . $style . 'border-width:' . round( $options['thumbnail_width'] / 20, 2 ) . 'px" src="' . $url . '" alt="' . $application->H( $entity->getTitle() ) . '" />';
				}
				$thumbnail_class = DRTS_BS_PREFIX . 'd-flex ' . DRTS_BS_PREFIX . 'justify-content-' . $options['thumbnail_align'];
			}
			if ( $thumbnail !== '' ) {
				$margin_top = $options['thumbnail_width'] / 2;
				if ( ! empty( $options['cover_field'] ) ) {
					$margin_top *= -1;
				}
			} else {
				$margin_top = 0;
			}
			$html[] = '<div class="drts-display-element-card-thumbnail ' . DRTS_BS_PREFIX . 'mx-3 ' . DRTS_BS_PREFIX . 'z-3 '
				. $thumbnail_class . '" style="margin-top:' . $margin_top . 'px;">'
				. $thumbnail . '</div>';
		}

		$has_title = ! empty( $options['title_field'] )
			&& '' !== ( $title = $this->_getTitle( $application, $entity, $options ) );

		// Start body
		if ( empty( $options['image_overlay'] ) ) {
			// Reduce padding if both thumbnail and title are present and their alignments differ
			$padding_class = '';
			if ( $has_thumbnail
				&& $options['thumbnail_align'] !== 'center'
				&& $has_title
				&& $options['title_align'] !== 'center'
				&& $options['thumbnail_align'] !== $options['title_align']
			) {
				$padding_class = ' ' . DRTS_BS_PREFIX . 'pt-1';
			}
			$html[] = '<div class="' . DRTS_BS_PREFIX . 'card-body' . $padding_class . '">';
		} else {
			$html[] = '<div class="' . DRTS_BS_PREFIX . 'card-img-overlay'
				. ( $hover_brighten ? ' drts-display-element-hover-brighten' : '' ) . '">';
		}

		// Add title
		if ( $has_title ) {
			$title_font_css = $this->_getFontCss( $application, $options['title_font'] );
			$atts           = array(
				'class' => $title_font_css['class'],
				'style' => $title_font_css['style'],
			);
			if ( ! empty( $options['stretch_link'] ) ) {
				$atts['class'] .= ' ' . DRTS_BS_PREFIX . 'stretched-link';
			}
			$margin_class = '';
			if ( ! empty( $options['image_overlay'] ) ) {
				$margin_class = ' ' . DRTS_BS_PREFIX . 'mt-2';
			}
			$html[] = '<div class="' . DRTS_BS_PREFIX . 'card-title ' . DRTS_BS_PREFIX . 'text-' . $options['title_align'] . $margin_class . '" style="' . $title_font_css['style'] . '">'
				. $application->Entity_Permalink(
					$entity,
					array(
						'title' => $title,
						'atts'  => $atts,
					)
				) . '</div>';
		}

		// Add subtitle
		if ( ! empty( $options['subtitle_field'] )
			&& ( $field = $application->Entity_Field( $entity, $options['subtitle_field'] ) )
			&& ( '' !== $subtitle = (string) $application->Field_Type( $field->getFieldType() )->fieldHumanReadableText( $field, $entity ) )
		) {
			$subtitle_font_css = $this->_getFontCss( $application, $options['subtitle_font'] );
			$html[]            = '<div class="drts-display-element-card-subtitle ' . DRTS_BS_PREFIX . 'mt-n2 ' . DRTS_BS_PREFIX . 'mb-2 ' . $subtitle_font_css['class'] . ' ' . DRTS_BS_PREFIX . 'text-' . $options['subtitle_align'] . '" style="' . $subtitle_font_css['style'] . '">'
				. $application->H( $subtitle ) . '</div>';
		}

		// Add content
		if ( isset( $options['content'] ) ) {
			$html[] = '<div class="drts-display-element-card-content ' . DRTS_BS_PREFIX . 'text-' . $options['content_align'] . '">';
			$html[] = $options['content'];
			$html[] = '</div>';
		}

		// End body
		$html[] = '</div>';

		return '<div class="' . $card_class . '" style="' . $card_style . '">' . implode( PHP_EOL, $html ) . '</div>';
	}

	protected function _getTitle( Application $application, IEntity $entity, array $options ) {
		if ( $field = $application->Entity_Field( $entity, $options['title_field'] ) ) {
			$title = (string) $application->Field_Type( $field->getFieldType() )->entityFieldTypeGetTitle( $field, $entity );
			$title = trim( $title );
		} else {
			$title = '';
		}
		if ( $title === '' ) {
			$title = $entity->getTitle();
		}

		return $title;
	}

	protected function _getFontCss( Application $application, array $fontSettings, ?IEntity $entity = null ) {
		$classes = array();
		if ( ! empty( $fontSettings['font_weight'] ) ) {
			$classes[] = DRTS_BS_PREFIX . 'fw-' . $fontSettings['font_weight'];
		}
		if ( ! empty( $fontSettings['font_style'] ) && $fontSettings['font_style'] === 'italic' ) {
			$classes[] = DRTS_BS_PREFIX . 'fst-italic';
		}
		$styles = array();
		if ( ! empty( $fontSettings['font_size'] ) ) {
			if ( $fontSettings['font_size'] === 'rel' ) {
				$font_size        = ! empty( $fontSettings['font_size_rel'] ) ? $fontSettings['font_size_rel'] : 1;
				$font_size_suffix = $application->getPlatform()->getCssRelSize();
			} else {
				$font_size        = ! empty( $fontSettings['font_size_abs'] ) ? $fontSettings['font_size_abs'] : 16;
				$font_size_suffix = 'px';
			}
			$styles[] = 'font-size:' . $font_size . $font_size_suffix;
		}
		if ( ! empty( $fontSettings['font_color'] ) ) {
			switch ( $fontSettings['font_color']['type'] ) {
				case 'custom':
					if ( ! empty( $fontSettings['font_color']['value'] ) ) {
						$styles[] = 'color:' . $fontSettings['font_color']['value'];
					}
					break;
				case 'field':
					if ( ! empty( $fontSettings['font_color']['field'] )
						&& isset( $entity )
						&& ( $field_value = $entity->getSingleFieldValue( $fontSettings['font_color']['field'] ) )
					) {
						$styles[] = 'color:' . $field_value;
					}
					break;
				default:
					if ( ! empty( $fontSettings['font_color']['name'] ) ) {
						$classes[] = DRTS_BS_PREFIX . 'text-' . $fontSettings['font_color']['name'];
					}
					break;
			}
		}

		return array(
			'class' => implode( ' ', $classes ),
			'style' => empty( $styles ) ? '' : implode( '; ', $styles ) . ';',
		);
	}

	protected function _getColorCssStyle( array $colorSettings, ?IEntity $entity = null ) {
		if ( ! empty( $colorSettings['type'] ) ) {
			if ( $colorSettings['type'] === '_custom' && ! empty( $colorSettings['custom'] ) ) {
				$color = $colorSettings['custom'];
			} elseif ( isset( $entity )
					&& ( $field_value = $entity->getSingleFieldValue( $colorSettings['type'] ) )
				) {

					$color = $field_value;
			}
		}
		return isset( $color ) ? 'color:' . $color . ';' : '';
	}
}
