<i18n>
{
  "en": {
    "assigned": "Assigned",
    "active": "Active",
    "source": "Source",
    "id": "ID",
    "name": "Name"
  },
  "de": {
    "assigned": "Zugewiesen",
    "active": "Aktiv",
    "source": "Quelle",
    "id": "ID",
    "name": "Name"
  }
}
</i18n>

<template>
  <v-tooltip top open-delay="200">
    <template #activator="{ on }">
      <v-chip v-on="on" small :color="color" :text-color="color ? 'white' : ''" :label="isEvent">
        <span class="chip-text">{{ config.name || config.id }}</span>
      </v-chip>
    </template>
    <div>
      <div v-if="active" class="success--text" style="text-align: center; font-weight: bold; font-size: 1.2em">{{ $t('active') }}</div>
      <div v-if="assigned" class="primary--text" style="text-align: center; font-weight: bold; font-size: 1.2em">{{ $t('assigned') }}</div>
      <v-divider v-if="active || assigned" style="margin-bottom: 4px"></v-divider>
      <div>
        <div v-for="(source, index) in sources" :key="source.data.id">
          <v-divider v-if="index > 0" style="margin-bottom: 4px"></v-divider>
          <div style="text-align: center; font-weight: bold;">{{ source.title }}</div>
          <table>
            <tr><td class="config-source-key">{{ $t('source') }}</td><td>{{ source.data.type }}</td></tr>
            <tr v-if="source.data.id"><td class="config-source-key">{{ $t('id') }}</td><td>{{ source.data.id }}</td></tr>
            <tr v-if="source.data.name"><td class="config-source-key">{{ $t('name') }}</td><td>{{ source.data.name }}</td></tr>
          </table>
        </div>
      </div>
    </div>
  </v-tooltip>
</template>

<script>

export default {
  name: 'ConfigChip',
  props: {
    config: {
      type: Object,
      default: () => {}
    },
    active: {
      type: Boolean,
      default: false
    },
    assigned: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
    }
  },
  computed: {
    color () {
      if (this.assigned) return 'primary'
      if (this.active) return 'success'
      return ''
    },
    isEvent () {
      return this.sources.some(source => (source.data.type === 'IMPORTANT_EVENT' || source.data.type === 'EVENT'))
    },
    sources () {
      return this.getSources(this.config)
    }
  },
  methods: {
    getSources (config) {
      const sources = []
      if (config.merged) {
        config.configs.forEach(c => {
          sources.push(...this.getSources(c))
        })
      } else {
        if (Array.isArray(config.source)) {
          sources.push(...config.source.map(source => ({
            title: '[' + config.id + '] ' + config.name,
            data: source
          })))
        } else {
          sources.push({
            title: '[' + config.id + '] ' + config.name,
            data: config.source
          })
        }
      }
      return sources
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.config-source-key {
  vertical-align: top;
  font-weight: bold;
  text-align: right;
  padding-right: 16px;
}
</style>
