<script setup>
import { historyOfDate } from "../store";
import { dayNames, uiTexts } from "../definitions";
import { getSubstitutionText } from "../util";
import dayjs from "dayjs";

function getChar(type) {
  switch (type) {
    case "change":
      return "~";
    case "addition":
      return "+";
    case "deletion":
      return "-";
  }
}

function getColor(type) {
  switch (type) {
    case "change":
      return "background-color: #14587E;";
    case "addition":
      return "background-color:  #476331;";
    case "deletion":
      return "background-color:  #7D2B2D;";
  }
}
</script>

<template>
  <div class="history">
    <template v-for="(events, date) of historyOfDate" :key="date">
      <div class="title">
        <span class="day">
          {{ dayNames[new Date(parseInt(date)).getDay() - 1] }},
          {{ dayjs(parseInt(date)).format("DD.MM.YYYY") }}
        </span>
      </div>

      <template v-for="(event, index) in events" :key="index">
        <div class="change" :style="getColor(event.type)">
          <span class="hour">{{ event.lesson }}{{ getChar(event.type) }}</span>
          <div class="infos">
            <span class="text" v-if="event.type == 'change'">
              <template v-for="(change, key) in event.change" :key="key">
                <p>
                  {{ key }}:&nbsp;
                  <s>{{ change.before }}</s>
                  &nbsp;{{ change.after }}
                </p>
              </template>
            </span>
            <span class="text" v-else>
              {{ getSubstitutionText(event.data) }}
              <span class="notes">
                {{ event.data.notes ? uiTexts.substitutionNotes + ": " : "" }}
                {{ event.data.notes }}
              </span>
            </span>
            <span class="notes">
              {{ dayjs(event.changedAt).format("DD.MM.YYYY, HH:mm") }}
            </span>
          </div>
        </div>
      </template>
    </template>
  </div>
</template>

<style scoped>
.history {
  padding: 65px 10px 80px 10px;
}

.title {
  font-size: 18px;
}

.change {
  display: grid;
  background-color: #26272a;
  min-height: 35px;
  border-radius: 11px;
  margin: 10px 0px;
  padding: 10px 10px;
  grid-template-columns: 40px auto;
  gap: 5px;
}

.hour {
  font-size: 30px;
  display: flex;
  align-items: center;
  letter-spacing: 5px;
}

.infos {
  display: flex;
  justify-content: center;
  flex-direction: column;
}

.text {
  font-size: 18px;
  display: flex;
  align-items: center;
  word-wrap: break-word;
  flex-direction: column;
  align-items: flex-start;
}

p {
  margin: 0px;
}

.notes {
  font-size: 13px;
  font-weight: 100;
}
</style>
