<template>
  <div class="main">
    <div ref="container" class="container">
      <div id="wrapper" :style="{ width: `${width}px`, height: `${height}px` }">
        <img v-frame-hm="44" v-frame-wm="393" src="~/assets/images/mobile/common/status-bar.png" />
        <div
          v-frame-hm="contentHeightMobile"
          v-frame-wm="contentWidthMobile"
          v-frame-xm="0"
          v-frame-ym="44"
          ref="content"
          class="content"
        >
          <slot />
        </div>
        <div
          v-if="$slots.stickyHeader"
          v-frame-wm="contentWidthMobile"
          v-frame-xm="0"
          v-frame-ym="44"
          class="sticky-header"
          :class="{ visible: y >= stickyHeaderOffset }"
        >
          <div class="sticky-header-inner">
            <slot name="stickyHeader" />
          </div>
          <slot name="stickyHeaderActions">
            <NuxtLink v-if="profilLink" v-hm:64="48" v-wm:393="48" v-xm:393="337" v-ym:64="8" :to="profilLink">
              <Spot />
            </NuxtLink>
          </slot>
        </div>
        <MenuMobile :hasPostfachLink="hasPostfachLink" :postfachBadge="postfachBadge" :prefix="prefix" />
      </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,
  },

  /**
   * The prefix for the navigation links.
   *
   * @default '/mobile'
   */
  prefix: {
    type: String,
    default: '/mobile',
  },

  /**
   * Path for the `profil` page.
   *
   * @default '/mobile/profil'
   */
  profilLink: {
    type: [String, null],
    default: '/mobile/profil',
  },

  /**
   * Whether the postfach badge should be shown.
   *
   * @default true
   */
  postfachBadge: {
    type: Boolean,
    default: true,
  },

  /**
   * Whether the postfach link should be shown.
   *
   * @default true
   */
  hasPostfachLink: {
    type: Boolean,
    default: true,
  },
})

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 * (frameWidthMobile.value / frameHeightMobile.value)
  } else {
    width.value = cw
    height.value = width.value * (frameHeightMobile.value / frameWidthMobile.value)
  }

  if (width.value > cw) {
    width.value = cw
    height.value = width.value * (frameHeightMobile.value / frameWidthMobile.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: 2rem;
  outline: 8px solid #000;
  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;
}

.sticky-header {
  position: absolute;
  z-index: 10;
  overflow: hidden;
}

.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>
