<script setup>
import { onMounted, ref } from 'vue'

import ModalContainer from '@/components/ModalContainer.vue'
import SetupForm from '@/components/forms/SetupForm.vue'
import { useBuildingStore } from '@/stores/buildings'

const isOpen = ref()
const setupRequired = ref(false)
const showModal = ref(false)

const buildings = useBuildingStore()

function togglePicker() {
  isOpen.value = !isOpen.value
}

function onSelect(id) {
  buildings.setActiveBuilding(id)
  isOpen.value = false
}

function openModal() {
  showModal.value = true
}

function closeModal() {
  showModal.value = false
}

function onComplete() {
  setupRequired.value = false
}

function getAddressString(value) {
  if (value) {
    const el = value.split('::')
    return `${el[0]} ${el[1]}, ${el[2]} ${el[3]}`
  }

  return null
}

onMounted(() => {
  if (buildings.firstBuilding?.setupCompleted === false) {
    setupRequired.value = true
    showModal.value = true
  }
})
</script>

<template>
  <div class="building-picker">
    <div class="building-picker__dropdown">
      <button class="button secondary" type="button" @click="togglePicker" :disabled="setupRequired">
        <div class="building-picker__button" v-if="buildings.current">
          <span class="building-picker__prefix">{{ buildings.current?.prefix }}</span>
          <span>{{ buildings.current?.name }}</span>
        </div>
        <div v-else>
          <span>Bitte Gebäude auswählen</span>
        </div>
        <div class="button-icon">
          <font-awesome-icon v-if="isOpen" icon="fa-solid fa-chevron-up" />
          <font-awesome-icon v-else icon="fa-solid fa-chevron-down" />
        </div>
      </button>
      <ul class="building-picker__list shadow" v-if="isOpen">
        <li class="building-picker__item" v-for="building of buildings.data" :key="building._id"
          @click="onSelect(building._id)">
          <div class="building-picker__title">
            <span class="building-picker__prefix">{{ building.prefix }}</span>
            <span>{{ building.name || building._id }}</span>
          </div>
          <div class="building-picker__subtitle">
            <span>{{ getAddressString(building.address) }}</span>
          </div>
        </li>
      </ul>
    </div>
    <div v-if="setupRequired && buildings.firstBuilding">
      <button class="button secondary" type="button" @click="openModal">
        <div class="button-content">Einrichtung abchließen</div>
        <div class="button-icon">
          <font-awesome-icon icon="fa-solid fa-pen" />
        </div>
      </button>
    </div>
    <ModalContainer v-if="showModal" label="Stammdaten eingeben" @close-modal="closeModal">
      <SetupForm @close-modal="closeModal" @setup-completed="onComplete" />
    </ModalContainer>
  </div>
</template>

<style scoped>
.building-picker {
  z-index: 1;
  height: var(--element-size);
  display: flex;
  gap: var(--spacing);
}

.building-picker__button {
  height: var(--element-size);
  display: flex;
  gap: var(--spacing);
  align-items: center;
}

.building-picker__dropdown {
  position: relative;
  height: var(--element-size);
  flex: 1;
  display: flex;
  border-radius: var(--radius);
  cursor: pointer;
}

.building-picker__list {
  position: fixed;
  background-color: hsl(var(--clr-content));
  top: 4em;
  left: 0;
  margin: var(--spacing-s);
  padding: var(--spacing-s);
  width: calc(100% - var(--spacing-s) * 2);
  display: flex;
  flex-direction: column;
  gap: var(--spacing-s);
  border: var(--border) solid hsl(var(--clr-primary));
  border-radius: var(--radius);
  list-style: none;
}

@media (min-width: 48em) {
  .building-picker__list {
    position: absolute;
    top: 3.5em;
    margin: 0;
    width: auto;
  }
}

.building-picker__item {
  overflow: hidden;
  padding: var(--spacing-s);
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  border-radius: var(--radius);
}

.building-picker__item:hover {
  background-color: hsl(var(--clr-background));
}

.building-picker__prefix {
  background-color: hsl(var(--clr-primary));
  padding: 0.2em var(--spacing-s);
  border-radius: var(--radius);
  color: var(--clr-white);
  font-size: var(--text-s);
}

.building-picker__title,
.building-picker__subtitle {
  width: 100%;
  display: flex;
}

.building-picker__title {
  gap: var(--spacing-s);
  align-items: center;
}

.building-picker__subtitle {
  white-space: nowrap;
}

.building-picker__subtitle>span {
  font-size: var(--text-s);
}
</style>