<?php
/**
 * Rating - Template
 * 
 * @package OrokuBlocks
 * @author OrokuBlocks (https://orokublocks.com/)
 * 
 * @since 1.0.0
 * 
 * @param array $args {
 *   @type string $additional_wrapper_classes       Classes to add to the wrapper.
 *   @type int    $rating_value                     Rating value.
 *   @type int    $rating_max_value                 Rating max value.
 *   @type string $icon                             Name of the icon to use. Optional. Default: 'star'.
 *   @type string $size                             Rating icon size. One of: 'small'.
 * }
 */

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

$defaults = [
  'additional_wrapper_classes'  => false,
  'decimal_points'              => 0,
  'icon'                        => 'star',
  'size'                        => ''
];

$options = array_merge( $defaults, $args );

$wrapper_classes = [
  'orokublocks-rating'
];

if ( $options['additional_wrapper_classes'] ) {
  $wrapper_classes[] = $options['additional_wrapper_classes'];
}

if ( $options['size'] !== '' ) {
  $wrapper_classes[] = 'orokublocks-rating--size--' . $options['size'];
}

$star_count = 5;

$star_score = ( $options['rating_value'] * $star_count ) / $options['rating_max_value'];

$filled_star_count = floor( $star_score );
$partial_star = is_float( $star_score );

$empty_star_count = $star_count - $filled_star_count;

if ( $partial_star ) {
  $empty_star_count -= 1;
}

?>
<div class="<?php echo esc_attr( implode( ' ', $wrapper_classes ) ); ?>">
<?php for ( $i = 0; $i < $filled_star_count; $i++ ) : ?>
  <div class="orokublocks-rating__item">
  <?php

    /**
     * Rating Icon
     */
    orokublocks()->requireTemplate(
      'includes/template/svg',
      [
        'additional_wrapper_classes'  => 'orokublocks-rating__icon orokublocks-rating__icon--filled',
        'icon'                        => $options['icon']
      ]
    );

  ?>
  </div>
<?php endfor; ?>

<?php if ( $partial_star ) : ?>
<div class="orokublocks-rating__item">
<?php

  $partial_decimals = $star_score - $filled_star_count;
  $partial_percentage = $partial_decimals * 100;

  /**
   * Rating Icon
   */
  orokublocks()->requireTemplate(
    'includes/template/svg',
    [
      'additional_wrapper_classes'  => 'orokublocks-rating__icon orokublocks-rating__icon--partial',
      'icon'                        => $options['icon'],
      'linear_gradient'             => [
        'id'    => 'rating-icon-gradient-' . OrokuBlocks::generateId( 'rating' ),
        'stops' => [
          [
            'offset'  => 0,
            'color'   => 'var(--rating-color)'
          ],
          [
            'offset'  => $partial_percentage,
            'color'   => 'var(--rating-color)'
          ],
          [
            'offset'  => $partial_percentage,
            'color'   => 'var(--rating-underline-color)'
          ],
          [
            'offset'  => 100,
            'color'   => 'var(--rating-underline-color)'
          ]
        ]
      ]
    ]
  );

?>
</div>
<?php endif; ?>

<?php for ( $i = 0; $i < $empty_star_count; $i++ ) : ?>
  <div class="orokublocks-rating__item">
  <?php

    /**
     * Rating Icon
     */
    orokublocks()->requireTemplate(
      'includes/template/svg',
      [
        'additional_wrapper_classes'  => 'orokublocks-rating__icon orokublocks-rating__icon--empty',
        'icon'                        => $options['icon']
      ]
    );

  ?>
  </div>
<?php endfor; ?>
</div>