<template>
  <div @click="$emit('select')" class="statblock">
    <h2 class="name" v-if="subject.perspective === firstperson">
      You
      <div class="tooltip-template">
        <div class="tooltip-title">{{ subject.title }}</div>
        <div class="tooltip-body">{{ subject.desc }}</div>
      </div>
    </h2>
    <h2 class="name" v-if="subject.perspective !== firstperson">
      {{subject.name.all.capital}}
      <div class="tooltip-template">
        <div class="tooltip-title">{{ subject.title }}</div>
        <div class="tooltip-body">{{ subject.desc }}</div>
      </div>
    </h2>
      <div class="stat-entry" v-for="vigor in Object.keys(subject.vigors)" v-bind:key="vigor">
        <div class="healthbar" v-bind:style="{'--fullness': (subject.vigors[vigor]/subject.maxVigors[vigor]*100) + '%', '--color': vigorColor(subject.vigors[vigor], subject.maxVigors[vigor]) }">
          <i :class="vigorIcons[vigor]" />
          <div class="healthbar-value"> {{ subject.vigors[vigor].toFixed(0) + '/' + subject.maxVigors[vigor].toFixed(0) }}</div>
        </div>
        <div class="tooltip-template">
          <div class="tooltip-title">{{ vigor }}</div>
          <div class="tooltip-body">{{ vigorDescs[vigor] }}</div>
        </div>
      </div>
    <div class="stat-line stats">
      <div :class="statClass(subject.stats[stat], subject.baseStats[stat])" v-for="stat in Object.keys(subject.stats)" v-bind:key="stat">
        <i :class="statIcons[stat]" />
        <div class="stat-value">{{subject.stats[stat].toFixed(0)}}</div>
        <div class="tooltip-template">
          <div class="tooltip-title">{{ stat }}</div>
          <div class="tooltip-body">{{ statDescs[stat] }}</div>
        </div>
      </div>
    </div>
    <div class="stat-line vore-stats">
      <div class="stat-entry" v-for="stat in Object.keys(subject.voreStats)" v-bind:key="stat">
        <i :class="voreStatIcons[stat]" />
        <div class="stat-value">{{subject.voreStats[stat].toFixed(0)}}</div>
        <div class="tooltip-template">
          <div class="tooltip-title">{{ stat }}</div>
          <div class="tooltip-body">{{ voreStatDescs[stat] }}</div>
        </div>
      </div>
    </div>
    <div>Status: {{subject.status}}</div>
    <button v-if="subject.perspective !== firstperson" @click="subject.perspective = firstperson">First-person</button>
    <button v-if="subject.perspective !== thirdperson" @click="subject.perspective = thirdperson">Third-person</button>
    <button class="if-not-selected" @click.stop="$emit('selectAlly')">Select ally as target</button>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue, Watch, Emit } from 'vue-property-decorator'
import { Creature, POV } from '@/game/entity'
import { Stats, Stat, StatIcons, StatDescs, Vigor, VigorIcons, VigorDescs, VoreStatDescs, VoreStatIcons } from '@/game/combat'
import ContainerView from './ContainerView.vue'
import tippy, { createSingleton } from 'tippy.js'
import 'tippy.js/dist/tippy.css'

@Component({
  components: {
    ContainerView
  },
  methods: {
    vigorColor (value: number, max: number) {
      if (value * 5 <= max) {
        return 'red'
      } else if (value * 3 <= max) {
        return 'yellow'
      } else {
        return 'green'
      }
    },
    statClass (value: number, normal: number) {
      if (value < normal) {
        return 'stat-entry crit'
      } else if (value > normal) {
        return 'stat-entry buff'
      } else {
        return 'stat-entry'
      }
    }
  }
})
export default class Statblock extends Vue {
  @Prop({ type: Creature, required: true })
  subject!: Creature

  private vigorIcons = VigorIcons
  private statIcons = StatIcons
  private voreStatIcons = VoreStatIcons
  private vigorDescs = VigorDescs
  private statDescs = StatDescs
  private voreStatDescs = VoreStatDescs
  private vigor = Vigor

  firstperson: POV = POV.First
  thirdperson: POV = POV.Third

  mounted () {
    const statEntries = Array.from(this.$el.querySelectorAll(".stat-entry"))
    const name = Array.from(this.$el.querySelectorAll(".name"))
    const tippyInstances = statEntries.concat(name).map(elem => {
      const tooltip = elem.querySelector(".tooltip-template") as HTMLElement

      return tippy(elem, {
        content: tooltip
      })
    })
    createSingleton(tippyInstances, { delay: 500 })
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h2 {
  margin-bottom: 8pt;
  font-size: 200%;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}

.statblock {
  flex: 1 0;
  flex-basis: 100pt;
  margin: 0pt 4pt 0pt;
  user-select: none;
}

.stat-line {
  width: 100%;
  display: flex;
  justify-content: space-evenly;
  flex-wrap: wrap;
}

.stat-entry {
  position: relative;
  font-size: 10pt;
  padding-top: 2pt;
  padding-bottom: 2pt;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  user-select: none;
}

.stat-value {
  padding-top: 4pt;
  padding-bottom: 4pt;
}

.healthbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  --color: green;
  --fullness: 100%;
  position: relative;
  width: 90%;
  margin: 0% 5% 0%;
  height: 14pt;
  border-radius: 2pt;
  border-width: 2pt;
  border-color: gray;
  border-style: outset;
  background: linear-gradient(90deg, var(--color) var(--fullness), black var(--fullness), black);
}

.stat-entry .healthbar i {
  flex: 0 1;
  flex-basis: 20pt;
  font-size: 14pt;
}

.healthbar .healthbar-value {
  flex: 1 0;
  font-size: 12pt;
  color: #bbb;
}

.stat-entry i {
  font-size: 150%;
}
.stat-entry.low {
  color: yellow;
}

.stat-entry.crit {
  color: red;
}

.stat-entry.buff {
  color: green;
}

.statblock[data-active] {
  background: #444;
  border-radius: 4px;
}

.statblock[data-active-ally] {
  background: #744;
  border-radius: 4px;
}

.statblock[data-active] .stats,
.statblock[data-active] .vore-stats {
  display: flex;
}

.statblock[data-active] .if-not-selected {
  display: none;
}
</style>

<style>

.left-stats .stat-entry::after {
  transform: translate(calc(0% + 16pt), -100%);
}
.left-stats .stat-entry::before {
  transform: translate(calc(0% + 16pt), calc(-100% + 18pt + 16pt));
}
.right-stats .stat-entry::after {
  transform: translate(calc(-100% + 16pt), -100%);
}
.right-stats .stat-entry::before {
  transform: translate(calc(-100% + 16pt), calc(-100% + 18pt + 16pt));
}

</style>
