<template>
  <div class="quote-card__container"
    :class="{ 'quote-card__container__editable': editable, 'quote-card__container__editable__edit-mode': isEditMode }">

    <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"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } 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}`
const isEditMode = ref(false)

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

if (props.instantEdit) {
  editQuote()
}

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

const saveQuote = async () => {
  isEditMode.value = false
  if (props.quote.quote) {
    useEngine().saveQuote(props.quote.id || ':new:', props.quote.quote)
  }
  emit('edit-end')
  await useEngine().loadQuotes()

  window.setTimeout(() => {
    const $el = document.getElementById(quoteElId)
    if ($el != null) {
      $el.blur()
      const sel = window.getSelection()
      sel.removeAllRanges()
    }
  }, 0)
}
</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-container {
    display: flex;
    align-items: center;
    width: 100%;
    height: 100%;
    overflow: hidden;
  }

  &__quote {
    margin: 2px;
    font-family: $font-secondary;
    font-weight: bold;
    font-size: 24px;
    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>
