<template>
  <div
    :class="[prefixCls + '-hue']"
    tabindex="0"
    @click="$el.focus()"
    @keydown.left="handleLeft"
    @keydown.right="handleRight"
    @keydown.up="handleUp"
    @keydown.down="handleDown"
  >
    <div
      ref="container"
      :class="[prefixCls + '-hue-container']"
      @mousedown="handleMouseDown"
      @touchmove="handleChange"
      @touchstart="handleChange"
    >
      <div
        :style="{ top: 0, left: `${percent}%` }"
        :class="[prefixCls + '-hue-pointer']"
      >
        <div :class="[prefixCls + '-hue-picker']"></div>
      </div>
    </div>
  </div>
</template>
<script>
import HASMixin from "./hsaMixin";
import Prefixes from "./prefixMixin";
import { clamp } from "./utils";

export default {
  name: "Hue",
  mixins: [HASMixin, Prefixes],
  emits: ["input", "change"],
  data() {
    const normalStep = (1 / 360) * 25;
    const jumpStep = 20 * normalStep;

    return {
      left: -normalStep,
      right: normalStep,
      up: jumpStep,
      down: -jumpStep,
      powerKey: "shiftKey",
      percent: clamp((this.value.hsl.h * 100) / 360, 0, 100),
    };
  },
  watch: {
    value() {
      this.percent = clamp((this.value.hsl.h * 100) / 360, 0, 100);
    },
  },
  methods: {
    change(percent) {
      this.percent = clamp(percent, 0, 100);

      const { h, s, l, a } = this.value.hsl;
      const newHue = clamp((percent / 100) * 360, 0, 360);

      if (h !== newHue) {
        this.$emit("change", { h: newHue, s, l, a, source: "hsl" });
      }
    },
    input(percent) {
      this.percent = clamp(percent, 0, 100);

      const { h, s, l, a } = this.value.hsl;
      const newHue = clamp((percent / 100) * 360, 0, 360);

      if (h !== newHue) {
        this.$emit("input", { h: newHue, s, l, a, source: "hsl" });
      }
    },
    handleSlide(e, direction) {
      e.preventDefault();
      e.stopPropagation();

      if (e[this.powerKey]) {
        this.change(direction < 0 ? 0 : 100);
        return;
      }

      this.change(this.percent + direction);
    },
    handleInput(e) {
      e.preventDefault();
      e.stopPropagation();

      const left = this.getLeft(e);

      if (left < 0) {
        this.input(0);
        return;
      }

      const { clientWidth } = this.$refs.container;

      if (left > clientWidth) {
        this.input(100);
        return;
      }

      this.input((left * 100) / clientWidth);
    },
    handleChange(e) {
      e.preventDefault();
      e.stopPropagation();

      const left = this.getLeft(e);

      if (left < 0) {
        this.change(0);
        return;
      }

      const { clientWidth } = this.$refs.container;

      if (left > clientWidth) {
        this.change(100);
        return;
      }

      this.change((left * 100) / clientWidth);
    },
  },
};
</script>
