<template>
  <div class="video-scroll" ref="root">
    <img class="visible loaded"
         :src="definition.files[0]"
         :style="{
              top: `${Math.round(definition.top)}px`,
              left: `${Math.round(definition.left)}px`,
              width: isHorizontal ? `${Math.round(definition.width)}px` : 'auto',
              height: isHorizontal ? 'auto' : `${Math.round(definition.height)}px`,
              rotate: `${definition.angle}deg`
            }"
    />

    <!--suppress RequiredAttributes -->
    <img v-for="file in dynamicFiles"
         :data-src="file.src"
         :data-direction="definition.direction"
         :style="{
             top: `${Math.round(file.top)}px`,
             left: `${Math.round(file.left)}px`,
             width: `${Math.round(definition.width)}px`,
             height: `${Math.round(definition.height)}px`,
             rotate: `${definition.angle}deg`
           }"
    />
  </div>
</template>

<script lang="ts">
import {defineComponent, onMounted, ref} from "vue";
import {rotate} from "@/utils";

export default defineComponent({
  name: "VideoScroll",
  props: {
    definition: {
      type: Object,
      required: true
    }
  },
  computed: {
    dynamicFiles(): { [key: string]: string } {
      return this.definition.files.slice(1).map((src: string, idx: number) => {
        const cy = this.definition.top +
            (this.isHorizontal ? 0 : (this.definition.height * (idx + 1) * this.direction));
        const cx = this.definition.left +
            (this.isHorizontal ? (this.definition.width * (idx + 1) * this.direction) : 0);
        const [left, top] = rotate(cx, cy, this.definition.left, this.definition.top, this.definition.angle);
        return {top, left, src};
      });
    },
    isHorizontal(): boolean {
      return this.definition.direction === "left" || this.definition.direction === "right";
    },
    direction(): number {
      return (this.definition.direction === "right" || this.definition.direction === "down") ? 1 : -1;
    }
  },
  setup(props) {
    const root = ref<Element | null>(null);

    console.debug(`[VIDEOSCROLL] Initializing ${props.definition.files[0]}...`);
    console.debug(props.definition);

    onMounted(() => {
      const observer = new IntersectionObserver((entries, observer) => {
        entries.forEach((entry) => {
          const element = entry.target as HTMLImageElement;
          if (entry.isIntersecting) {
            if (!element.src) {
              console.debug(`[VIDEOSCROLL] Intersected, loading ${element.dataset.src}`);
              element.src = element.dataset.src!;
              if (element.dataset.direction == "left" || element.dataset.direction == "right") {
                element.style.height = "auto";
              } else {
                element.style.width = "auto";
              }
              element.onload = () => {
                element.classList.add("loaded");
              };
            }
          }
        });
      });
      Array.from((root.value as Element).children).forEach((el) => {
        observer.observe(el);
      });
    });

    return {
      root
    };
  }
});

export enum VideoScrollDirection {
  RIGHT = "right",
  LEFT = "left",
  UP = "up",
  DOWN = "down"
}

export interface VideoScrollDef {
  top: number,
  left: number,
  angle: number,
  width: number,
  height: number,
  direction: VideoScrollDirection,
  files: string[]
}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.video-scroll img {
  position: absolute;
  image-rendering: optimizeSpeed;
  opacity: 0;
  transition: opacity .5s;
}

.loaded {
  opacity: 1 !important;
}

</style>
