<!--
Composant Image permettant de charger et d'afficher une image stockée sur les serveurs Opentalent à partir de son id.
Permet d'afficher une image par défaut si l'image demandée n'est pas disponible ou invalide.

Si la propriété 'upload' est à 'true', propose aussi un input pour uploader une nouvelle image.
-->
<template>
  <main>
    <div class="image-wrapper" :style="{width: width + 'px'}">
      <v-img
        :src="imageSrc"
        :lazy-src="defaultImagePath"
        :height="height"
        :width="width"
        aspect-ratio="1"
      >
        <template #placeholder>
          <v-row
            class="fill-height ma-0"
            align="center"
            justify="center"
            v-if="pending"
          >
            <v-progress-circular
              :indeterminate="true"
              color="neutral"
            />
          </v-row>
        </template>
      </v-img>

      <div>
        <div v-if="upload" class="click-action hover" @click="openUpload=true"><v-icon>mdi-upload</v-icon></div>
        <UiInputImage
          v-if="openUpload"
          @close="openUpload=false"
          :existingImageId="id"
          :field="field"
          :ownerId="ownerId"
          @update="$emit('update', $event, field); openUpload=false"
          @reload="onReload"
          @reset="reset"
        ></UiInputImage>
      </div>
    </div>
  </main>
</template>


<script setup lang="ts">
import {ref} from "@vue/reactivity";
import type {Ref} from "@vue/reactivity";
import {useImageFetch} from "~/composables/data/useImageFetch";
import {onUnmounted, watch} from "@vue/runtime-core";
import type {WatchStopHandle} from "@vue/runtime-core";
import {useImageManager} from "~/composables/data/useImageManager";
import ImageManager from "~/services/data/imageManager";

const props = defineProps({
  id: {
    type: Number,
    required: false,
    default: null
  },
  defaultImage: {
    type: String,
    required: false
  },
  height: {
    type: Number,
    required: false
  },
  width: {
    type: Number,
    required: false
  },
  field: {
    type: String,
    required: false
  },
  upload: {
    type: Boolean,
    required: false,
    default: false
  },
  ownerId:{
    type: Number,
    required: false
  }
})

const { imageManager } = useImageManager()
const { fetch } = useImageFetch()

const defaultImagePath = props.defaultImage ?? ImageManager.defaultImage

const openUpload: Ref<Boolean> = ref(false)

const { data: imageSrc, pending, refresh } = fetch(props.id ?? null, defaultImagePath, props.height, props.width)

const unwatch: WatchStopHandle = watch(() => props.id, async (newValue, oldValue) => {
  await refresh()
})

const onReload = async () => {
  await refresh()
  openUpload.value = false
}

/**
 * Quand on souhaite faire un reset de l'image
 */
const reset = () => {
  imageSrc.value = defaultImagePath
  openUpload.value = false
}

/**
 * Lorsqu'on démonte le component, on supprime le watcher
 */
onUnmounted(() => {
  unwatch()
})
</script>

<style lang="scss">
  div.image-wrapper {
    display: block;
    position: relative;

    img {
      max-width: 100%;
    }

    .click-action {
      position: absolute;
      top:0;
      left:0;
      width: 100%;
      height: 100%;
      background: transparent;
      opacity: 0;
      transition: all .2s;
      &:hover {
        opacity: 1;
        background: rgb(var(--v-theme-neutral-strong));
        cursor: pointer;
      }
      i {
        color: rgb(var(--v-theme-on-neutral-strong));
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50% , -50%);
        font-size: 50px;
        z-index: 1;
        opacity: 1;
        &:hover{
          color: rgb(var(--v-theme-primary-alt));
        }
      }
    }
  }
</style>
