<template>
  <SVGRect :rectangle="door.position" :focused="false" @click.left="on_click" />
</template>

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

import SVGRect from "./SVGRect.vue";

@Options({
  components: {
    SVGRect,
  },
  props: {
    door: Door,
  },
  emits: ["doorClick", "doorSuccess", "doorFailure"],
})
export default class extends Vue {
  public door!: Door;

  public on_click() {
    this.$emit("doorClick");

    this.$advent22
      .api_get_blob(`days/image/${this.door.day}`)
      .then((data) => this.$emit("doorSuccess", data))
      .catch(([reason]) => {
        let msg = "Unbekannter Fehler, bitte wiederholen!";

        if (
          reason instanceof AxiosError &&
          reason.response !== undefined &&
          reason.response.status === 401
        ) {
          msg = "Netter Versuch :)";
        }

        this.$emit("doorFailure", msg);
      });
  }
}
</script>

<style scoped>
rect {
  cursor: pointer;
}
</style>
