<template>
  <q-card style="text-align: center">
    <q-card-section class="row justify-center content-stretch">
      <div class="col-4">
        <div style="width: 100%; padding-bottom: 100%; position: relative">
          <q-avatar style="position: absolute; top: 0; left: 0; width: 100%; height: 100%">
            <img :src="avatarLink" />
          </q-avatar>
        </div>
      </div>
      <div class="col-8">
        <span class="text-h6">Hallo {{ name }}</span
        ><br />
        <span v-if="hasBirthday">Herzlichen Glückwunsch zum Geburtstag!<br /></span>
        <span v-if="birthday.length > 0"
          >Heute <span v-if="birthday.length === 1">hat </span><span v-else>haben </span
          ><span v-for="(user, index) in birthday" :key="index"
            >{{ user.display_name }}<span v-if="index < birthday.length - 1">, </span></span
          >
          Geburtstag.</span
        >
        <span v-else>Heute stehen keine Geburtstage an</span>
      </div>
    </q-card-section>
  </q-card>
</template>

<script lang="ts">
import { useMainStore } from 'src/stores';
import { computed, defineComponent, onMounted, ref } from 'vue';
import { useUserStore } from '../store';

export default defineComponent({
  name: 'Greeting',
  setup() {
    const mainStore = useMainStore();
    const userStore = useUserStore();

    // Ensure users are loaded,so we can query birthdays
    onMounted(() => userStore.getUsers(false));

    const name = ref(mainStore.currentUser.display_name);
    const avatarLink = ref(mainStore.currentUser.avatar_url);

    function userHasBirthday(user: FG.User) {
      const today = new Date();
      return (
        user.birthday &&
        user.birthday.getMonth() === today.getMonth() &&
        user.birthday.getDate() === today.getDate()
      );
    }

    const hasBirthday = computed(() => {
      return userHasBirthday(mainStore.currentUser);
    });

    const birthday = computed(() =>
      userStore.users
        .filter(userHasBirthday)
        .filter((user) => user.userid !== mainStore.currentUser.userid)
    );

    return { avatarLink, name, hasBirthday, birthday };
  },
});
</script>
