<script setup lang="ts">
import moduleStore from "../store/moduleStore.ts";
import { createIndividualFeed } from "../api/createFeed.ts";
import router from "../router";
import tokenStore from "../store/tokenStore.ts";
import { computed, ref } from "vue";
import ModuleTemplateDialog from "./ModuleTemplateDialog.vue";
import { onlyWhitespace } from "../helpers/strings.ts";
import { useI18n } from "vue-i18n";
const { t } = useI18n({ useScope: "global" });

const store = moduleStore();
const tableData = ref(
  store.getAllModules().map((module) => {
    return {
      Course: module.course,
      Module: module,
    };
  }),
);

const columns = computed(() => [
  { field: "Course", header: t("moduleInformation.course") },
  { field: "Module", header: t("moduleInformation.module") },
  { field: "Reminder", header: t("renameModules.reminder") },
]);

async function finalStep() {
  const token: string = await createIndividualFeed(store.getAllModules());
  tokenStore().setToken(token);
  await router.push("/calendar-link");
}
</script>

<template>
  <div class="flex flex-column">
    <div class="flex align-items-center justify-content-center h-4rem m-2">
      <h3>{{ $t("renameModules.subTitle") }}</h3>
      <ModuleTemplateDialog />
    </div>
    <div class="card flex align-items-center justify-content-center m-2">
      <DataTable
        :value="tableData"
        edit-mode="cell"
        table-class="editable-cells-table"
        responsive-layout="scroll"
      >
        <template #header>
          <div class="flex align-items-center justify-content-end">
            {{ $t("renameModules.enableAllNotifications") }}
            <InputSwitch
              class="mx-4"
              :model-value="
                tableData.reduce(
                  (acc, curr) => acc && curr.Module.reminder,
                  true,
                )
              "
              @update:model-value="
                tableData.forEach((module) => (module.Module.reminder = $event))
              "
            />
          </div>
        </template>
        <Column
          v-for="col of columns"
          :key="col.field"
          :field="col.field"
          :header="col.header"
          :class="col.field === 'Reminder' ? 'text-center' : ''"
        >
          <!-- Text Body -->
          <template #body="{ data, field }">
            <template v-if="field === 'Module'">
              {{
                onlyWhitespace(data[field].userDefinedName)
                  ? data[field].name
                  : data[field].userDefinedName
              }}
            </template>
            <template v-else-if="field === 'Reminder'">
              <Button
                :icon="data.Module.reminder ? 'pi pi-bell' : 'pi pi-times'"
                :severity="data.Module.reminder ? 'warning' : 'secondary'"
                rounded
                outlined
                class="small-button"
                @click="data.Module.reminder = !data.Module.reminder"
              ></Button>
            </template>
            <template v-else>
              {{ data[field] }}
            </template>
          </template>
          <!-- Editor Body -->
          <template #editor="{ data, field }">
            <template v-if="field === 'Module'">
              <InputText
                v-model="data[field].userDefinedName"
                class="w-full"
                autofocus
              />
            </template>
            <template v-else-if="field === 'Reminder'">
              <Button
                :icon="data.Module.reminder ? 'pi pi-bell' : 'pi pi-times'"
                :severity="data.Module.reminder ? 'warning' : 'secondary'"
                rounded
                outlined
                class="small-button"
                @click="data.Module.reminder = !data.Module.reminder"
              ></Button>
            </template>
            <template v-else>
              <div>{{ data[field] }}</div>
            </template>
          </template>
        </Column>
      </DataTable>
    </div>

    <div class="flex align-items-center justify-content-center h-4rem m-2">
      <Button @click="finalStep()">{{ $t("renameModules.nextStep") }}</Button>
    </div>
  </div>
</template>

<style scoped>
</style>
