<!--
Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
Copyright (C) 2024 HTWKalender support@htwkalender.de

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
-->

<script lang="ts" setup>
import tokenStore from "@/store/tokenStore.ts";
import { useToast } from "primevue/usetoast";
import { computed, onMounted } from "vue";
import { router } from "@/main";
import { useI18n } from "vue-i18n";
import { getLink } from "@/helpers/url.ts";

const { t } = useI18n({ useScope: "global" });

const toast = useToast();

const show = () => {
  toast.add({
    severity: "info",
    summary: t("calendarLink.copyToastSummary"),
    detail: t("calendarLink.copyToastNotification"),
    life: 3000,
  });
};

const failedClipboard = () => {
  toast.add({
    severity: "error",
    summary: t("calendarLink.copyToastError"),
    detail: t("calendarLink.copyToastErrorDetail"),
    life: 3000,
  });
};

onMounted(() => {
  rerouteIfTokenIsEmpty();
});

function rerouteIfTokenIsEmpty() {
  if (tokenStore().token == "") {
    router.push("/");
  }
}

function copyToClipboard() {
  // Copy the text inside the text field
  navigator.clipboard.writeText(getLink("/api/feed?token=", tokenStore().token)).then(() => show(),() => failedClipboard());
}

const forwardToGoogle = () => {
  window.open(
    "https://calendar.google.com/calendar/u/0/r?cid=" +
      encodeURI(getLink("/api/feed?token=", tokenStore().token).replace("https://", "http://")),
  );
};

const forwardToMicrosoft = () => {
  window.open(
    "https://outlook.live.com/owa?path=/calendar/action/compose&rru=addsubscription&name=HTWK%20Kalender&url=" +
      encodeURI(getLink("/api/feed?token=", tokenStore().token)),
  );
};

const copyTokenToClipboard = () => {
  navigator.clipboard.writeText(tokenStore().token).then(() => {
    toast.add({
      severity: "info",
      summary: t("calendarLink.copyTokenToastSummary"),
      detail: t("calendarLink.copyTokenToastNotification"),
      life: 3000,
    });
  });
};

const actions = computed(() => [
  {
    label: t("calendarLink.copyToClipboard"),
    icon: "pi pi-copy",
    command: copyToClipboard,
  },
  {
    label: t("calendarLink.toGoogleCalendar"),
    icon: "pi pi-google",
    command: forwardToGoogle,
  },
  {
    label: t("calendarLink.toMicrosoftCalendar"),
    icon: "pi pi-microsoft",
    command: forwardToMicrosoft,
  },
  {
    label: t("calendarLink.toHTWKApp"),
    icon: "pi pi-mobile",
    command: copyTokenToClipboard,
  },
]);
</script>

<template>
  <div class="flex flex-column mt-8">
    <div class="flex align-items-center justify-content-center m-2">
      <h2 class="text-base md:text-2xl">
        {{ getLink("/api/feed?token=", tokenStore().token) }}
      </h2>
    </div>
    <div class="flex align-items-center justify-content-center m-2">
      <Menu :model="actions" />
    </div>
  </div>
</template>

<style scoped>
:deep(
    .p-menu .p-menuitem .p-menuitem-content .p-menuitem-link .p-menuitem-icon
  ) {
  margin-right: 1rem;
}
</style>
