<template>
  <teleport to="body">
    <transition
        name="error-container"
        tag="div"
        class="error-notification-container">
      <div class="error-notification" v-if="error">
        <div>
          <ph-warning size="24"></ph-warning>
        </div>
        <div class="error-message">
          <div class="error-message-title">
            {{ title }}
          </div>
          <div class="error-message-content">
            {{ message }}
          </div>
          <div class="error-view-trace" v-if="trace" @click="activateTrace">
            View trace
          </div>
        </div>
        <div class="icon-error-notification">
          <ph-x size="24" @click="close"></ph-x>
        </div>
        <modal :z-index="9001" :state="showTrace"><trace-view :error="error" @close="deactivateTrace"></trace-view></modal>
      </div>
    </transition>
  </teleport>
</template>

<script>

import {useErrorStore} from "@/store/error.js";
import {mapStores} from "pinia";
import TraceView from "@/components/layout/TraceView.vue";
import Modal from "@/components/UI/Modal.vue";

export default {
  name: "ErrorNotification",
  components: {Modal, TraceView},
  data() {
    return {
      showTrace: false,
    }
  },
  computed: {
    ...mapStores(useErrorStore),
    title() {
      return this.error?.title || 'Error';
    },
    message() {
      return this.error?.message || 'An unexpected error occurred';
    },
    trace() {
      return !((this.error?.trace ?? null) === null) || !((this.error?.traceCombined ?? null) === null);
    },
    error() {
      return this.errorStore.lastError;
    }
  },
  methods: {
    close() {
      this.errorStore.clearErrors();
    },
    activateTrace() {
      this.showTrace = true;
    },
    deactivateTrace() {
      this.showTrace = false;
    },
  }
}
</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;
}

</style>