<?php

/**
 * Checkbox fields should have default value set to false. Because if
 * set to true & checkbox is not checked, function "inoby_parse_args"
 * will evaluate the field as thruthy because it is not present in
 * the fieldlist and therefore the default value will be used.
 *
 * If you want to enable some features by default, you should
 * add ["std" => "on"] to metabox checkbox field.
 * @package inobydoc
 */
$defaults = apply_filters("inoby_slider_gallery_default_settings", [
  "has_lightbox" => false,
  "has_thumbs" => false,
  "has_arrows" => false,
  "per_view" => 4,
]);
$settings = inoby_parse_args(mb_get_block_field("settings"), $defaults, true);
$gallery_id = uniqid("inoby-slider-gallery-");
$heading = mb_get_block_field("heading");
?>

<div <?= inoby_block_attrs($attributes, [
  "class" => "inoby-slider-gallery",
  "id" => $gallery_id,
  "data-settings" => json_encode($settings),
]) ?>>
  <div class="container">
    <?php if($heading): ?>
    <div class="row row-head">
      <div class="col-12">
        <h2 class="underlined"><?= $heading ?></h2>
      </div>
    </div>
    <?php endif; ?>
    <?php if ($settings["has_thumbs"]): ?>
    <?php
      $images = mb_get_block_field("images", ["size" => "h-12"]);
      $thumbs = mb_get_block_field("images", ["size" => "s-2"]);
      ?>
    <div class="keen-slider gallery">
      <?php foreach ($images as $image): ?>
      <?php if ($settings["has_lightbox"]): ?>
      <a href="<?= $image[
              "full_url"
            ] ?>" class="gallery-slide glightbox-slide" data-gallery="<?= $gallery_id ?>">
        <?= __img(
                $image["url"],
                $image["width"],
                $image["height"],
                $image["alt"],
              ) ?>
      </a>
      <?php else: ?>
      <div class="gallery-slide">
        <?= __img(
                $image["url"],
                $image["width"],
                $image["height"],
                $image["alt"],
              ) ?>
      </div>
      <?php endif; ?>
      <?php endforeach; ?>
    </div>
    <div class="keen-slider thumbs">
      <?php foreach ($thumbs as $image): ?>
      <div class="thumb">
        <?= __img(
              $image["url"],
              $image["width"],
              $image["height"],
              $image["alt"],
            ) ?>
      </div>
      <?php endforeach; ?>
    </div>
    <?php else: ?>
    <?php $images = mb_get_block_field("images", ["size" => "h-6"]); ?>
    <div class="keen-slider gallery">
      <?php foreach ($images as $image): ?>
      <?php if ($settings["has_lightbox"]): ?>
      <a href="<?= $image[
              "full_url"
            ] ?>" class="gallery-slide glightbox-slide" data-gallery="<?= $gallery_id ?>">
        <?= __img(
                $image["url"],
                $image["width"],
                $image["height"],
                $image["alt"],
              ) ?>
      </a>
      <?php else: ?>
      <div class="gallery-slide">
        <?= __img(
                $image["url"],
                $image["width"],
                $image["height"],
                $image["alt"],
              ) ?>
      </div>
      <?php endif; ?>
      <?php endforeach; ?>
    </div>
    <?php endif; ?>
  </div>
</div>