<script lang="ts">
  import { onMount } from "svelte";
  import { fade } from "svelte/transition";
  import normalizeWheel from "@helpers/normalizeWheel";
  type Image = {
    exif: string[];
    src: string;
    alt: string;
    sizes: string;
    original: string;
    loaded: string;
    originalLoaded: boolean;
    startedLoading: boolean;
  };
  let images: Image[] = [];
  let progress: number[] = [];
  let currentIndex = -1;
  const maxZoom = 5;
  import { useSwipe } from "svelte-gestures";

  const mod = (a: number, b: number) => ((a % b) + b) % b;

  const lerp = (a: number, b: number, t: number) => a * t + b * (1 - t);

  const addIndex = (offset: number) => {
    return setIndex(mod(currentIndex + offset, images.length));
  };

  let mx = 0,
    my = 0,
    _mx = 0,
    _my = 0,
    scale = 1;

  const setIndex = (index: number) => {
    mx = window.innerWidth / 2;
    my = window.innerHeight / 2;
    scale = 1;
    if (index < 0) {
      document.body.style.overflowY = "auto";
    } else {
      document.body.style.overflowY = "hidden";
    }
    currentIndex = index;
  };

  const handleKeyDown = ({ key }: KeyboardEvent) => {
    if (currentIndex < 0) return;
    if (key === "Escape" && currentIndex > -1) setIndex(-1);
    if (key === "ArrowLeft") addIndex(-1);
    if (key === "ArrowRight") addIndex(+1);
  };

  const handleOriginalLoading = async (image: Image) => {
    if (!image.startedLoading) {
      image.startedLoading = true;
      let cIndex = currentIndex;
      if (image.original) {
        const response = await fetch(image.original, {
          headers: {
            "Accept-Encoding": "gzip, deflate, br",
            "Content-Type":
              "image/avif,image/webp,image/apng,image/svg+xml,image/jpeg,image/png,image/*,*/*;q=0.8",
          },
        });
        const total = Number(response.headers.get("content-length"));
        const reader = response?.body?.getReader();
        if (!reader) return;
        let bytesReceived = 0;
        let chunks = [];
        console.log("[SLIDER] started loading " + image.original);
        while (true) {
          const { done, value } = await reader.read();
          if (done) {
            console.log("[SLIDER] Image complete");
            break;
          }
          chunks.push(value);
          if (total) {
            bytesReceived += value.length;
            progress[cIndex] = bytesReceived / total;
            console.log(
              "[SLIDER] " + Math.floor(progress[cIndex] * 1000) / 10 + "%",
            );
            progress = progress;
          }
        }

        const b = new Blob(chunks);
        images[cIndex].loaded = URL.createObjectURL(b);
        images = images;
      }
    }
  };

  function handleSwipe(ev: any) {
    if (currentIndex === -1) return;
    if (ev.detail.direction === "right") addIndex(-1);
    if (ev.detail.direction === "left") addIndex(+1);
  }

  const handleScroll = (ev: WheelEvent) => {
    const { pixelY } = normalizeWheel(ev);
    scale = Math.min(Math.max(scale - pixelY / 100, 1), maxZoom);
    if (scale > 3) {
      handleOriginalLoading(images[currentIndex]);
    }
  };

  let isUpdating = false;
  const update = () => {
    isUpdating && requestAnimationFrame(update);

    let dist = Math.abs(mx - _mx) + Math.abs(my - _my);

    if (dist < 0.1 || scale == 1) {
      mx = _mx;
      my = _my;
      isUpdating = false;
    } else {
      let s = (0.2 + (1 - scale / maxZoom) * 0.8) * 0.3;
      mx = lerp(mx, _mx, 1 - s);
      my = lerp(my, _my, 1 - s);
    }
  };

  const startUpdate = () => {
    if (!isUpdating && scale !== 1) {
      isUpdating = true;
      update();
    }
  };

  const handleMouseMove = (ev: MouseEvent) => {
    _mx = ev.clientX;
    _my = ev.clientY;
    startUpdate();
  };

  const handlePointerMove = () => {
    // console.log(ev);
  };

  function formatExposureTime(num: string) {
    if (num.includes("/")) {
      const [a, b] = num.split("/");
      return `${a}/${b}s`;
    } else {
      return num + "s";
    }
  }

  onMount(() => {
    const wrappers = Array.prototype.slice.call(
      document.querySelectorAll("picture > img"),
    );

    images = wrappers.map((image, i: number) => {
      image.classList.add("image-active");
      image.addEventListener("click", () => setIndex(i));
      image.addEventListener("touch", () => setIndex(i));
      image.style.cursor = "pointer";

      image.addEventListener("error", () => {
        console.log("Error loading", image);
      });

      let exif: string[] = [];

      try {
        const rawExif = image.getAttribute("data-exif");
        const exifData = JSON.parse(rawExif);
        if (exifData) {
          exif = [
            "Model" in exifData ? exifData.Model : "",
            "FocalLength" in exifData
              ? exifData.FocalLength.replace(" mm", "mm")
              : "",
            "FNumber" in exifData ? exifData.FNumber : "",
            "ExposureTime" in exifData
              ? formatExposureTime(exifData.ExposureTime)
              : "",
          ];
        }
      } catch (error) {
        // No biggie
      }

      return {
        exif,
        startedLoading: false,
        loaded: "",
        src: image.getAttribute("srcset"),
        alt: image.getAttribute("alt"),
        sizes: image.getAttribute("sizes"),
        original: image.getAttribute("data-original"),
        originalLoaded: false,
      };
    });

    return () => {
      wrappers.forEach(({ children: [_, image] }, i: number) => {
        image.removeEventListener("click", () => setIndex(i));
      });
    };
  });
</script>

<svelte:window on:keydown={handleKeyDown} />

<div class="image-wrapper">
  <slot />
</div>

<div class="gallery-wrapper" class:visible={currentIndex > -1}>
  {#if images.length > 1}
    <div class="controls">
      {#each images as _, i}
        <button
          aria-label={`Image ${i + 1}`}
          class="rounded bg-light"
          class:active={currentIndex === i}
          class:bg-light={currentIndex === i}
          on:click={() => {
            currentIndex = i;
          }}></button>
      {/each}
    </div>
  {/if}

  <button
    class="left flex-1 bg-light"
    aria-label="previous image"
    on:click={() => addIndex(- 1)}>
    <span class="i-tabler-arrow-left" />
  </button>
  <button
    class="right flex-1 bg-light"
    aria-label="next image"
    on:click={() => addIndex( + 1)}>
    <span class="i-tabler-arrow-right" />
  </button>
  <button
    class="close bg-light"
    aria-label="close gallery"
    on:click={() => setIndex(-1)}>
    <span class="i-tabler-x" />
  </button>

  {#if currentIndex > -1}
    <div
      {...useSwipe(handleSwipe)}
      class="image"
      role="dialog"
      on:wheel|passive={handleScroll}
      on:mousemove={handleMouseMove}
      on:pointermove={handlePointerMove}>
      {#if progress[currentIndex] && progress[currentIndex] < 0.99}
        <div
          transition:fade
          id="progress"
          style={`transform: scaleX(${progress[currentIndex]});`} />
      {/if}

      <span>
        <img
          style={`transform: scale(${scale}); transform-origin: ${
            window.innerWidth - mx
          }px ${window.innerHeight - my}px`}
          srcset={images[currentIndex].loaded ? "" : images[currentIndex].src}
          src={images[currentIndex].loaded}
          alt={images[currentIndex].alt} />
      </span>
    </div>
    {#if images[currentIndex].exif}
      {@const exif = images[currentIndex].exif}
      <div class="exif">
        {#each exif as e}
          <span> {e} </span>
        {/each}
      </div>
    {/if}
  {/if}
</div>

<style>
  #progress {
    position: absolute;
    bottom: 0px;
    height: 10px;
    z-index: 99;
    left: 47px;
    background: var(--neutral-800);
    width: calc(100% - 94px);
    transform-origin: left;
    transition: transform 0.3s ease;
  }

  .gallery-wrapper {
    position: fixed;
    z-index: 199;
    top: 0;
    left: 0;
    height: 100vh;
    width: 100vw;
    background: var(--neutral-800);

    opacity: 0;
    transition: opacity 0.1s ease;
    pointer-events: none;
    backdrop-filter: blur(20px);
  }

  .gallery-wrapper.visible {
    pointer-events: all;
    opacity: 1;
  }

  .image {
    position: relative;
    display: grid;
    place-items: center;
    width: 100%;
    height: 100%;
  }

  .image > span {
    height: calc(100vh - 70px);
    width: 95%;
  }

  .image img {
    position: relative;
    max-width: 100vw;
    max-height: 100vh;
    width: 100%;
    height: 100%;
    z-index: 98;
    object-fit: contain;
    image-rendering: pixelated;
    transition:
      transform 0.2s ease,
      transform-origin 0.1s linear;
  }

  .controls {
    width: fit-content;
    left: 50vw;
    transform: translateX(-50%);
    background: var(--neutral-800);
    padding: 5px;
    position: absolute;
    z-index: 99;
    display: flex;
  }

  .controls > button {
    width: 15px;
    height: 15px;
    border-style: none;
    background: transparent;
    border: solid 1px white;
    cursor: pointer;
    margin: 2px;
    padding: 0px;
  }

  .controls > button.active {
    background: white;
  }

  .exif {
    position: absolute;
    bottom: 0px;
    left: 50vw;
    transform: translateX(-50%);
    z-index: 99;
    background: var(--neutral-800);
    color: white;
    padding: 5px;
    padding-inline: 10px;
    font-size: 0.9em;
    white-space: pre;
    display: flex;
  }

  .exif > span {
    padding: 0px 8px;
  }

  .exif > span:last-child {
    border-right: none;
  }

  .gallery-wrapper > button {
    border-radius: 6px;
    padding: 0;
    position: fixed;
    z-index: 200;
    font-size: 1.5rem;
    width: 44px;
    height: 44px;
    display: grid;
    place-items: center;
    cursor: pointer;
    transition: background-color 0.2s;
  }

  .close {
    top: 10px;
    right: 10px;
  }

  .left {
    bottom: 10px;
    left: 10px;
  }

  .right {
    bottom: 10px;
    right: 10px;
  }
</style>
