<template>
  <div :class="cssClasses">
    <div v-if="editable && !isEditMode" class="quote-card__action-buttons">
      <QuoteCardActionButton @click="editQuote" icon="edit" />
      <QuoteCardActionButton @click="deleteQuote" icon="trash" />
    </div>
    <div class="quote-card__text-container">
      <contenteditable :id="quoteElId" class="quote-card__quote" tag="div" :contenteditable="isEditMode"
        v-model="quote.quote" :no-nl="true" :no-html="true" @returned="saveQuote" @blur="saveQuote"
        @keydown="keydown" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import contenteditable from 'vue-contenteditable'
import useEngine from '@/composables/useEngine'
import type { Quote } from '@/composables/engine.d'

const props = defineProps<{
  quote: Quote,
  editable?: boolean,
  instantEdit?: boolean,
}>()
const emit = defineEmits(['edit-end'])

const quoteElId = `quote-${props.quote.id || 'new'}`
const isEditMode = ref(false)

const cssClasses = computed(() => {
  const len = props.quote.quote.length
  return {
    'quote-card__container': true,
    'quote-card__container__editable': props.editable,
    'quote-card__container__editable__edit-mode': isEditMode.value,
    'quote-card__container__text-short': len < 80,
    'quote-card__container__text-medium': len >= 80 && len < 120,
    'quote-card__container__text-long': len >= 120,
  }
})

const editQuote = () => {
  isEditMode.value = true
  window.setTimeout(() => {
    const $el = document.getElementById(quoteElId)
    if ($el != null) {
      $el.focus()
      const range = document.createRange()
      range.selectNodeContents($el)
      const sel = window.getSelection()
      if (sel != null) {
        sel.removeAllRanges()
        sel.addRange(range)
      }
    }
  }, 0)
}

if (props.instantEdit) {
  editQuote()
}

const deleteQuote = () => useEngine().deleteQuote(props.quote.id)

const editModeEnd = async () => {
  isEditMode.value = false
  emit('edit-end')
  await useEngine().loadQuotes()

  window.setTimeout(() => {
    const $el = document.getElementById(quoteElId)
    if ($el != null) {
      $el.blur()
      const sel = window.getSelection()
      if (sel != null) {
        sel.removeAllRanges()
      }
    }
  }, 0)
}

const saveQuote = async () => {
  if (props.quote.quote) {
    await useEngine().saveQuote(props.quote.id, props.quote.quote)
  }

  await editModeEnd()
}

const keydown = async (ev: KeyboardEvent) => {
  if (ev.key === 'Escape') {
    await editModeEnd()
  }
}
</script>

<style lang="scss">
@import '~/assets/css/components';

.quote-card {
  &__container {
    position: relative;
    width: 300px;
    height: 150px;
    margin: 32px;
    padding: 32px;
    background-color: $quote-background-color;
    border: $quote-border;
    border-radius: 0 16px 0 0;
    color: $quote-text-color;

    &__editable {
      cursor: text;

      &__edit-mode {
        background-color: $quote-editmode-background-color;
      }

      & .quote-card__quote {
        cursor: text;
      }
    }

    &__text-short {
      font-size: 24px;
    }

    &__text-medium {
      font-size: 20px;
    }

    &__text-long {
      font-size: 18px;
    }
  }

  &__text-container {
    display: flex;
    align-items: center;
    width: 100%;
    height: 100%;
    overflow: hidden;
  }

  &__quote {
    margin: 2px;
    font-family: $font-secondary;
    font-weight: bold;
    text-align: center;
    cursor: default;
  }

  &__container__editable:hover &__action-buttons {
    visibility: visible;
  }

  &__action-buttons {
    visibility: hidden;
    position: absolute;
    right: -28px;
    top: -24px;
    display: flex;
  }
}
</style>
