<template>
  <div
    ref="galleryContainer"
    class="Gallery"
    :class="{ '-long': tooManyAttachments && hidingLong }"
  >
    <div class="gallery-rows">
      <div
        v-for="(row, rowIndex) in rows"
        :key="rowIndex"
        class="gallery-row"
        :style="rowStyle(row)"
        :class="{ '-audio': row.audio, '-minimal': row.minimal, '-grid': grid }"
      >
        <div
          class="gallery-row-inner"
          :class="{ '-grid': grid }"
        >
          <Attachment
            v-for="(attachment, attachmentIndex) in row.items"
            :key="attachment.id"
            class="gallery-item"
            :nsfw="nsfw"
            :attachment="attachment"
            :size="size"
            :editable="editable"
            :remove="removeAttachment"
            :shift-up="!(attachmentIndex === 0 && rowIndex === 0) && shiftUpAttachment"
            :shift-dn="!(attachmentIndex === row.items.length - 1 && rowIndex === rows.length - 1) && shiftDnAttachment"
            :edit="editAttachment"
            :description="descriptions && descriptions[attachment.id]"
            :hide-description="size === 'small' || tooManyAttachments && hidingLong"
            :style="itemStyle(attachment.id, row.items)"
            @setMedia="onMedia"
            @naturalSizeLoad="onNaturalSizeLoad"
          />
        </div>
      </div>
    </div>
    <div
      v-if="tooManyAttachments"
      class="many-attachments"
    >
      <div class="many-attachments-text">
        {{ $t("status.many_attachments", { number: attachments.length }) }}
      </div>
      <div class="many-attachments-buttons">
        <span
          v-if="!hidingLong"
          class="many-attachments-button"
        >
          <button
            class="button-unstyled -link"
            @click="toggleHidingLong(true)"
          >
            {{ $t("status.collapse_attachments") }}
          </button>
        </span>
        <span
          v-if="hidingLong"
          class="many-attachments-button"
        >
          <button
            class="button-unstyled -link"
            @click="toggleHidingLong(false)"
          >
            {{ $t("status.show_all_attachments") }}
          </button>
        </span>
        <span
          v-if="hidingLong"
          class="many-attachments-button"
        >
          <button
            class="button-unstyled -link"
            @click="openGallery"
          >
            {{ $t("status.open_gallery") }}
          </button>
        </span>
      </div>
    </div>
  </div>
</template>

<script src='./gallery.js'></script>

<style lang="scss">
@import '../../_variables.scss';
@import '../../_logical.scss';

.Gallery {
  .gallery-rows {
    display: flex;
    flex-direction: column;
  }

  .gallery-row {
    position: relative;
    block-size: 0;
    inline-size: 100%;
    flex-grow: 1;

    &:not(:first-child) {
      margin-block-start: 0.5em;
    }
  }

  &.-long {
    .gallery-rows {
      max-block-size: 25em;
      overflow: hidden;
      mask:
        linear-gradient(to top, white, transparent) bottom/100% 70px no-repeat,
        linear-gradient(to top, white, white);

      /* Autoprefixed seem to ignore this one, and also syntax is different */
      -webkit-mask-composite: xor;
      mask-composite: exclude;
      .#{$writing-mode-vertical}lr & {
        mask:
          linear-gradient(to left, white, transparent) right/70px 100% no-repeat,
          linear-gradient(to left, white, white);
        mask-composite: exclude;
      }
      .#{$writing-mode-vertical}rl & {
        mask:
          linear-gradient(to right, white, transparent) left/70px 100% no-repeat,
          linear-gradient(to right, white, white);
        mask-composite: exclude;
      }
    }
  }

  .many-attachments-text {
    text-align: center;
    line-height: 2;
  }

  .many-attachments-buttons {
    display: flex;
  }

  .many-attachments-button {
    display: flex;
    flex: 1;
    justify-content: center;
    line-height: 2;

    button {
      padding-block: 0;
      padding-inline: 2em;
    }
  }

  .gallery-row {
    &.-grid,
    &.-minimal {
      block-size: auto;
      .gallery-row-inner {
        position: relative;
      }
    }
  }

  .gallery-row-inner {
    position: absolute;
    inset-block-start: 0;
    inset-inline-start: 0;
    inset-inline-end: 0;
    inset-block-end: 0;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-content: stretch;

    &.-grid {
      inline-size: 100%;
      block-size: auto;
      position: relative;
      display: grid;
      grid-column-gap: 0.5em;
      grid-row-gap: 0.5em;
      grid-template-columns: repeat(auto-fill, minmax(15em, 1fr));

      .gallery-item {
        margin: 0;
      }
    }
  }

  .gallery-item {
    margin-block: 0 0;
    margin-inline: 0 0.5em;
    flex-grow: 1;
    block-size: 100%;
    box-sizing: border-box;
    // to make failed images a bit more noticeable on chromium
    min-inline-size: 2em;
    &:last-child {
      margin: 0;
    }
  }
}
</style>
