<template>
  <MultiModal @handle="modal_handle" />

  <figure>
    <div class="image is-unselectable">
      <img :src="$advent22.api_url('user/background_image')" />
      <ThouCanvas>
        <CalendarDoor
          v-for="(door, index) in doors"
          :key="`door-${index}`"
          :door="door"
          :visible="store.is_touch_device"
          :title="$advent22.name_door(door.day)"
          @click="door_click(door.day)"
        />
      </ThouCanvas>
    </div>
  </figure>
</template>

<script lang="ts">
import { Door } from "@/lib/door";
import { advent22Store } from "@/plugins/store";
import { Options, Vue } from "vue-class-component";

import MultiModal from "./MultiModal.vue";
import BulmaButton from "./bulma/Button.vue";
import CalendarDoor from "./calendar/CalendarDoor.vue";
import ThouCanvas from "./calendar/ThouCanvas.vue";

@Options({
  components: {
    MultiModal,
    BulmaButton,
    ThouCanvas,
    CalendarDoor,
  },
  props: {
    doors: Array,
  },
})
export default class extends Vue {
  public readonly doors!: Door[];
  public readonly store = advent22Store();

  private multi_modal?: MultiModal;

  public modal_handle(modal: MultiModal) {
    this.multi_modal = modal;
  }

  public door_click(day: number) {
    if (this.multi_modal === undefined) return;
    this.multi_modal.show_progress();

    this.$advent22
      .api_get_blob(`user/image_${day}`)
      .then((image_src) => {
        this.multi_modal!.show_image(image_src, this.$advent22.name_door(day));
      })
      .catch((error) => {
        alert(error);
        this.multi_modal!.hide();
      });
  }
}
</script>
