<script setup lang="ts">
const props = defineProps({
  tile: Object,
  top: Number,
  bottom: Number,
  revealed: {
    type: Boolean,
    default: false
  }
})

function getPipClasses(value: number | undefined): string[] {
  if (value === undefined) return []

  const positions = [
    [],
    ['c'],
    ['l-t', 'r-b'],
    ['l-t', 'c', 'r-b'],
    ['l-t', 'r-t', 'l-b', 'r-b'],
    ['l-t', 'r-t', 'c', 'l-b', 'r-b'],
    ['l-t', 'r-t', 'l-m', 'r-m', 'l-b', 'r-b']
  ]

  return positions[value]
}
</script>

<template>
  {{ tile }}
  <button @click="$emit('tile-clicked')">
    <div v-if="!revealed" class="tile"></div>
    <div v-if="revealed" class="tile shown">
      <div class="half top">
        <div
          :key="$index"
          v-for="(clasz, $index) in getPipClasses(top)"
          class="pip"
          :class="clasz"
        ></div>
      </div>
      <div class="half bottom">
        <div
          :key="$index"
          v-for="(clasz, $index) in getPipClasses(bottom)"
          class="pip"
          :class="clasz"
        ></div>
      </div>
    </div>
  </button>
</template>

<style scoped lang="scss">
button {
  border: none;
  background: none;
  padding: 0;
  margin: 0;
  line-height: 0;
}
.tile {
  cursor: pointer;
  display: inline-block;
  border: 0;
  padding: 0;
  border-radius: 4px;
  color: #000;
  background-color: #fff;
  width: 50px;
  height: 100px;

  &.shown {
    display: flex;
    flex-direction: column;
  }

  // &.shown::after {
  //   content: '';
  //   display: inline-block;
  //   border-bottom: 1px solid #ddd;
  //   width: calc(100% - 4px);
  //   height: 0;
  //   position: relative;
  //   margin: 0 2px;
  //   top: 30px;
  // }
}

.top {
  margin: 0 2px;
  border-bottom: 2px solid #ddd;
  width: calc(100% - 4px);
}

.half {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

.pip {
  width: 8px;
  height: 8px;
  background: black;
  border-radius: 50%;
  position: absolute;
}

.c {
  left: 40%;
  top: 40%;
}

.l {
  &-t {
    left: 10%;
    top: 10%;
  }
  &-m {
    left: 10%;
    top: 40%;
  }
  &-b {
    left: 10%;
    bottom: 10%;
  }
}

.r {
  &-t {
    right: 10%;
    top: 10%;
  }
  &-m {
    right: 10%;
    top: 40%;
  }
  &-b {
    right: 10%;
    bottom: 10%;
  }
}
</style>
