<script setup lang="ts">
import { computed } from 'vue'
import { useBadnessVisuals } from '@/hooks/badnessVisualizer'

import { DomainInfo } from '@/types/api/domain'
import BadnessMeter from './BadnessMeter.vue'

const props = defineProps<{
  info: DomainInfo
  selected: boolean
}>()

const emit = defineEmits<{
  (e: 'hover', name: string): void
}>()

const badness = computed(() => {
  const results = props.info.results
  const final = results.find(r => r.final)
  const last = results[results.length - 1]
  const result = final || last
  return (result?.badness ?? 0) * 100
})

const { hue } = useBadnessVisuals(badness.value)
</script>

<template>
  <HListItem
    :key="info.domain_name"
    class="domain-box"
    :class="{ selected: selected }"
    :style="{ '--hue': hue }"
    href="#"
    @mouseenter="emit('hover', String(info.domain_name))"
  >
    <div class="offenses" v-if="info.offense_count > 0">
      <span class="offense-info">{{ info.offense_count }} offense(s)</span>
      <span class="offense-info">{{ info.event_flow_count }} EFC</span>
    </div>
    <div class="name">
      {{ info.domain_name }}
    </div>
    <div class="infobar">
      <div class="stat">
        <Icon name="globe" />
        {{ info.geo_data.length }}
      </div>
      <div class="badness">
        <BadnessMeter :badness="badness" />
        <div class="badness-number">
          {{ Math.round(badness * 10) / 10 }}%
        </div>
      </div>
    </div>
  </HListItem>
</template>

<style>
.domain-box {
  background-color: hsl(var(--hue) 60% 30% / 50%);
}

.domain-box.selected {
  background-color: hsl(var(--bg0));
}

.domain-box > a {
  display: inline-block;
  width: 100%;
  overflow: hidden;
}

.domain-box > a:hover {
  background-color: hsl(var(--bg0) / 10%);
}

.domain-box.selected > a:hover {
  background-color: hsl(var(--bg0));
}

.name {
  font-family: "IBM Plex Sans Condensed";
  font-size: 0.9em;
  white-space: nowrap;
  hyphens: none;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-block-start: 0.5em;
}

.infobar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-block: 0.5em;
}

.stat {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  margin-inline-end: 0.25em;
  font-size: 0.85em;
  font-family: "IBM Plex Mono";
  width: 6ch;
}

.stat svg {
  font-size: 1.2em;
  margin-inline-end: 0.25em;
}

.badness {
  flex-grow: 1;
  display: flex;
  align-items: center;
}

.badness-number {
  font-family: "IBM Plex Sans Condensed";
  font-weight: bolder;
  width: 5.5ch;
  text-align: end;
  font-size: 0.7em;
  margin-inline-start: 0.5em;
}

.offenses {
  display: flex;
  align-items: center;
  font-size: 0.8em;
  margin-block-start: 0.5em;
  gap: 0.75em;
}
.offense-info {
  display: inline-block;
  font-weight: bolder;
  background-color: hsl(var(--bg0));
  color: hsl(var(--caution));
  border: 1px solid;
  border-inline-start-width: 0.5em;
  padding: 0.25em 0.65em;
  border-radius: 0.35em;
}
</style>