<?php

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

/**
 * WooCommerce Product 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 $rating_color                             Rating icon color.
 *   @type string $rating_underline_color                   Rating underlying icon 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    $card_image_border_radius                 Card image border radius.
 *   @type int    $width                                    Card width, a value of 0 to lets the card auto adjust size.
 * 
 *   @type string $image_url                                Card image URL.
 * 
 *   @type array  $gallery_image_urls                       {
 *     @type string $gallery_image_url                      Gallery image URL.
 *   }
 * 
 *   @type string $title                                    Title.
 *   @type string $title_link                               Title link URL.
 *   @type string $subtitle                                 Subtitle.
 *   
 *   @type string $price                                    Price text.
 *   @type string $previous_price                           Previous price text.
 * 
 *   @type int    $score_value                              Score value.
 *   @type int    $score_max_value                          Score max value.
 * 
 *   @type string $attributes_display                       Comma separated list of ids of attributes to display.
 *   @type array  $attributes                               {
 *     @type string $name           Attribute name.
 *     @type array  $options        Attribute options
 *   }
 *   @type array  $colors                               {
 *     @type string $name           Color name.
 *     @type array  $options        Color options.
 *     @type array  $values         Color values.
 *   }
 * 
 *   @type string $footer_left_title                        Footer left title.
 *   @type bool   $share_facebook_enabled                   True to display facebook share link, false otherwise.
 *   @type bool   $share_x_enabled                          True to display x share link, false otherwise.
 * }
 */

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

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

$gallery_image_count = count( $options['gallery_image_urls'] );
$has_gallery = $gallery_image_count > 0;

$attribute_count = count( $options['attributes'] );
$has_attributes = $attribute_count > 0;
$has_colors = array_key_exists( 'values', $options['colors'] ) && ( count( $options['colors']['values'] ) > 0 );

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

?>
  <!-- CARD -->
  <div class="orokublocks-card">
    <div class="orokublocks-card__content orokublocks-card__content--bordered<?php if ( $has_attributes || $has_colors ) : ?> orokublocks-card__content--border--bottom--flat<?php endif; ?>">
    <?php

      if ( ! $has_gallery ) {
        /**
         * Card Image
         */
        orokublocks()->requireTemplate(
          'includes/template/card-image',
          [
            'image_url' => $options['image_url'],
            'size'      => 'regular'
          ]
        );
      }

    ?>
    <?php if ( $has_gallery ) : ?>
      <div class="orokublocks-glide-carousel-container">
        <div class="glide orokublocks-glide-carousel">
          <div class="glide__track" data-glide-el="track">
            <ul class="glide__slides">
            <?php foreach ( $options['gallery_image_urls'] as $gallery_image_url ) : ?>
              <li class="glide__slide">
              <?php

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

              ?>
              </li>
            <?php endforeach; ?>
            </ul>
          </div>

          <div class="orokublocks-bullets orokublocks-bullets--style--floaty glide__bullets" data-glide-el="controls[nav]">
          <?php for ( $i = 0; $i < $gallery_image_count; $i++ ) : ?>
            <button class="orokublocks-bullet glide__bullet" data-glide-dir="=<?php echo esc_attr( $i ); ?>"></button>
          <?php endfor; ?>
          </div>
        </div>
      </div>
    <?php endif; ?>
      <div class="orokublocks-card__content orokublocks-card__content--padding--inner orokublocks-card__content--padding--top--regular">
        <div class="orokublocks-card__item orokublocks-card__item--divided orokublocks-card__item--direction--vertical<?php if ( $options['score_value'] <= 0 ) : ?> orokublocks-card__item--gap--micro<?php endif; ?>">
          <div class="orokublocks-card__item orokublocks-card__item--divided orokublocks-card__item--gap--tiny orokublocks-card__item--direction--vertical">
          <?php if ( $options['subtitle'] !== '' ) : ?>
            <div class="orokublocks-card__item">
              <p class="orokublocks-card__text orokublocks-card__text--size--regular"><?php echo esc_html( $options['subtitle'] ); ?></p>
            </div>
          <?php endif; ?>

            <div class="orokublocks-card__item">
            <?php if ( $options['title_link'] !== '' ) : ?>
              <a class="orokublocks-card__link" href="<?php echo esc_url( $options['title_link'] ); ?>">
            <?php endif; ?>
              <h2 class="orokublocks-card__title orokublocks-card__title--size--small orokublocks-card__title--line--height--small"><?php echo esc_html( $options['title'] ); ?></h2>
            <?php if ( $options['title_link'] !== '' ) : ?>
              </a>
            <?php endif; ?>
            </div>

          <?php if ( $options['score_value'] > 0 ) : ?>
            <div class="orokublocks-card__item">
            <?php

              /**
               * Rating
               */
              orokublocks()->requireTemplate(
                'includes/template/rating',
                [
                  'rating_value'      => $options['score_value'],
                  'rating_max_value'  => $options['score_max_value'],
                  'size'              => 'small'
                ]
              );

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

          <div class="orokublocks-card__item orokublocks-card__item--divided orokublocks-card__item--gap--tiny orokublocks-card__item--align--bottom orokublocks-card__item--justify--space--between">
          <?php if ( $options['price'] !== '' ) : ?>
            <div class="orokublocks-card__item<?php if ( $options['previous_price'] !== '' ) : ?> orokublocks-card__item--divided orokublocks-card__item--gap--tiny orokublocks-card__item--align--bottom<?php endif; ?>">
              <p class="orokublocks-card__title orokublocks-card__title--size--small"><?php echo esc_html( $options['price'] ); ?></p>
            <?php if ( $options['previous_price'] !== '' ) : ?>
              <p class="orokublocks-card__text orokublocks-card__text--size--regular orokublocks-card__text--no--space orokublocks-text-line-through"><?php echo esc_html( $options['previous_price'] ); ?></p>
            <?php endif; ?>
            </div>
          <?php endif; ?>
          <?php

            /**
             * Symbol Button
             */
            orokublocks()->requireTemplate(
              'includes/template/symbol-button',
              [
                'icon'  => 'bag',
                'href'  => $options['title_link']
              ]
            );

          ?>
          </div>
        </div>
      </div>
    </div>

  <?php if ( $has_colors ) : ?>
    <div class="orokublocks-card__content orokublocks-card__content--bordered orokublocks-card__content--adjacent orokublocks-card__content--adjacent--small<?php if ( $has_attributes ) : ?> orokublocks-card__content--border--bottom--flat<?php endif; ?>">
      <div class="orokublocks-card__item orokublocks-card__item--divided orokublocks-card__item--gap--tiny 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--regular"><?php echo esc_html( $options['colors']['name'] ); ?></p>
        </div>
      <?php

        /**
         * Color Bullets
         */
        orokublocks()->requireTemplate(
          'includes/template/color-bullets',
          [
            'colors'  => $options['colors']['values']
          ]
        );

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

  <?php foreach ( $options['attributes'] as $attribute_id => $attribute ) : ?>
    <div class="orokublocks-card__content orokublocks-card__content--bordered orokublocks-card__content--adjacent orokublocks-card__content--adjacent--small<?php if ( $attribute_id !== array_key_last( $options['attributes'] ) ) : ?> orokublocks-card__content--border--bottom--flat<?php endif; ?>">
    <?php

      /**
       * Card Pill Options
       */
      orokublocks()->requireTemplate(
        'includes/template/card-pill-options',
        [
          'title'   => $attribute['name'],
          'options' => $attribute['options']
        ]
      );

    ?>
    </div>
  <?php endforeach; ?>

  <?php

    /**
     * Card Footer
     */
    orokublocks()->requireTemplate(
      'includes/template/card-footer',
      [
        'title' => $options['footer_left_title'],
        'share' => [
          'share_facebook_enabled'  => $options['share_facebook_enabled'],
          'share_x_enabled'         => $options['share_x_enabled'],
          'share_text'              => $options['title'],
          'share_url'               => $options['title_link']
        ]
      ]
    );

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