<!--
 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-list-item-avatar
    v-if="column === 'avatar'"
    :title="deedTitle"
    class="my-auto">
    <v-img :src="cardImage" />
  </v-list-item-avatar>
  <v-list-item-avatar v-else-if="column === 'nftId'" class="my-auto">
    <v-chip
      :href="etherscanLink"
      :color="cardTypeColor"
      :title="$t('deed.task.openDeedNftInfoInEtherscan')"
      target="_blank"
      rel="nofollow noreferrer noopener"
      class="my-auto"
      dark>
      {{ deed.nftId }}
    </v-chip>
  </v-list-item-avatar>
  <v-list-item
    v-else-if="column === 'title'"
    :title="title"
    :class="completed && 'text-decoration-line-through'"
    class="mx-0 px-0 px-sm-2 rounded-xl"
    @click="openTaskDrawer">
    <v-list-item-content class="ms-0 ms-sm-3 my-auto">
      <v-list-item-title class="text-truncate">{{ title }}</v-list-item-title>
    </v-list-item-content>
  </v-list-item>
  <v-list-item
    v-else-if="column === 'date'"
    class="mx-0">
    <date-format
      v-if="date"
      :value="date"
      class="text-no-wrap" />
  </v-list-item>
  <v-list-item v-else-if="column === 'actions'" class="mx-0">
    <v-list-item-action class="my-auto ms-0 me-4">
      <v-btn
        :href="taskLink"
        :title="$t('deed.task.openInNewWindow')"
        target="_blank"
        rel="nofollow noreferrer noopener"
        icon>
        <v-icon>fas fa-external-link-alt</v-icon>
      </v-btn>
    </v-list-item-action>
    <v-list-item-action class="my-auto mx-0">
      <v-btn
        :title="$t('deed.task.configureAndSendEmails')"
        icon
        @click="openTenantManagementDrawer">
        <v-icon>fa fa-cog</v-icon>
      </v-btn>
    </v-list-item-action>
  </v-list-item>
  <v-list-item v-else-if="column === 'taskLink'" class="mx-0">
    <v-list-item-action class="my-auto me-0 me-4">
      <v-btn
        :href="taskLink"
        :title="$t('deed.task.openInNewWindow')"
        target="_blank"
        rel="nofollow noreferrer noopener"
        icon>
        <v-icon>fas fa-external-link-alt</v-icon>
      </v-btn>
    </v-list-item-action>
  </v-list-item>
</template>
<script>
export default {
  props: {
    status: {
      type: String,
      default: null,
    },
    deed: {
      type: Object,
      default: null,
    },
    column: {
      type: String,
      default: null,
    },
    staticElements: {
      type: Boolean,
      default: false,
    },
  },
  data: () => ({
    task: null,
    changedCompleted: null,
  }),
  computed: {
    completed() {
      if (this.changedCompleted === null) {
        return this.deed?.completed;
      } else {
        return this.changedCompleted;
      }
    },
    title() {
      return this.task?.title || this.deedTitle;
    },
    deedTitle() {
      return `${this.cityName} - ${this.cardType}`;
    },
    cardTypeColor() {
      return this.$root.cardTypeColors[this.deed?.cardType];
    },
    provisioningStatusLabel() {
      return this.$t(`deeds.provisioning.management.${this.status}`);
    },
    etherscanLink() {
      return this.deed?.etherscanLink;
    },
    cityName() {
      return this.$root.cities[this.deed?.cityIndex];
    },
    cardType() {
      return this.$root.cardTypes[this.deed?.cardType];
    },
    cardImage() {
      return `/deeds-tenant-provisioning/images/nft/${this.cityName}-${this.cardType}.png`.toLowerCase();
    },
    taskLink() {
      return `${eXo.env.portal.context}/${eXo.env.portal.portalName}/tasks/taskDetail/${this.deed?.properties?.currentTaskId}`;
    },
    date() {
      return this.deed?.date  || 0;
    },
  },
  status(newVal, oldVal) {
    if (newVal !== oldVal && !this.staticElements) {
      this.refreshTask();
    }
  },
  created() {
    if (this.column === 'title') {
      if (this.staticElements) {
        this.refreshTask();
      } else {
        this.$root.$on('deed.event.tenantCompletedCommand', this.markTaskAsCompleted);
        this.refreshTaskIfNull();
      }
    }
  },
  beforeDestroy() {
    if (this.column === 'title' && !this.staticElements) {
      this.$root.$off('deed.event.tenantCompletedCommand', this.markTaskAsCompleted);
    }
  },
  methods: {
    markTaskAsCompleted(deed) {
      if (deed.nftId === this.deed.nftId
          && deed?.properties?.currentTaskId === this.deed?.properties?.currentTaskId
          && deed.completed !== this.completed) {
        this.changedCompleted = deed.completed;
        return this.refreshTask();
      }
    },
    refreshTaskIfNull() {
      if (this.deed.task) {
        this.task = this.deed.task;
        return Promise.resolve(this.task);
      } else {
        return this.refreshTask();
      }
    },
    refreshTask() {
      return this.$tenantProvisioningService.refreshDeedTask(this.deed)
        .then(task => this.task = task);
    },
    openTaskDrawer() {
      if (this.task) {
        document.querySelector('#drawers-overlay').click();
        window.setTimeout(() => {
          this.$root.$emit('open-deed-task', this.task);
        }, 500);
      }
    },
    openTenantManagementDrawer() {
      this.$root.$emit('open-tenant-management', this.deed);
    },
  },
};
</script>
