<?php

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

/**
 * GamiPress Achievement V1 - Template
 * 
 * @package OrokuBlocks
 * @author OrokuBlocks (https://orokublocks.com/)
 * 
 * @since 1.0.0
 * 
 * @param array $args {
 *   @type string $theme                                    Card color scheme, one of: 'light', 'dark'.
 * 
 *   @type string $light_primary_color                      Light theme primary color. Used when $theme is set to 'light'.
 *   @type string $light_background_color                   Light theme background color. Used when $theme is set to 'light'.
 *   @type string $light_background_alt_color               Light theme alternate background color. Used when $theme is set to 'light'.
 *   @type string $light_line_color                         Light theme line color. Used when $theme is set to 'light'.
 *   @type string $light_text_color                         Light theme text color. Used when $theme is set to 'light'.
 * 
 *   @type string $dark_primary_color                       Dark theme primary color. Used when $theme is set to 'dark'.
 *   @type string $dark_background_color                    Dark theme background color. Used when $theme is set to 'dark'.
 *   @type string $dark_background_alt_color                Dark theme alternate background color. Used when $theme is set to 'dark'.
 *   @type string $dark_line_color                          Dark theme line color. Used when $theme is set to 'dark'.
 *   @type string $dark_text_color                          Dark theme text color. Used when $theme is set to 'dark'.
 * 
 *   @type string $progress_bar_color                       Progress bar color.
 *   @type string $progress_bar_underline_color             Progress bar underline color.
 *   @type bool   $progress_bar_gradient                    True to use a gradient for the progress bar, false otherwise.
 *   @type string $progress_bar_gradient_start_color        Progress bar gradient start color.
 *   @type string $progress_bar_gradient_end_color          Progress bar gradient end color.
 * 
 *   @type bool   $use_plugin_font                          True to use plugin font, false otherwise.
 *   @type int    $font_scale                               Font size scale value.
 *   @type int    $card_border_radius                       Card border radius.
 *   @type int    $width                                    Card width, a value of 0 to lets the card auto adjust size.
 * 
 *   @type string $cover_image_url                          Card cover image URL.
 *   @type string $image_url                                Card image URL.
 * 
 *   @type string $title                                    Title.
 *   @type string $summary                                  Summary.
 * 
 *   @type string $tag_image_url                            Tag image URL.
 *   @type string $tag_text                                 Tag text.
 * 
 *   @type int    $score_value                              Score value.
 *   @type int    $score_max_value                          Score max value.
 *   @type int    $decimal_points                           Amount of decimal points for the score.
 *   
 *   @type string $progress_text                            Progress text.
 *   @type string $locked_text                              Locked text.
 *   @type string $unlocked_text                            Unlocked text.
 * 
 *   @type int    $progress_bar_width                       Progress bar width.
 *   @type int    $progress_bar_breakpoint_width            Progress bar width used for mobile.
 *   @type int    $progress_bar_split_parts                 Number of parts to split the progress bar into.
 *   @type bool   $animate_on_scroll                        True to animate score progress on scroll, false otherwise.
 * 
 *   @type int    $earners_count                            Earners count.
 *   @type string $earners_text                             Earners text.
 *   @type array  $earners                                  {
 *     @type string $image_url        Earner image url.
 *   }
 * }
 */

$defaults = orokublocks()->widgets['gamipress_achievement']['instance']->getDefaultOptions();

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

$cover_image_enabled = $options['cover_image_url'] !== '';

$tag_text = $options['tag_text'];
$tag_text_number = intval( $tag_text );

if ( $tag_text_number > 0 ) {
  $tag_text = '+ ' . $tag_text;
} else if ( $tag_text_number < 0 ) {
  $tag_text = '- ' . absint( $tag_text );
}

$progress_bar_container_classes = $options['shortcode'] ? 'xm-progress-bar_shortcode' : 'xm-progress-bar';
$progress_bar_text_container_classes = $options['shortcode'] ? 'bar-progress-wrap_shortcode' : 'bar-progress-wrap';

/**
 * Card Opening Tag
 */
orokublocks()->requireTemplate(
  'widgets/gamipressachievement/template/card-opening-tag',
  [
    'version' => 'v1',
    'options' => $options
  ]
);

?>
  <!-- CARD -->
  <div class="orokublocks-card">
  <?php if ( $cover_image_enabled ) : ?>
    <div class="orokublocks-card__content orokublocks-card__content--padding--none orokublocks-card__content--bordered orokublocks-card__content--border--bottom--none orokublocks-card__content--border--bottom--flat">
    <?php

      /**
       * Card Image
       */
      orokublocks()->requireTemplate(
        'includes/template/card-image',
        [
          'image_url' => $options['cover_image_url']
        ]
      );

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

    if ( ( $tag_text !== '' ) && ( $tag_text_number !== 0 ) ) {
      /**
       * Point Tag Box
       */
      orokublocks()->requireTemplate(
        'includes/template/point-tag-box',
        [
          'tag_text'      => $tag_text,
          'tag_image_url' => $options['tag_image_url'],
          'position'      => [ 'top', 'right' ]
        ]
      );
    }

  ?>
    <div class="orokublocks-card__content orokublocks-card__content--bordered<?php if ( $cover_image_enabled ) : ?> orokublocks-card__content--border--top--none orokublocks-card__content--border--top--flat<?php else : ?> orokublocks-card__item--centered<?php endif; ?>">
      <div class="orokublocks-card__item orokublocks-card__item--divided orokublocks-card__item--gap--huge orokublocks-card__item--direction--vertical">
      <?php if ( $cover_image_enabled ) : ?>
        <div class="orokublocks-card__content orokublocks-card__content--padding--none orokublocks-card__content--layered">
      <?php endif; ?>
          <div class="orokublocks-card__content orokublocks-card__content--padding--inner">
            <div class="orokublocks-card__item orokublocks-card__item--divided orokublocks-card__item--gap orokublocks-card__item--direction--vertical orokublocks-card__item--align--center orokublocks-card__item--centered">
            <?php if ( $options['image_url'] !== '' ) : ?>
              <div class="orokublocks-card__item<?php if ( $cover_image_enabled ) : ?> orokublocks-card__item--style--floating--top<?php endif; ?>">
              <?php

                /**
                 * Card Image
                 */
                orokublocks()->requireTemplate(
                  'includes/template/card-image',
                  [
                    'image_url' => $options['image_url'],
                    'size'      => 'badge'
                  ]
                );

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

              <div class="orokublocks-card__item">
                <h2 class="orokublocks-card__title orokublocks-card__title--line--height--small"><?php echo esc_html( $options['title'] ); ?></h2>
              </div>

            <?php if ( $options['summary'] !== '' ) : ?>
              <div class="orokublocks-card__item">
                <div class="orokublocks-card__text orokublocks-card__text--line--height"><?php echo wp_kses_post( $options['summary'] ); ?></div>
              </div>
            <?php endif; ?>
            </div>
          </div>
      <?php if ( $cover_image_enabled ) : ?>
        </div>
      <?php endif; ?>
        <div class="orokublocks-card__content orokublocks-card__content--padding--inner orokublocks-card__content--padding--top--none">
          <div class="orokublocks-card__item orokublocks-card__item--divided orokublocks-card__item--gap--smaller orokublocks-card__item--direction--vertical orokublocks-card__item--align--center">
            <div class="orokublocks-card__item">
            <?php if ( $options['score_value'] !== '' ) : ?>
              <h2 class="orokublocks-card__text orokublocks-card__text--size--small orokublocks-card__text--style--bold <?php echo esc_attr( $progress_bar_text_container_classes ); ?>">
                <span class="bar-progress-text"><?php echo esc_html( $options['score_value'] ); ?></span>
                <?php echo esc_html( $options['progress_text'] ); ?>
              </h2>
            <?php endif; ?>
            </div>

            <!-- PROGRESS BAR -->
            <div class="<?php echo esc_attr( $progress_bar_container_classes ); ?>"></div>
            <!-- /PROGRESS BAR -->
          </div>
        </div>
      </div>
    </div>

    <div class="orokublocks-card__content orokublocks-card__content--bordered orokublocks-card__content--adjacent orokublocks-card__content--adjacent--small orokublocks-card__content--theme--darker">
      <div class="orokublocks-card__item orokublocks-card__item--divided orokublocks-card__item--gap--small orokublocks-card__item--align--center orokublocks-card__item--justify--space--between">
        <div class="orokublocks-card__item">
          <p class="orokublocks-card__text orokublocks-card__text--size--small orokublocks-card__text--style--bold">
            <span class="orokublocks-card__text--style--highlighted"><?php echo esc_html( $options['earners_count'] ); ?></span>
            <?php echo esc_html( $options['earners_text'] ); ?>
          </p>
        </div>
      <?php

        /**
         * Overlayed Avatars
         */
        orokublocks()->requireTemplate(
          'includes/template/overlayed-avatars',
          [
            'avatars' => $options['earners']
          ]
        );

      ?>
      </div>
    </div>
  </div>
  <!-- /CARD -->
</div>