<template>
  <td
    @mouseover="showNumberTemp = true"
    @mouseout="showNumberTemp = false"
    class="square-wrapper"
  >
    <div v-if="!hide" class="square-inner correlation">
      <div class="correlation-viz" :style="correlationStyle"></div>
    </div>
    <div v-if="!hide" class="square-inner maximum-sub">
      <div class="correlation-viz" :style="maxSubcorrelationStyle"></div>
      <div v-show="showNumber || showNumberTemp" class="value">
        <p>
          {{ Number(maximum_subcorrelaiton).toFixed(2) }}
        </p>
        <p>({{ Number(correlation).toFixed(2) }})</p>
      </div>
    </div>
  </td>
</template>

<script>
function correlationToPercent(correlation) {
  const map = (value, x1, y1, x2, y2) =>
    ((value - x1) * (y2 - x2)) / (y1 - x1) + x2;

  return map(Math.abs(correlation) * 100, 0, 100, 10, 100).toFixed() + "%";
}

export default {
  props: {
    correlation: Number,
    maximum_subcorrelaiton: Number,
    hide: Boolean,
    showNumber: Boolean,
    correlation_scale: Function,
    maximum_subcorrelation_scale: Function,
  },
  data() {
    return {
      showNumberTemp: false,
    };
  },
  computed: {
    correlationStyle() {
      return {
        ...this.squareStyle(this.correlation),
        backgroundColor: this.correlation_scale(this.correlation),
      };
    },

    maxSubcorrelationStyle() {
      return {
        ...this.squareStyle(this.maximum_subcorrelaiton),
        backgroundColor: this.maximum_subcorrelation_scale(
          this.maximum_subcorrelaiton
        ),
      };
    },
  },
  methods: {
    squareStyle(val) {
      return {
        height: correlationToPercent(val),
        width: correlationToPercent(val),
      };
    },
  },
};
</script>

<style scoped>
.value {
  font-size: small;
  position: absolute;
  text-align: center;
  color: black;
  text-shadow: 0.05em 0 white, 0 0.05em white, -0.05em 0 white, 0 -0.05em white,
    -0.05em -0.05em white, -0.05em 0.05em white, 0.05em -0.05em white,
    0.05em 0.05em white;
}

.value p {
  margin: 0px;
}
.correlation-viz {
  display: flex;
  justify-content: center;
  align-items: center;
}
.square-wrapper {
  margin: 1px;
  flex: 1 0 auto;
  position: relative;
  max-width: 30px;
  max-height: 30px;
}

.square-wrapper:after {
  content: "";
  float: left;
  display: block;
  padding-top: 100%;
}

.square-wrapper .square-inner {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>