<script lang="ts">
  import { Store, Stores, UIEventSource } from "../../../Logic/UIEventSource"

  /**
   * Given the available floors, shows an elevator to pick a single one
   *
   * This is but the input element, the logic of handling the filter is in 'LevelSelector'
   */
  export let floors: Store<string[]>
  export let value: UIEventSource<string>

  const HEIGHT = 40
  const WIDTH = HEIGHT

  let initialIndex = Math.max(
    0,
    floors?.data?.findIndex((f) => f === value?.data) ?? floors?.data?.length + 1
  )
  let index: UIEventSource<number> = new UIEventSource<number>(initialIndex)
  let forceIndex: number | undefined = undefined
  let top = Math.max(0, initialIndex) * HEIGHT

  let mouseDown = false

  let container: HTMLElement

  $: {
    if (top > 0 || forceIndex !== undefined) {
      index.setData(closestFloorIndex())
      value.setData(floors.data[forceIndex ?? closestFloorIndex()])
    }
  }

  function unclick() {
    mouseDown = false
  }

  function click() {
    mouseDown = true
  }

  function closestFloorIndex() {
    return Math.min(floors.data.length - 1, Math.max(0, Math.round(top / HEIGHT)))
  }

  function onMove(e: { movementY: number }) {
    if (mouseDown) {
      forceIndex = undefined
      const containerY = container.clientTop
      const containerMax = containerY + (floors.data.length - 1) * HEIGHT
      top = Math.min(Math.max(0, top + e.movementY), containerMax)
    }
  }

  let momentum = 0

  function stabilize() {
    // Automatically move the elevator to the closest floor
    if (mouseDown) {
      return
    }
    const target = (forceIndex ?? index.data) * HEIGHT
    let diff = target - top
    if (diff > 1) {
      diff /= 3
    }
    const sign = Math.sign(diff)
    momentum = momentum + sign
    let diffR = Math.min(Math.abs(momentum), forceIndex !== undefined ? 9 : 3, Math.abs(diff))
    momentum = Math.sign(momentum) * Math.min(diffR, Math.abs(momentum))
    top += sign * diffR
    if (index.data === forceIndex) {
      forceIndex = undefined
    }
    top = Math.max(top, 0)
  }

  value.addCallbackAndRun((level) => {
    forceIndex = floors.data.indexOf(level)
  })

  Stores.Chronic(50).addCallback(() => stabilize())
  floors.addCallback((floors) => {
    forceIndex = floors.findIndex((s) => s === value.data)
  })

  let image: HTMLImageElement
  $: {
    if (image) {
      let lastY = 0
      image.ontouchstart = (e: TouchEvent) => {
        mouseDown = true
        lastY = e.changedTouches[0].clientY
      }
      image.ontouchmove = (e) => {
        const y = e.changedTouches[0].clientY
        console.log(y)
        const movementY = y - lastY
        lastY = y
        onMove({ movementY })
      }
      image.ontouchend = unclick
    }
  }
</script>

<div
  bind:this={container}
  class="relative"
  style={`height: calc(${HEIGHT}px * ${$floors.length}); width: calc( 96px )`}
>
  <div
    class="absolute left-0 top-0 rounded-xl"
    style="margin: -0.25rem; width: calc(100% + 0.5rem); height: calc(100% + 0.5rem); background: rgba(255,255,255, 0.65); backdrop-filter: blur(2px); "
  />
  <div class="absolute right-0 h-full w-min">
    {#each $floors as floor, i}
      <button
        style={`height: ${HEIGHT}px; width: ${HEIGHT}px`}
        class:selected={i === (forceIndex ?? $index)}
        class={"content-box m-0 flex items-center justify-center border-2 border-gray-300"}
        on:click={() => {
          forceIndex = i
        }}
      >
        {floor}
      </button>
    {/each}
  </div>

  <div style={`width: ${WIDTH}px`}>
    <img
      alt=""
      aria-hidden="true"
      bind:this={image}
      class="draggable"
      draggable="false"
      on:mousedown={click}
      src="./assets/svg/elevator.svg"
      style={`top: ${top}px;`}
    />
  </div>
</div>

<svelte:window on:mousemove={onMove} on:mouseup={unclick} />

<style>
  .draggable {
    user-select: none;
    cursor: move;
    position: absolute;
    user-drag: none;

    height: 72px;
    margin-top: -15px;
    margin-bottom: -15px;
    margin-left: -18px;
    -webkit-user-drag: none;
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
  }
</style>
