<template>
  <ClientOnly>
    <PpDeleteDialog
      ref="deleteModal"
      :current-card-index="currentCardIndex"
      @delete="removeCard(currentCardIndex)"
    />
    <PpPriceCardDialog
      ref="modal"
      :current-card="currentCard"
      :current-card-index="currentCardIndex"
      @update="updateCard()"
    />
    <section class="content flex-col">
      <aside class="filter-bar">
        <PpButtonGroup
          :buttons="filterButtons"
          @click="sort"
        />
      </aside>
      <div class="flex-col" role="list">
          <PpPriceCard
            ref="priceCard"
            v-for="(card, index) in cards"
            :key="card.uuid"
            :deletable="cards.length > 1"
            :card="card"
            @update="openModal(false, index)"
            @remove="openDeleteModal()"
          />
      </div>
    </section>
    <PpToolbar>
      <PpButton class="mini-button text-white transparent" @click="sort(currentSort)">
        <Icon class="icon" name="uil:refresh" mode="svg" />
        <span>Neu sortieren</span>
        <span
          class="dot"
          :class="{ visible : isDirty}"
          aria-hidden="true"
        />
      </PpButton>
      <PpButton class="mini-button text-white transparent" @click="openModal(true, -1)">
        <Icon class="icon" name="uil:plus" mode="svg" />
        <span>Hinzufügen</span>
      </PpButton>
    </PpToolbar>
  </ClientOnly>
</template>

<script setup lang="ts">
import type { Card } from '../../shared/Card'
import type { Button } from '../../shared/ButtonGroup'
import { PpPriceCardDialog, PpDeleteDialog, PpPriceCard } from '#components'

const cards = useLocalStorage<Card[]>('cards', [])
const currentSort = useLocalStorage<number>('sort', 0)
const isDirty = ref(false)
const currentCard = ref<Card>()
const currentCardIndex = ref<number>(-1)
const modal = useTemplateRef<typeof PpPriceCardDialog>('modal')
const deleteModal = useTemplateRef<typeof PpDeleteDialog>('deleteModal')
const priceCards = useTemplateRef<(typeof PpPriceCard)[]>('priceCard')

const createCard = (uuid : string) : Card => ({
  uuid,
  name: '',
  price: '',
  roles: '',
  sheets: '',
  layers: '',
})


const addCard = (card : Card) => {
  cards.value.unshift({ ...card })
  isDirty.value = true
}

const removeCard = (index : number) => {
  cards.value.splice(index, 1)
  isDirty.value = true
}

const updateCard = () => {
  if (currentCardIndex.value === -1) {
    addCard(currentCard.value!)
    return
  }

  const newCard = { ...currentCard.value! }
  cards.value.splice(currentCardIndex.value, 1, newCard)
  isDirty.value = true
}

const openModal = (createNew : boolean, index : number) => {
  if (createNew) {
    currentCardIndex.value = -1
    currentCard.value = createCard(randomUUID())
    modal.value?.$el.showModal()
    return
  }

  currentCardIndex.value = index
  currentCard.value = { ...cards.value[index]! }

  modal.value?.$el.showModal()
  return
}

const openDeleteModal = () => {
  deleteModal.value?.$el.showModal()
}

const filterButtons = ref<Button[]>([
  {
    label: 'Rollen',
    icon: 'uil:toilet-paper',
    active: currentSort.value === 0,
  },
  {
    label: 'Blatt',
    icon: 'uil:file-landscape',
    active: currentSort.value === 1,
  },
  {
    label: 'Lagen',
    icon: 'uil:layer-group',
    active: currentSort.value === 2,
  },
])

const sortBy = (key : 'ppr' | 'pps' | 'ppl') => {
  cards.value.sort((a, b) => {
    const aCard = priceCards.value?.find(card => card.uuid === a.uuid) || null
    const bCard = priceCards.value?.find(card => card.uuid === b.uuid) || null
    if (!aCard || !bCard) return 0
    return aCard[key] - bCard[key]
  })
}

const sort = (index : number) => {
  currentSort.value = index
  filterButtons.value.forEach(button => { button.active = false })
  filterButtons.value[index]!.active = true

  switch (index) {
    case 0:
      sortBy('ppr')
      break
    case 1:
      sortBy('pps')
      break
    case 2:
      sortBy('ppl')
      break
  }

  isDirty.value = false
}
</script>
