<template>
  <teleport to="body">
    <toast ref="toast"></toast>


    <modal :z-index="9000" :state="hasSpinner">
      <div class="spinner-box space-around">
        <spinner></spinner>
        <span>{{ spinnerMsg }}</span>
      </div>
    </modal>

  </teleport>
</template>

<script>

import {useNotificationStore} from "@/store/notification.js";
import {mapStores} from "pinia";
import Toast from "@/components/UI/Toast.vue";
import logger from "@/logger.js";
import Box from "@/components/UI/Box.vue";
import Spinner from "@/components/UI/Spinner.vue";
import Modal from "@/components/UI/Modal.vue";

export default {
  name: "TheNotificationSystem",
  components: {Modal, Spinner, Box, Toast},
  data() {
    return {}
  },
  computed: {
    ...mapStores(useNotificationStore),
    hasSpinner() {
      return this.notificationStore.hasSpinner;
    },
    spinnerMsg() {
      return this.notificationStore.spinnerMsg;
    },
    notifications() {
      return this.notificationStore.notifications;
    },
    hasNotifications() {
      return this.notificationStore.notifications?.length !== 0;
    }
  },
  watch: {
    hasNotifications(newValue) {
      if (newValue)
        this.fireToasts();
    }
  },
  methods: {
    fireToasts() {
      while (this.hasNotifications) {
        const msg = this.notificationStore.popNotification();

        logger.log("fire msg", msg);

        if (msg)
          this.$refs.toast.addToast({
            icon: msg.icon ?? null,
            message: msg.message ?? null,
            title: msg.title ?? null,
            variant: msg.variant ?? 'exception',
            duration: msg.duration ?? 8000
          });
      }
    }


  }
}
</script>

<style scoped>

.error-message {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  flex: 1;
}

.error-message-title {
  font-size: 1.6rem;
  font-weight: 500;
}

.error-message-content {
  font-size: 1.4rem;
}

.error-view-trace {
  font-size: 1.4rem;
  cursor: pointer;
  text-decoration: underline;
}

.error-notification-container {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translate(-50%, 0);
  z-index: 9000;
  pointer-events: none;
}

.error-notification {
  min-width: 30rem;
  max-width: 100rem;
  padding: 1.6rem;
  border-radius: 0.8rem;

  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1.6rem;

  background-color: #BC2B72;
  color: #ffffff;

  font-size: 1.4rem;
  cursor: pointer;
  pointer-events: auto;

  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.icon-error-notification {
  cursor: pointer;
  transition: all 0.1s ease-in-out;
  background-color: #BC2B72;
  color: #ffffff;
}

.icon-error-notification:hover, .icon-error-notification:active {
  cursor: pointer;
  transform: scale(1.1);
}


/* Transition animations */
.error-container-enter-active {
  transition: all 0.3s ease;
}

.error-container-leave-active {
  transition: all 0.3s ease;
}

.error-container-enter-from {
  opacity: 0;
  transform: translate(-50%, 100%);
}

.error-container-leave-to {
  opacity: 0;
  transform: translate(-50%, 100%);
}

.error-container-move {
  transition: transform 0.3s ease;
}

.spinner-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3.6rem;
    flex: 1 1 auto;
    font-size: 1.6rem;
}

.space-around {
  margin: 3rem;
}

</style>