<!-- Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->
<template>
  <g>
    <rect
      x="0"
      y="0"
      :width="`${boundaryLeft}%`"
      height="100%"
      fill="var(--el-color-primary-light-8)"
      fill-opacity="0.6"
      pointer-events="none"
    />
    <rect
      :x="`${boundaryRight}%`"
      y="0"
      :width="`${100 - boundaryRight}%`"
      height="100%"
      fill="var(--el-color-primary-light-8)"
      fill-opacity="0.6"
      pointer-events="none"
    />
    <rect
      :x="`${boundaryLeft}%`"
      y="0"
      width="3"
      height="100%"
      fill="var(--el-color-primary-light-5)"
      transform="translate(-1)"
      pointer-events="none"
    />
    <rect
      :x="`${boundaryRight}%`"
      y="0"
      width="3"
      height="100%"
      fill="var(--el-color-primary-light-5)"
      transform="translate(-1)"
      pointer-events="none"
    />

    <rect
      v-if="minMouseDownX !== undefined && minCurrentX !== undefined"
      :x="`${Math.min(minMouseDownX, minCurrentX)}%`"
      y="0"
      :width="`${Math.abs(minMouseDownX - minCurrentX)}%`"
      height="100%"
      fill="var(--el-color-primary-light-6)"
      fill-opacity="0.4"
      pointer-events="none"
    />
    <rect
      v-if="maxMouseDownX !== undefined && maxCurrentX !== undefined"
      :x="`${Math.min(maxMouseDownX, maxCurrentX)}%`"
      y="0"
      :width="`${Math.abs(maxMouseDownX - maxCurrentX)}%`"
      height="100%"
      fill="var(--el-color-primary-light-6)"
      fill-opacity="0.4"
      pointer-events="none"
    />
    <rect
      :x="`${boundaryLeft}%`"
      y="0"
      width="6"
      height="40%"
      fill="var(--el-color-primary)"
      @mousedown="minRangeHandler.onMouseDown"
      cursor="pointer"
      transform="translate(-3)"
    />
    <rect
      :x="`${boundaryRight}%`"
      y="0"
      width="6"
      height="40%"
      fill="var(--el-color-primary)"
      @mousedown="maxRangeHandler.onMouseDown"
      cursor="pointer"
      transform="translate(-3)"
    />
  </g>
</template>
<script lang="ts" setup>
  import { computed, ref, onMounted } from "vue";
  import { useRangeTimestampHandler, adjustPercentValue } from "./useHooks";

  interface Props {
    minTimestamp: number;
    maxTimestamp: number;
    selectedMinTimestamp: number;
    selectedMaxTimestamp: number;
  }
  const emit = defineEmits<{
    (e: "setSelectedMinTimestamp", value: number): void;
    (e: "setSelectedMaxTimestamp", value: number): void;
  }>();
  const props = defineProps<Props>();
  const svgEle = ref<SVGSVGElement | null>(null);

  onMounted(() => {
    const element = document.querySelector(".trace-min-timeline svg") as SVGSVGElement;
    if (element) {
      svgEle.value = element;
    }
  });
  const maxOpositeX = computed(
    () => ((props.selectedMaxTimestamp - props.minTimestamp) / (props.maxTimestamp - props.minTimestamp)) * 100,
  );
  const minOpositeX = computed(
    () => ((props.selectedMinTimestamp - props.minTimestamp) / (props.maxTimestamp - props.minTimestamp)) * 100,
  );

  const minRangeHandler = computed(() => {
    return useRangeTimestampHandler({
      rootEl: svgEle.value,
      minTimestamp: props.minTimestamp,
      maxTimestamp: props.maxTimestamp,
      selectedTimestamp: props.selectedMaxTimestamp,
      isSmallerThanOpositeX: true,
      setTimestamp: (value) => emit("setSelectedMinTimestamp", value),
    });
  });
  const maxRangeHandler = computed(() =>
    useRangeTimestampHandler({
      rootEl: svgEle.value,
      minTimestamp: props.minTimestamp,
      maxTimestamp: props.maxTimestamp,
      selectedTimestamp: props.selectedMinTimestamp,
      isSmallerThanOpositeX: false,
      setTimestamp: (value) => emit("setSelectedMaxTimestamp", value),
    }),
  );

  const boundaryLeft = computed(() => {
    return adjustPercentValue(minOpositeX.value);
  });

  const boundaryRight = computed(() => adjustPercentValue(maxOpositeX.value));

  const minMouseDownX = computed(() => minRangeHandler.value.mouseDownX.value);
  const minCurrentX = computed(() => minRangeHandler.value.currentX.value);
  const maxMouseDownX = computed(() => maxRangeHandler.value.mouseDownX.value);
  const maxCurrentX = computed(() => maxRangeHandler.value.currentX.value);
</script>
