<i18n>
{
  "en": {
    "noNotifications": "No notifications.",
    "new": "New",
    "old": "Old",
    "notifications": "Notifications"
  },
  "de": {
    "noNotifications": "Keine Benachrichtigungen.",
    "new": "Neu",
    "old": "Alt",
    "notifications": "Benachrichtigungen"
  }
}
</i18n>

<template>
  <div>
    <v-menu v-model="menu" offset-y left :close-on-content-click="false" :nudge-bottom="10" content-class="notifications">
      <v-btn icon slot="activator">
        <v-badge :value="!menu && newAlertCount" color="red darken-3" bottom>
              <span slot="badge">{{newAlertCount}}</span>
              <v-icon>notifications</v-icon>
        </v-badge>
      </v-btn>

      <v-card>
        <v-card-text>
          <div style="max-height: 70vh; overflow: auto">
            <div v-if="alerts.length === 0" class="text-xs-center" style="width: 100%">{{ $t('noNotifications') }}</div>
            <div v-else class="flex-between-center">
              <v-subheader v-if="newAlertCount > 0">{{ $t('new') }}</v-subheader>
              <v-subheader v-else>{{ $t('notifications') }}</v-subheader>
              <v-btn @click="clearAll" icon small style="margin-right: 16px"><v-icon>clear_all</v-icon></v-btn>
            </div>
            <template v-for="(a, index) in alerts">
              <v-alert
                :key="'alert' + index"
                transition="scale-transition"
                :value="a.show"
                :type="a.type"
                :color="a.color"
                :icon="a.icon"
              >
                <div class="flex-between-center">
                  <div>{{ a.text }}</div>
                  <v-btn @click="closeAlert(a)" small :ripple="false" style="margin: 0; margin-top: -4px" icon><v-icon>close</v-icon></v-btn>
                </div>
              </v-alert>
              <v-subheader :key="'subheader' + index" v-if="newAlertCount < alerts.length && index == newAlertCount - 1">{{ $t('old') }}</v-subheader>
            </template>
          </div>
        </v-card-text>
      </v-card>
    </v-menu>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'

export default {
  name: 'NotificationsAlerts',
  data () {
    return {
      menu: false
    }
  },
  computed: {
    ...mapState('notifications', ['newAlertCount', 'alerts']),
    alertsShown () {
      return this.alerts.some(a => a.show)
    }
  },
  watch: {
    menu (value) {
      if (!value) {
        this.$socket.emit('notifications resetNewAlertCount')
        this.resetNewAlertCount()
      }
    }
  },
  methods: {
    ...mapMutations('notifications', ['resetNewAlertCount', 'removeAlert']),
    closeAlert (a, emit = true) {
      if (emit) this.$socket.emit('notifications closeAlert', a)
      this.removeAlert(a.id)
      if (!this.alertsShown) this.menu = false
    },
    clearAll (emit = true) {
      if (emit) this.$socket.emit('notifications clearAll')
      var timeout = this.alerts.length * 50
      setTimeout(this.closeMenu, timeout)
      var removeAlert = this.removeAlert
      this.alerts.forEach(a => {
        timeout -= 50
        setTimeout(function () {
          removeAlert(a.id)
        }, timeout)
      })
    },
    closeMenu () {
      this.menu = false
    }
  },
  created () {
    this.$socket.on('notifications clearAll', () => {
      this.clearAll(false)
    })
    this.$socket.on('notifications newAlert', data => {
      this.$alert(data, false)
    })
    this.$socket.on('notifications closeAlert', id => {
      this.closeAlert(id, false)
    })
    this.$socket.on('notifications resetNewAlertCount', () => {
      this.resetNewAlertCount()
    })
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

@media (max-width: 960px) {
  .notifications {
    right: 12px;
    max-width: 100%;
  }
}

@media (min-width: 961px) {
  .notifications {
    width: 600px;

  }
}

.flex-between-center {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

</style>
