<template>
  <div class="main">
    <div ref="container" class="container">
      <div id="wrapper" :style="{ width: `${width}px`, height: `${height}px` }">
        <img v-frame-h="111" v-frame-w="1512" src="~/assets/images/desktop/common/browser.png" />
        <img v-frame-h="871" v-frame-w="1512" src="~/assets/images/desktop/common/body.png" />
        <Header />
        <Menu />
        <div
          v-frame-h="contentHeight"
          v-frame-w="contentWidth"
          v-frame-x="271"
          v-frame-y="194"
          ref="content"
          class="content"
        >
          <slot />
        </div>
        <div
          v-if="$slots.stickyHeader"
          v-frame-w="contentWidth"
          v-frame-x="271"
          v-frame-y="194"
          class="sticky-header"
          :class="{ visible: y >= stickyHeaderOffset }"
        >
          <div class="sticky-header-inner">
            <slot name="stickyHeader" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
defineProps({
  /**
   * Scroll offset in pixels at which the sticky header becomes visible.
   *
   * @default 38
   */
  stickyHeaderOffset: {
    type: Number,
    default: 38,
  },
})

const container = useTemplateRef('container')
const content = useTemplateRef('content')
const width = ref(0)
const height = ref(0)
const spotsVisible = useSpotsVisible()
const { y } = useScroll(content)

let timeout: NodeJS.Timeout | undefined

onMounted(resize)

useEventListener('resize', resize)

useEventListener('click', (event) => {
  let target = event.target

  while (target instanceof HTMLElement) {
    if (target.nodeName === 'A' || target.nodeName === 'BUTTON') {
      return
    }
    target = (target as HTMLElement).parentElement
  }

  spotsVisible.value = true

  clearTimeout(timeout)
  timeout = setTimeout(() => {
    spotsVisible.value = false
  }, 300)
})

onUnmounted(() => {
  clearTimeout(timeout)
})

function resize() {
  const cw = container.value!.clientWidth
  const ch = container.value!.clientHeight

  if (cw > ch) {
    height.value = ch
    width.value = height.value * (frameWidth.value / frameHeight.value)
  } else {
    width.value = cw
    height.value = width.value * (frameHeight.value / frameWidth.value)
  }

  if (width.value > cw) {
    width.value = cw
    height.value = width.value * (frameHeight.value / frameWidth.value)
  }
}
</script>

<style scoped>
.main {
  display: flex;
  height: 100vh;
  padding: 2rem;
}

.container {
  display: flex;
  width: 100%;
  height: 100%;
}

#wrapper {
  position: relative;
  margin: auto;
  overflow: hidden;
  border-radius: 0.375rem;
  box-shadow:
    0 0 #0000,
    0 0 #0000,
    0 10px 15px -3px rgba(0, 0, 0, 0.1),
    0 4px 6px -4px rgba(0, 0, 0, 0.1);
}

.content {
  position: absolute;
  overflow-y: auto;
  border-radius: 0.8%;
}

.sticky-header {
  position: absolute;
  z-index: 10;
  overflow: hidden;
  border-radius: 8px;
}

.sticky-header-inner {
  position: relative;
  transform: translateY(-100%);
  transition: transform 0.3s ease;
}

.sticky-header.visible .sticky-header-inner {
  transform: translateY(0);
}

@media (max-width: 1023px) {
  .main {
    padding: 1rem;
  }
}
</style>
