<template>
  <v-dialog
    persistent
    v-model="showDialog"
    class="ma-0 pa-0"
    :width="$vuetify.display.smAndUp ? '688px' : '95%'"
    scroll-strategy="none"
  >
    <div v-if="timeout == -1" class="dialog-content text-center">
      <v-icon size="28">$vuetify.icons.trash_black</v-icon>
      <h2 class="dialog-title">{{ $t("purge_room.title") }}</h2>
      <div class="dialog-text">
        {{ $t("purge_room.info") }}
      </div>
      <div class="dialog-text">
        {{ status }}
      </div>
      <v-container fluid>
        <v-row cols="12">
          <v-col cols="6">
            <v-btn
              id="btn-purge-room-cancel"
              variant="flat"
              block
              class="text-button"
              :disabled="isPurging"
              @click="showDialog = false"
              >{{ $t("menu.cancel") }}</v-btn
            >
          </v-col>
          <v-col cols="6" align="center">
            <v-btn
              id="btn-purge-room"
              color="red"
              variant="flat"
              block
              class="filled-button"
              :disabled="isPurging"
              @click.stop="onPurgeRoom()"
              >{{ $t("purge_room.button") }}</v-btn
            >
          </v-col>
        </v-row>
      </v-container>
    </div>

    <!-- Timer -->
    <div v-if="timeout >= 0 && !isPurging" class="dialog-content text-center">
      <v-icon size="20" class="d-inline-block me-2">$vuetify.icons.timer</v-icon>
      {{ $t("purge_room.n_seconds", { seconds: timeout }) }}
      <h2 class="dialog-title mb-0">{{ $t("purge_room.self_destruct") }}</h2>
      <div class="dialog-text text-center mb-5">
        {{ $t("purge_room.notified") }}
      </div>
      <div class="dialog-text">
        {{ status }}
      </div>
      <v-container fluid>
        <v-row cols="12">
          <v-col cols="6">
            <v-btn
              id="btn-purge-room-undo"
              variant="flat"
              block
              class="text-button"
              :disabled="isPurging"
              @click="undo"
              >{{ $t("menu.undo") }}</v-btn
            >
          </v-col>
          <v-col cols="6">
            <v-btn variant="flat" block class="outlined-button" @click="onDoPurgeRoom">
              {{ $t("menu.delete_now") }}
            </v-btn>
          </v-col>
        </v-row>
      </v-container>
    </div>

    <!-- Purging -->
    <div v-if="isPurging" class="dialog-content">
      <h2 class="dialog-title">{{ $t("purge_room.deleting") }}</h2>
      <div class="dialog-text">
        {{ status }}
      </div>
    </div>
  </v-dialog>
</template>
<script>
import { STATE_EVENT_ROOM_DELETION_NOTICE } from "../plugins/utils";
import RoomDialogBase from "./RoomDialogBase.vue";

export default {
  name: "PurgeRoomDialog",
  extends: RoomDialogBase,
  data() {
    return {
      timeout: -1,
      timeoutTimer: null,
      isPurging: false,
      status: null,
    };
  },
  methods: {
    onOpenDialog() {
      // Showing, reset
      this.status = null;
    },
    undo() {
      if (this.timeoutTimer) {
        clearInterval(this.timeoutTimer);
        this.timeoutTimer = null;
      }
      this.timeout = -1;

      // Cancel the state event for deletion
      this.$matrix.matrixClient.sendStateEvent(this.room.roomId, STATE_EVENT_ROOM_DELETION_NOTICE, {
        status: "cancel",
      });

      this.showDialog = false;
    },
    onPurgeRoom() {
      // Send custom state event!
      this.$matrix.matrixClient.sendStateEvent(this.room.roomId, STATE_EVENT_ROOM_DELETION_NOTICE, {
        status: "delete",
      });

      this.timeout = 7;
      this.timeoutTimer = setInterval(() => {
        this.timeout = this.timeout - 1;
        if (this.timeout == 0) {
          this.onDoPurgeRoom();
        }
      }, 1000);
    },
    onDoPurgeRoom() {
      this.timeout = 0;
      clearInterval(this.timeoutTimer);
      this.timeoutTimer = null;
      this.isPurging = true;
      this.$matrix
        .purgeRoom(this.room.roomId, this.onPurgeStatus)
        .then(() => {
          this.showDialog = false;
          console.log("Purged room");
          this.$navigation.push({ name: "Home", params: { roomId: null } }, -1);
        })
        .catch((err) => {
          console.error("Error purging", err);
          this.status = this.$t("room.purge_failed");
        })
        .finally(() => {
          this.isPurging = false;
        });
    },

    onPurgeStatus(message) {
      this.status = message;
    },
  },
};
</script>

<style lang="scss">
@use "@/assets/css/chat.scss" as *;
</style>
