<!--
 This file is part of the Meeds project (https://meeds.io/).
 
 Copyright (C) 2020 - 2022 Meeds Association contact@meeds.io
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 3 of the License, or (at your option) any later version.
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public License
 along with this program; if not, write to the Free Software Foundation,
 Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
-->
<template>
  <v-snackbar
    v-model="snackbar"
    :left="!$vuetify.rtl"
    :right="$vuetify.rtl"
    content-class="pa-0"
    color="transparent"
    max-height="50%"
    width="30%"
    max-width="50%"
    timeout="-1"
    elevation="0"
    light>
    <v-expansion-panels
      v-model="panel"
      class="modal-z-index border-box-sizing white"
      eager
      dense>
      <v-expansion-panel class="modal-z-index white">
        <v-expansion-panel-header class="info info--text px-2" ripple>
          <span>
            <v-icon
              color="info"
              size="18"
              class="me-2">fas fa-info-circle</v-icon>
            {{ $t('deed.event.tenantCommandNotifications') }}
            <strong class="error--text">({{ updatedDeeds.length }})</strong>
          </span>
        </v-expansion-panel-header>
        <v-expansion-panel-content class="mx-n6" eager>
          <v-card
            max-height="50vh"
            class="overflow-y-auto overflow-x-hidden mb-n4"
            flat
            light>
            <deed-provisioning-management-tenants-table
              :loading="loading"
              :status="status"
              :deeds="updatedDeeds"
              :headers="headers"
              :options="options"
              class="mt-n1"
              group-by="eventId"
              static-elements
              hide-loading
              light />
          </v-card>
        </v-expansion-panel-content>
      </v-expansion-panel>
    </v-expansion-panels>
  </v-snackbar>
</template>
<script>
export default {
  data: () => ({
    headers: [{
      text: '',
      align: 'center',
      value: 'avatar',
      width: '60px',
      class: 'pxdddd-0',
    }, {
      text: '',
      align: 'center',
      value: 'nftId',
      width: '60px',
      class: 'pxddd-0',
    }, {
      text: '',
      align: 'left',
      value: 'title',
      width: 'auto',
    }, {
      text: '',
      align: 'center',
      value: 'taskLink',
      width: '75px',
    }],
    options: {
      groupBy: 'eventId',
    },
    updatedDeeds: [],
    snackbar: false,
    alert: false,
    panel: false,
  }),
  created() {
    this.$tenantProvisioningWebSocket.initCometd(this.handleUpdates);
  },
  methods: {
    handleUpdates(updatedDeed) {
      const newDeedEvent = JSON.parse(JSON.stringify(updatedDeed));
      this.$root.$emit(updatedDeed.eventName, updatedDeed);
      newDeedEvent.date = Date.now();
      const progress = newDeedEvent.tenantProvisioningStatus?.includes('IN_PROGRESS');
      const stopCommand = newDeedEvent.eventName.includes('StopCommand');
      const startCommand = newDeedEvent.eventName.includes('StartCommand');
      const suffix = (stopCommand || startCommand) && (progress && 'InProgress' || 'Confirmed') || '';
      newDeedEvent.eventId = `${newDeedEvent.eventName}${suffix}`;
      this.updatedDeeds.splice(0, 0, newDeedEvent);
      this.snackbar = true;
      this.alert = true;
      this.panel = true;
    },
    dismissMessages() {
      this.updatedDeeds = [];
      this.snackbar = false;
      this.alert = false;
      this.panel = false;
    },
  },
};
</script>